8aacf714f87bec95d647cc08c04f2f29a8bdbd39
[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         if config.node:
36                 l_nodes = [config.node]
37         else:
38                 l_nodes = [node['hostname'] for node in l_plcnodes]
39         
40         checkAndRecordState(l_nodes, l_plcnodes)
41
42 def checkAndRecordState(l_nodes, l_plcnodes):
43         global externalState
44         global count
45         global_round = externalState['round']
46
47         for nodename in l_nodes:
48                 if nodename not in externalState['nodes']:
49                         externalState['nodes'][nodename] = {'round': 0, 'values': []}
50
51                 node_round   = externalState['nodes'][nodename]['round']
52                 if node_round < global_round:
53                         # do work
54                         values = collectStatusAndState(nodename, l_plcnodes)
55                         global_round = externalState['round']
56                         externalState['nodes'][nodename]['values'] = values
57                         externalState['nodes'][nodename]['round'] = global_round
58                 else:
59                         count += 1
60
61                 if count % 20 == 0:
62                         database.dbDump(config.dbname, externalState)
63
64         database.dbDump(config.dbname, externalState)
65
66 fb = database.dbLoad('findbad')
67
68 def getnodesup(nodelist):
69         up = 0
70         for node in nodelist:
71                 if node['hostname'] in fb['nodes'].keys():
72                         try:
73                                 if fb['nodes'][node['hostname']]['values']['state'] == "BOOT":
74                                         up = up + 1
75                         except:
76                                 pass
77         return up
78
79 def get(fb, path):
80         indexes = path.split("/")
81         values = fb
82         for index in indexes:
83                 if index in values:
84                         values = values[index]
85                 else:
86                         return None
87         return values
88
89 def collectStatusAndState(nodename, l_plcnodes):
90         global count
91
92         d_node = None
93         for node in l_plcnodes:
94                 if node['hostname'] == nodename:
95                         d_node = node
96                         break
97         if not d_node:
98                 return None
99
100         pf = PersistFlags(nodename, 1, db='node_persistflags')
101
102         if not pf.checkattr('last_changed'):
103                 pf.last_changed = time.time()
104                 
105         pf.last_checked = time.time()
106
107         if not pf.checkattr('status'):
108                 pf.status = "unknown"
109
110         state_path     = "nodes/" + nodename + "/values/state"
111         bootstate_path = "nodes/" + nodename + "/values/plcnode/boot_state"
112
113         if get(fb, state_path) == "BOOT":
114                 if pf.status != "good": pf.last_changed = time.time()
115                 pf.status = "good"
116         elif get(fb, state_path)  == "DEBUG":
117                 bs = get(fb, bootstate_path)
118                 if pf.status != bs: pf.last_changed = time.time()
119                 pf.status = bs
120         else:
121                 if pf.status != "down": pf.last_changed = time.time()
122                 pf.status = "down"
123
124         count += 1
125         print "%d %35s %s since(%s)" % (count, nodename, pf.status, diff_time(pf.last_changed))
126         # updated by other modules
127         #pf.enabled = 
128         #pf.suspended = 
129
130         pf.save()
131
132         return True
133
134 if __name__ == '__main__':
135         import parser as parsermodule
136         parser = parsermodule.getParser(['nodesets'])
137         parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, 
138                                                 increment=False, dbname="nodebad", cachenodes=False)
139         
140         parser.add_option("", "--dbname", dest="dbname", metavar="FILE", 
141                                                 help="Specify the name of the database to which the information is saved")
142         parser.add_option("-i", "--increment", action="store_true", dest="increment", 
143                                                 help="Increment round number to force refresh or retry")
144         parser = parsermodule.getParser(['defaults'], parser)
145         config = parsermodule.parse_args(parser)
146
147         try:
148                 main(config)
149         except Exception, err:
150                 import traceback
151                 print traceback.print_exc()
152                 print "Exception: %s" % err
153                 print "Saving data... exitting."
154                 database.dbDump(config.dbname, externalState)
155                 sys.exit(0)