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 print rec.plc_node_stats
42 boot_state = rec.plc_node_stats['boot_state']
43 last_contact = rec.plc_node_stats['last_contact']
45 boot_state = "unknown"
48 if boot_state == 'disable': boot_state = 'disabled'
49 if boot_state == 'diag' or boot_state == 'diagnose': boot_state = 'safeboot'
51 if len(rec.plc_node_stats['pcu_ids']) > 0:
56 # NOTE: 'DOWN' and 'DEBUG' are temporary states, so only need
57 # 'translations' into the node.status state
58 # 'BOOT' is a permanent state, but we want it to have a bit of
59 # hysteresis (less than 0.5 days)
61 #################################################################
62 # "Initialize" the findbad states into nodebad status if they are not already set
64 if node_state == 'DOWN' and ( node.status != 'offline' and node.status != 'down' ) and boot_state != 'disabled' :
65 print "changed status from %s to offline" % node.status
66 node.status = 'offline'
67 node.last_changed = datetime.now()
69 if node_state == 'DEBUG' and node.status != 'monitordebug' and \
70 node.status != 'disabled' and \
71 node.status != 'safeboot':
72 if boot_state != 'disabled' and boot_state != 'safeboot':
74 print "changed status from %s to monitordebug" % (node.status)
75 node.status = "monitordebug"
76 node.last_changed = datetime.now()
78 print "changed status from %s to %s" % (node.status, boot_state)
79 node.status = boot_state
80 node.last_changed = datetime.now()
82 if node_state == 'BOOT' and node.status != 'online' and node.status != 'good':
83 print "changed status from %s to online" % node.status
84 node.status = 'online'
85 node.last_changed = datetime.now()
87 #################################################################
88 # Switch temporary hystersis states into their 'firm' states.
89 # online -> good after half a day
90 # offline -> down after two days
91 # monitordebug -> down after 30 days
92 # safeboot -> monitordebug after 60 days
93 # disabled -> down after 60 days
95 if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
96 print "changed status from %s to good" % node.status
98 # NOTE: do not reset last_changed, or you lose how long it's been up.
100 if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
101 print "changed status from %s to down" % node.status
103 # NOTE: do not reset last_changed, or you lose how long it's been down.
105 if node.status == 'monitordebug' and changed_greaterthan(node.last_changed, 30):
106 print "changed status from %s to down" % node.status
108 # NOTE: do not reset last_changed, or you lose how long it's been down.
110 if node.status == 'safeboot' and changed_greaterthan(node.last_changed, 60):
111 print "changed status from %s to down" % node.status
112 # NOTE: change an admin mode back into monitordebug after two months.
113 node.status = 'monitordebug'
114 node.last_changed = datetime.now()
116 # extreme cases of offline nodes
117 if ( boot_state == 'disabled' or last_contact == None ) and \
118 changed_greaterthan(node.last_changed, 2*30) and \
119 node.status != 'down':
120 print "changed status from %s to down" % node.status
122 node.last_changed = datetime.now()
124 def checkAndRecordState(l_nodes, l_plcnodes):
127 for nodename in l_nodes:
129 nodehist = HistoryNodeRecord.findby_or_create(hostname=nodename,
130 if_new_set={'status' : 'offline',
131 'last_changed' : datetime.now()})
132 nodehist.last_checked = datetime.now()
135 # Find the most recent record
136 noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
138 print "COULD NOT FIND %s" % nodename
141 print traceback.print_exc()
145 print "none object for %s"% nodename
148 check_node_state(noderec, nodehist)
151 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
153 # NOTE: this commits all pending operations to the DB. Do not remove.
158 if __name__ == '__main__':
159 from monitor import parser as parsermodule
160 parser = parsermodule.getParser(['nodesets'])
161 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, cachenodes=False)
162 parser = parsermodule.getParser(['defaults'], parser)
163 config = parsermodule.parse_args(parser)
167 except Exception, err:
169 print traceback.print_exc()
170 print "Exception: %s" % err