Changed 'import auth' statements to use plc.py or monitorconfig.py
[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
16 import plc
17 api = plc.getAuthAPI()
18 from unified_model import *
19 from monitor_policy 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 pcus
30                 externalState['round'] += 1
31
32         l_plcpcus = database.if_cached_else_refresh(1, 1, "pculist", lambda : plc.GetPCUs())
33
34         l_pcu = None
35         if config.pcu:
36                 for pcu in l_plcpcus:
37                         if pcu['hostname'] == config.pcu  or pcu['ip'] == config.pcu:
38                                 l_pcus = [pcu['pcu_id']]
39                 if not l_pcu:
40                         print "ERROR: could not find pcu %s" % config.pcu
41                         sys.exit(1)
42         else:
43                 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
44         
45         checkAndRecordState(l_pcus, l_plcpcus)
46
47 def checkAndRecordState(l_pcus, l_plcpcus):
48         global externalState
49         global count
50         global_round = externalState['round']
51
52         for pcuname in l_pcus:
53                 if pcuname not in externalState['nodes']:
54                         externalState['nodes'][pcuname] = {'round': 0, 'values': []}
55
56                 pcu_round   = externalState['nodes'][pcuname]['round']
57                 if pcu_round < global_round:
58                         # do work
59                         values = collectStatusAndState(pcuname, l_plcpcus)
60                         global_round = externalState['round']
61                         externalState['nodes'][pcuname]['values'] = values
62                         externalState['nodes'][pcuname]['round'] = global_round
63                 else:
64                         count += 1
65
66                 if count % 20 == 0:
67                         database.dbDump(config.dbname, externalState)
68
69         database.dbDump(config.dbname, externalState)
70
71 fbpcu = database.dbLoad('findbadpcus')
72 hn2lb = database.dbLoad("plcdb_hn2lb")
73
74 def get(fb, path):
75         indexes = path.split("/")
76         values = fb
77         for index in indexes:
78                 if index in values:
79                         values = values[index]
80                 else:
81                         return None
82         return values
83
84 def collectStatusAndState(pcuname, l_plcpcus):
85         global count
86
87         d_pcu = None
88         for pcu in l_plcpcus:
89                 if pcu['pcu_id'] == pcuname:
90                         d_pcu = pcu
91                         break
92         if not d_pcu:
93                 return None
94
95         pf = PersistFlags(pcuname, 1, db='pcu_persistflags')
96
97         if not pf.checkattr('last_changed'):
98                 pf.last_changed = time.time()
99                 
100         pf.last_checked = time.time()
101
102         if not pf.checkattr('valid'):
103                 pf.valid = "unknown"
104                 pf.last_valid = 0
105
106         if not pf.checkattr('status'):
107                 pf.status = "unknown"
108
109         state_path     = "nodes/id_" + str(pcuname) + "/values/reboot"
110         bootstate_path = "nodes/id_" + str(pcuname) + "/values/plcpcu/boot_state"
111
112         current_state = get(fbpcu, state_path)
113         if current_state == 0:
114                 if pf.status != "good": pf.last_changed = time.time()
115                 pf.status = "good"
116         elif current_state == 'NetDown':
117                 if pf.status != "netdown": pf.last_changed = time.time()
118                 pf.status = "netdown"
119         elif current_state == 'Not_Run':
120                 if pf.status != "badconfig": pf.last_changed = time.time()
121                 pf.status = "badconfig"
122         else:
123                 if pf.status != "error": pf.last_changed = time.time()
124                 pf.status = "error"
125
126         count += 1
127         print "%d %35s %s since(%s)" % (count, pcu_name(d_pcu), pf.status, diff_time(pf.last_changed))
128         # updated by other modules
129         #pf.enabled = 
130         #pf.suspended = 
131
132         pf.save()
133
134         return True
135
136 if __name__ == '__main__':
137         from config import config
138         from optparse import OptionParser
139         parser = OptionParser()
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 = config(parser)
152         config.parse_args()
153
154         try:
155                 main(config)
156         except Exception, err:
157                 import traceback
158                 print traceback.print_exc()
159                 print "Exception: %s" % err
160                 print "Saving data... exitting."
161                 database.dbDump(config.dbname, externalState)
162                 sys.exit(0)