8 from datetime import datetime,timedelta
10 from monitor import database
11 from monitor import reboot
12 from monitor import parser as parsermodule
13 from monitor import config
14 from monitor.database.info.model import HistoryPCURecord, FindbadPCURecord
15 from monitor.database.dborm import mon_session as session
16 from monitor.wrapper import plc,plccache
17 from monitor.const import MINUP
19 from monitor.common import *
20 from monitor.query import verify,query_to_dict,node_select
21 from monitor.model import *
23 api = plc.getAuthAPI()
30 l_plcpcus = plccache.l_pcus
33 if config.site is not None:
34 site = plccache.GetSitesByName([config.site])
35 l_nodes = plccache.GetNodesByIds(site[0]['node_ids'])
38 pcus += node['pcu_ids']
40 l_pcus = [pcu for pcu in sets.Set(pcus)]
43 node = plccache.GetNodeByName(config.node)
44 pcus = node['pcu_ids']
46 l_pcus = [pcu for pcu in sets.Set(pcus)]
50 if ( pcu['hostname'] is not None and config.pcu in pcu['hostname'] ) or \
51 ( pcu['ip'] is not None and config.pcu in pcu['ip'] ):
52 l_pcus = [pcu['pcu_id']]
54 print "ERROR: could not find pcu %s" % config.pcu
57 l_pcus = [pcu['pcu_id'] for pcu in l_plcpcus]
59 checkAndRecordState(l_pcus, l_plcpcus)
61 hn2lb = plccache.plcdb_hn2lb
63 def check_pcu_state(rec, pcu):
65 pcu_state = rec.reboot_trial_status
66 good_list = [0, "0", "Test: No error"]
69 if pcu_state not in good_list and pcu.status not in ['offline', 'down']:
70 print "changed status from %s to offline" % pcu.status
71 pcu.status = 'offline'
72 pcu.last_changed = datetime.now()
75 if pcu_state in good_list and pcu.status not in [ 'online', 'good' ]:
76 print "changed status from %s to online" % pcu.status
78 pcu.last_changed = datetime.now()
82 if pcu.status == 'online' and changed_greaterthan(pcu.last_changed, 0.5):
83 #send thank you notice, or on-line notice.
84 print "changed status from %s to good" % pcu.status
86 # NOTE: do not reset last_changed, or you lose how long it's been up.
88 if pcu.status == 'offline' and changed_greaterthan(pcu.last_changed, 2):
89 # send down pcu notice
90 print "changed status from %s to down" % pcu.status
93 # if pcu.status in [ 'offline', 'down' ] and changed_greaterthan(pcu.last_changed, 2*30):
94 # print "changed status from %s to down" % pcu.status
96 # pcu.last_changed = datetime.now()
98 def checkAndRecordState(l_pcus, l_plcpcus):
100 for pcuname in l_pcus:
103 for pcu in l_plcpcus:
104 if pcu['pcu_id'] == pcuname:
110 pcuhist = HistoryPCURecord.findby_or_create(plc_pcuid=d_pcu['pcu_id'],
111 if_new_set={'status' : 'offline',
112 'last_changed' : datetime.now()})
113 pcuhist.last_checked = datetime.now()
116 # Find the most recent record
117 pcurec = FindbadPCURecord.query.filter(FindbadPCURecord.plc_pcuid==pcuname).first()
119 print "COULD NOT FIND FB record for %s" % reboot.pcu_name(d_pcu)
122 print traceback.print_exc()
123 # don't have the info to create a new entry right now, so continue.
127 print "none object for pcu %s"% reboot.pcu_name(d_pcu)
130 check_pcu_state(pcurec, pcuhist)
133 print "%d %35s %s since(%s)" % (count, reboot.pcu_name(d_pcu), pcuhist.status, diff_time(time.mktime(pcuhist.last_changed.timetuple())))
135 # NOTE: this commits all pending operations to the DB. Do not remove, or
136 # replace with another operations that also commits all pending ops, such
137 # as session.commit() or flush() or something
139 print HistoryPCURecord.query.count()
143 if __name__ == '__main__':
144 parser = parsermodule.getParser()
145 parser.set_defaults(filename=None, pcu=None, node=None, site=None, pcuselect=False, pcugroup=None, cachepcus=False)
146 parser.add_option("", "--pcu", dest="pcu", metavar="hostname",
147 help="Provide a single pcu to operate on")
148 parser.add_option("", "--site", dest="site", metavar="sitename",
149 help="Provide a single sitename to operate on")
150 parser.add_option("", "--node", dest="node", metavar="nodename",
151 help="Provide a single node to operate on")
152 parser.add_option("", "--pculist", dest="pculist", metavar="file.list",
153 help="Provide a list of files to operate on")
155 config = parsermodule.parse_args(parser)
159 except Exception, err:
161 traceback.print_exc()
162 print "Exception: %s" % err