7 from datetime import datetime,timedelta
9 from monitor import database
10 from monitor.pcu import reboot
11 from monitor import parser as parsermodule
12 from monitor import config
13 from monitor.database import HistoryPCURecord, FindbadPCURecord
14 from monitor.wrapper import plc,plccache
15 from monitor.const import MINUP
17 from nodecommon import *
18 from nodequery import verify,query_to_dict,node_select
19 from unified_model import *
21 api = plc.getAuthAPI()
25 #l_plcpcus = database.if_cached_else_refresh(1, 1, "pculist", lambda : plc.GetPCUs())
26 l_plcpcus = plccache.l_pcus
31 if ( pcu['hostname'] is not None and config.pcu in pcu['hostname'] ) or \
32 ( pcu['ip'] is not None and config.pcu in pcu['ip'] ):
33 l_pcus = [pcu['pcu_id']]
35 print "ERROR: could not find pcu %s" % config.pcu
38 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
40 checkAndRecordState(l_pcus, l_plcpcus)
42 hn2lb = plccache.plcdb_hn2lb
44 def checkAndRecordState(l_pcus, l_plcpcus):
46 for pcuname in l_pcus:
50 if pcu['pcu_id'] == pcuname:
56 pf = HistoryPCURecord.findby_or_create(plc_pcuid=d_pcu['pcu_id'])
57 pf.last_checked = datetime.now()
60 # Find the most recent record
61 pcurec = FindbadPCURecord.query.filter(FindbadPCURecord.plc_pcuid==pcuname).order_by(FindbadPCURecord.date_checked.desc()).first()
62 print "NODEREC: ", pcurec.date_checked
64 print "COULD NOT FIND FB record for %s" % reboot.pcu_name(pcu)
66 print traceback.print_exc()
67 # don't have the info to create a new entry right now, so continue.
70 pcu_state = pcurec.reboot_trial_status
71 current_state = pcu_state
73 if current_state == 0 or current_state == "0":
74 if pf.status != "good":
75 pf.last_changed = datetime.now()
77 elif current_state == 'NetDown':
78 if pf.status != "netdown":
79 pf.last_changed = datetime.now()
81 elif current_state == 'Not_Run':
82 if pf.status != "badconfig":
83 pf.last_changed = datetime.now()
84 pf.status = "badconfig"
86 if pf.status != "error":
87 pf.last_changed = datetime.now()
91 print "%d %35s %s since(%s)" % (count, reboot.pcu_name(d_pcu), pf.status, diff_time(time.mktime(pf.last_changed.timetuple())))
93 # NOTE: this commits all pending operations to the DB. Do not remove, or
94 # replace with another operations that also commits all pending ops, such
95 # as session.commit() or flush() or something
96 print HistoryPCURecord.query.count()
100 if __name__ == '__main__':
101 parser = parsermodule.getParser()
102 parser.set_defaults(filename=None, pcu=None, pcuselect=False, pcugroup=None, cachepcus=False)
103 parser.add_option("", "--pcu", dest="pcu", metavar="hostname",
104 help="Provide a single pcu to operate on")
105 parser.add_option("", "--pculist", dest="pculist", metavar="file.list",
106 help="Provide a list of files to operate on")
108 config = parsermodule.parse_args(parser)
112 except Exception, err:
114 print traceback.print_exc()
115 print "Exception: %s" % err