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