7 from datetime import datetime,timedelta
9 from nodequery import verify,query_to_dict,node_select
11 from monitor.common import *
13 from monitor import config
14 from monitor.wrapper import plc,plccache
15 from monitor.const import MINUP
16 from monitor.database.info.model import FindbadNodeRecord, HistoryNodeRecord
17 from monitor.database.dborm import mon_session as session
19 from monitor.model import *
21 api = plc.getAuthAPI()
30 l_plcnodes = plccache.l_nodes
31 l_nodes = get_nodeset(config)
33 checkAndRecordState(l_nodes, l_plcnodes)
37 def check_node_state(rec, node):
39 node_state = rec.observed_status
40 if rec.plc_node_stats:
41 boot_state = rec.plc_node_stats['boot_state']
42 last_contact = rec.plc_node_stats['last_contact']
44 boot_state = "unknown"
47 if node_state == 'DOWN' and ( node.status == 'online' or node.status == 'good' ):
48 print "changed status from %s to offline" % node.status
49 node.status = 'offline'
50 node.last_changed = datetime.now()
52 if node_state == 'BOOT' and changed_lessthan(node.last_changed, 0.5) and node.status != 'online':
53 print "changed status from %s to online" % node.status
54 node.status = 'online'
55 node.last_changed = datetime.now()
57 if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
58 #send thank you notice, or on-line notice.
59 print "changed status from %s to good" % node.status
61 # NOTE: do not reset last_changed, or you lose how long it's been up.
63 #if node.status == 'offline' and changed_greaterthan(node.last_changed, 1): # and pcu.status == 'good'
66 #if node.status == 'offline' and changed_greaterthan(node.last_changed, 1.5): # and node.has_pcu
67 # # send PCU failure message
70 if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
71 print "changed status from %s to down" % node.status
72 # send down node notice
74 node.last_changed = datetime.now()
76 if ( boot_state == 'disabled' or last_contact == None ) and \
77 changed_greaterthan(node.last_changed, 2*30) and \
78 node.status != 'down':
79 print "changed status from %s to down" % node.status
81 node.last_changed = datetime.now()
83 def checkAndRecordState(l_nodes, l_plcnodes):
86 for nodename in l_nodes:
88 nodehist = HistoryNodeRecord.findby_or_create(hostname=nodename,
89 if_new_set={'status' : 'offline',
90 'last_changed' : datetime.now()})
91 nodehist.last_checked = datetime.now()
94 # Find the most recent record
95 noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==nodename).order_by(FindbadNodeRecord.date_checked.desc()).first()
97 print "COULD NOT FIND %s" % nodename
99 print traceback.print_exc()
103 print "none object for %s"% nodename
106 check_node_state(noderec, nodehist)
109 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
111 # NOTE: this commits all pending operations to the DB. Do not remove, or
112 # replace with another operations that also commits all pending ops, such
113 # as session.commit() or flush() or something
115 print HistoryNodeRecord.query.count()
119 if __name__ == '__main__':
120 from monitor import parser as parsermodule
121 parser = parsermodule.getParser(['nodesets'])
122 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, cachenodes=False)
123 parser = parsermodule.getParser(['defaults'], parser)
124 config = parsermodule.parse_args(parser)
128 except Exception, err:
130 print traceback.print_exc()
131 print "Exception: %s" % err