changes for 3.0
[monitor.git] / pcubad.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7
8 from reboot import pcu_name
9
10 import database
11 import comon
12 import threadpool
13 import syncplcdb
14 from nodequery import verify,query_to_dict,node_select
15 import parser as parsermodule
16 from nodecommon import *
17
18 import plc
19 api = plc.getAuthAPI()
20 from unified_model import *
21 from const import MINUP
22
23 round = 1
24 externalState = {'round': round, 'nodes': {}}
25 count = 0
26
27 def main(config):
28         global externalState
29         externalState = database.if_cached_else(1, config.dbname, lambda : externalState) 
30         if config.increment:
31                 # update global round number to force refreshes across all pcus
32                 externalState['round'] += 1
33
34         l_plcpcus = database.if_cached_else_refresh(1, 1, "pculist", lambda : plc.GetPCUs())
35
36         l_pcu = None
37         if config.pcu:
38                 for pcu in l_plcpcus:
39                         if pcu['hostname'] == config.pcu  or pcu['ip'] == config.pcu:
40                                 l_pcus = [pcu['pcu_id']]
41                 if not l_pcu:
42                         print "ERROR: could not find pcu %s" % config.pcu
43                         sys.exit(1)
44         else:
45                 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
46         
47         checkAndRecordState(l_pcus, l_plcpcus)
48
49 def checkAndRecordState(l_pcus, l_plcpcus):
50         global externalState
51         global count
52         global_round = externalState['round']
53
54         for pcuname in l_pcus:
55                 if pcuname not in externalState['nodes']:
56                         externalState['nodes'][pcuname] = {'round': 0, 'values': []}
57
58                 pcu_round   = externalState['nodes'][pcuname]['round']
59                 if pcu_round < global_round:
60                         # do work
61                         values = collectStatusAndState(pcuname, l_plcpcus)
62                         global_round = externalState['round']
63                         externalState['nodes'][pcuname]['values'] = values
64                         externalState['nodes'][pcuname]['round'] = global_round
65                 else:
66                         count += 1
67
68                 if count % 20 == 0:
69                         database.dbDump(config.dbname, externalState)
70
71         database.dbDump(config.dbname, externalState)
72
73 fbpcu = database.dbLoad('findbadpcus')
74 hn2lb = database.dbLoad("plcdb_hn2lb")
75
76 def get(fb, path):
77         indexes = path.split("/")
78         values = fb
79         for index in indexes:
80                 if index in values:
81                         values = values[index]
82                 else:
83                         return None
84         return values
85
86 def collectStatusAndState(pcuname, l_plcpcus):
87         global count
88
89         d_pcu = None
90         for pcu in l_plcpcus:
91                 if pcu['pcu_id'] == pcuname:
92                         d_pcu = pcu
93                         break
94         if not d_pcu:
95                 return None
96
97         pf = PersistFlags(pcuname, 1, db='pcu_persistflags')
98
99         if not pf.checkattr('last_changed'):
100                 pf.last_changed = time.time()
101                 
102         pf.last_checked = time.time()
103
104         if not pf.checkattr('valid'):
105                 pf.valid = "unknown"
106                 pf.last_valid = 0
107
108         if not pf.checkattr('status'):
109                 pf.status = "unknown"
110
111         state_path     = "nodes/id_" + str(pcuname) + "/values/reboot"
112         bootstate_path = "nodes/id_" + str(pcuname) + "/values/plcpcu/boot_state"
113
114         current_state = get(fbpcu, state_path)
115         if current_state == 0:
116                 if pf.status != "good": pf.last_changed = time.time()
117                 pf.status = "good"
118         elif current_state == 'NetDown':
119                 if pf.status != "netdown": pf.last_changed = time.time()
120                 pf.status = "netdown"
121         elif current_state == 'Not_Run':
122                 if pf.status != "badconfig": pf.last_changed = time.time()
123                 pf.status = "badconfig"
124         else:
125                 if pf.status != "error": pf.last_changed = time.time()
126                 pf.status = "error"
127
128         count += 1
129         print "%d %35s %s since(%s)" % (count, pcu_name(d_pcu), pf.status, diff_time(pf.last_changed))
130         # updated by other modules
131         #pf.enabled = 
132         #pf.suspended = 
133
134         pf.save()
135
136         return True
137
138 if __name__ == '__main__':
139         parser = parsermodule.getParser()
140         parser.set_defaults(filename=None, pcu=None, pcuselect=False, pcugroup=None, 
141                                                 increment=False, dbname="pcubad", cachepcus=False)
142         parser.add_option("", "--pcu", dest="pcu", metavar="hostname", 
143                                                 help="Provide a single pcu to operate on")
144         parser.add_option("", "--pculist", dest="pculist", metavar="file.list", 
145                                                 help="Provide a list of files to operate on")
146
147         parser.add_option("", "--dbname", dest="dbname", metavar="FILE", 
148                                                 help="Specify the name of the database to which the information is saved")
149         parser.add_option("-i", "--increment", action="store_true", dest="increment", 
150                                                 help="Increment round number to force refresh or retry")
151         config = parsermodule.parse_args(parser)
152
153         try:
154                 main(config)
155         except Exception, err:
156                 import traceback
157                 print traceback.print_exc()
158                 from nodecommon import email_exception
159                 email_exception()
160                 print "Exception: %s" % err
161                 print "Saving data... exitting."
162                 database.dbDump(config.dbname, externalState)
163                 sys.exit(0)