www/database.php
[monitor.git] / nodebad.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7
8
9 import database
10 import comon
11 import threadpool
12 import syncplcdb
13 from nodequery import verify,query_to_dict,node_select
14 from nodecommon import *
15
16 import plc
17 api = plc.getAuthAPI()
18 from unified_model import *
19 from const import MINUP
20
21 round = 1
22 externalState = {'round': round, 'nodes': {}}
23 count = 0
24
25 def main(config):
26         global externalState
27         externalState = database.if_cached_else(1, config.dbname, lambda : externalState) 
28         if config.increment:
29                 # update global round number to force refreshes across all nodes
30                 externalState['round'] += 1
31
32         l_nodes = syncplcdb.create_plcdb()
33         l_plcnodes = database.dbLoad("l_plcnodes")
34
35         l_nodes = get_nodeset(config)
36         #if config.node:
37         #       l_nodes = [config.node]
38         ##else:
39         #       l_nodes = [node['hostname'] for node in l_plcnodes]
40         
41         checkAndRecordState(l_nodes, l_plcnodes)
42
43 def checkAndRecordState(l_nodes, l_plcnodes):
44         global externalState
45         global count
46         global_round = externalState['round']
47
48         for nodename in l_nodes:
49                 if nodename not in externalState['nodes']:
50                         externalState['nodes'][nodename] = {'round': 0, 'values': []}
51
52                 node_round   = externalState['nodes'][nodename]['round']
53                 if node_round < global_round:
54                         # do work
55                         values = collectStatusAndState(nodename, l_plcnodes)
56                         global_round = externalState['round']
57                         externalState['nodes'][nodename]['values'] = values
58                         externalState['nodes'][nodename]['round'] = global_round
59                 else:
60                         count += 1
61
62                 if count % 20 == 0:
63                         database.dbDump(config.dbname, externalState)
64
65         database.dbDump(config.dbname, externalState)
66
67 fb = database.dbLoad('findbad')
68
69 def getnodesup(nodelist):
70         up = 0
71         for node in nodelist:
72                 if node['hostname'] in fb['nodes'].keys():
73                         try:
74                                 if fb['nodes'][node['hostname']]['values']['state'] == "BOOT":
75                                         up = up + 1
76                         except:
77                                 pass
78         return up
79
80 def get(fb, path):
81         indexes = path.split("/")
82         values = fb
83         for index in indexes:
84                 if index in values:
85                         values = values[index]
86                 else:
87                         return None
88         return values
89
90 def collectStatusAndState(nodename, l_plcnodes):
91         global count
92
93         d_node = None
94         for node in l_plcnodes:
95                 if node['hostname'] == nodename:
96                         d_node = node
97                         break
98         if not d_node:
99                 return None
100
101         pf = PersistFlags(nodename, 1, db='node_persistflags')
102
103         if not pf.checkattr('last_changed'):
104                 pf.last_changed = time.time()
105                 
106         pf.last_checked = time.time()
107
108         if not pf.checkattr('status'):
109                 pf.status = "unknown"
110
111         state_path     = "nodes/" + nodename + "/values/state"
112         bootstate_path = "nodes/" + nodename + "/values/plcnode/boot_state"
113
114         if get(fb, state_path) == "BOOT":
115                 if pf.status != "good": pf.last_changed = time.time()
116                 pf.status = "good"
117         elif get(fb, state_path)  == "DEBUG":
118                 bs = get(fb, bootstate_path)
119                 if pf.status != bs: pf.last_changed = time.time()
120                 pf.status = bs
121         else:
122                 if pf.status != "down": pf.last_changed = time.time()
123                 pf.status = "down"
124
125         count += 1
126         print "%d %35s %s since(%s)" % (count, nodename, pf.status, diff_time(pf.last_changed))
127         # updated by other modules
128         #pf.enabled = 
129         #pf.suspended = 
130
131         pf.save()
132
133         return True
134
135 if __name__ == '__main__':
136         import parser as parsermodule
137         parser = parsermodule.getParser(['nodesets'])
138         parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, 
139                                                 increment=False, dbname="nodebad", cachenodes=False)
140         
141         parser.add_option("", "--dbname", dest="dbname", metavar="FILE", 
142                                                 help="Specify the name of the database to which the information is saved")
143         parser.add_option("-i", "--increment", action="store_true", dest="increment", 
144                                                 help="Increment round number to force refresh or retry")
145         parser = parsermodule.getParser(['defaults'], parser)
146         config = parsermodule.parse_args(parser)
147
148         try:
149                 main(config)
150         except Exception, err:
151                 import traceback
152                 print traceback.print_exc()
153                 print "Exception: %s" % err
154                 print "Saving data... exitting."
155                 database.dbDump(config.dbname, externalState)
156                 sys.exit(0)