8 from reboot import pcu_name
14 from nodequery import verify,query_to_dict,node_select
18 api = plc.PLC(auth.auth, auth.plc)
19 from unified_model import *
20 from monitor_policy import MINUP
23 externalState = {'round': round, 'nodes': {}}
28 externalState = database.if_cached_else(1, config.dbname, lambda : externalState)
30 # update global round number to force refreshes across all pcus
31 externalState['round'] += 1
33 l_plcpcus = database.if_cached_else_refresh(1, 1, "pculist", lambda : plc.GetPCUs())
38 if pcu['hostname'] == config.pcu or pcu['ip'] == config.pcu:
39 l_pcus = [pcu['pcu_id']]
41 print "ERROR: could not find pcu %s" % config.pcu
44 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
46 checkAndRecordState(l_pcus, l_plcpcus)
48 def checkAndRecordState(l_pcus, l_plcpcus):
51 global_round = externalState['round']
53 for pcuname in l_pcus:
54 if pcuname not in externalState['nodes']:
55 externalState['nodes'][pcuname] = {'round': 0, 'values': []}
57 pcu_round = externalState['nodes'][pcuname]['round']
58 if pcu_round < global_round:
60 values = collectStatusAndState(pcuname, l_plcpcus)
61 global_round = externalState['round']
62 externalState['nodes'][pcuname]['values'] = values
63 externalState['nodes'][pcuname]['round'] = global_round
68 database.dbDump(config.dbname, externalState)
70 database.dbDump(config.dbname, externalState)
72 fbpcu = database.dbLoad('findbadpcus')
73 hn2lb = database.dbLoad("plcdb_hn2lb")
76 indexes = path.split("/")
80 values = values[index]
85 def collectStatusAndState(pcuname, l_plcpcus):
90 if pcu['pcu_id'] == pcuname:
96 pf = PersistFlags(pcuname, 1, db='pcu_persistflags')
98 if not pf.checkattr('last_changed'):
99 pf.last_changed = time.time()
101 pf.last_checked = time.time()
103 if not pf.checkattr('valid'):
107 if not pf.checkattr('status'):
108 pf.status = "unknown"
110 state_path = "nodes/id_" + str(pcuname) + "/values/reboot"
111 bootstate_path = "nodes/id_" + str(pcuname) + "/values/plcpcu/boot_state"
113 current_state = get(fbpcu, state_path)
114 if current_state == 0:
115 if pf.status != "good": pf.last_changed = time.time()
117 elif current_state == 'NetDown':
118 if pf.status != "netdown": pf.last_changed = time.time()
119 pf.status = "netdown"
120 elif current_state == 'Not_Run':
121 if pf.status != "badconfig": pf.last_changed = time.time()
122 pf.status = "badconfig"
124 if pf.status != "error": pf.last_changed = time.time()
128 print "%d %35s %s since(%s)" % (count, pcu_name(d_pcu), pf.status, diff_time(pf.last_changed))
129 # updated by other modules
137 if __name__ == '__main__':
138 from config import config
139 from optparse import OptionParser
140 parser = OptionParser()
141 parser.set_defaults(filename=None, pcu=None, pcuselect=False, pcugroup=None,
142 increment=False, dbname="pcubad", cachepcus=False)
143 parser.add_option("", "--pcu", dest="pcu", metavar="hostname",
144 help="Provide a single pcu to operate on")
145 parser.add_option("", "--pculist", dest="pculist", metavar="file.list",
146 help="Provide a list of files to operate on")
148 parser.add_option("", "--dbname", dest="dbname", metavar="FILE",
149 help="Specify the name of the database to which the information is saved")
150 parser.add_option("-i", "--increment", action="store_true", dest="increment",
151 help="Increment round number to force refresh or retry")
152 config = config(parser)
157 except Exception, err:
159 print traceback.print_exc()
160 print "Exception: %s" % err
161 print "Saving data... exitting."
162 database.dbDump(config.dbname, externalState)