8 from reboot import pcu_name
14 from nodequery import verify,query_to_dict,node_select
15 import parser as parsermodule
16 from nodecommon import *
19 api = plc.getAuthAPI()
20 from unified_model import *
21 from const import MINUP
24 externalState = {'round': round, 'nodes': {}}
29 externalState = database.if_cached_else(1, config.dbname, lambda : externalState)
31 # update global round number to force refreshes across all pcus
32 externalState['round'] += 1
34 l_plcpcus = database.if_cached_else_refresh(1, 1, "pculist", lambda : plc.GetPCUs())
39 if pcu['hostname'] == config.pcu or pcu['ip'] == config.pcu:
40 l_pcus = [pcu['pcu_id']]
42 print "ERROR: could not find pcu %s" % config.pcu
45 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
47 checkAndRecordState(l_pcus, l_plcpcus)
49 def checkAndRecordState(l_pcus, l_plcpcus):
52 global_round = externalState['round']
54 for pcuname in l_pcus:
55 if pcuname not in externalState['nodes']:
56 externalState['nodes'][pcuname] = {'round': 0, 'values': []}
58 pcu_round = externalState['nodes'][pcuname]['round']
59 if pcu_round < global_round:
61 values = collectStatusAndState(pcuname, l_plcpcus)
62 global_round = externalState['round']
63 externalState['nodes'][pcuname]['values'] = values
64 externalState['nodes'][pcuname]['round'] = global_round
69 database.dbDump(config.dbname, externalState)
71 database.dbDump(config.dbname, externalState)
73 fbpcu = database.dbLoad('findbadpcus')
74 hn2lb = database.dbLoad("plcdb_hn2lb")
77 indexes = path.split("/")
81 values = values[index]
86 def collectStatusAndState(pcuname, l_plcpcus):
91 if pcu['pcu_id'] == pcuname:
97 pf = PersistFlags(pcuname, 1, db='pcu_persistflags')
99 if not pf.checkattr('last_changed'):
100 pf.last_changed = time.time()
102 pf.last_checked = time.time()
104 if not pf.checkattr('valid'):
108 if not pf.checkattr('status'):
109 pf.status = "unknown"
111 state_path = "nodes/id_" + str(pcuname) + "/values/reboot"
112 bootstate_path = "nodes/id_" + str(pcuname) + "/values/plcpcu/boot_state"
114 current_state = get(fbpcu, state_path)
115 if current_state == 0:
116 if pf.status != "good": pf.last_changed = time.time()
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"
125 if pf.status != "error": pf.last_changed = time.time()
129 print "%d %35s %s since(%s)" % (count, pcu_name(d_pcu), pf.status, diff_time(pf.last_changed))
130 # updated by other modules
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")
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)
155 except Exception, err:
157 print traceback.print_exc()
158 from nodecommon import email_exception
160 print "Exception: %s" % err
161 print "Saving data... exitting."
162 database.dbDump(config.dbname, externalState)