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 run_level = rec.plc_node_stats['run_level']
44 last_contact = rec.plc_node_stats['last_contact']
45 node.plc_nodeid = rec.plc_node_stats['node_id']
47 boot_state = "unknown"
50 if boot_state == 'disable': boot_state = 'disabled'
51 if boot_state == 'diag' or boot_state == 'diagnose': boot_state = 'safeboot'
53 if len(rec.plc_node_stats['pcu_ids']) > 0:
58 node.firewall = rec.firewall
59 node.plc_siteid = rec.plc_node_stats['site_id']
61 # NOTE: 'DOWN' and 'DEBUG' are temporary states, so only need
62 # 'translations' into the node.status state
63 # 'BOOT' is a permanent state, but we want it to have a bit of
64 # hysteresis (less than 0.5 days)
65 #################################################################
66 # "Initialize" the findbad states into nodebad status if they are not already set
68 if node_state == 'DOWN':
69 if boot_state == 'disabled' and changed_lessthan(node.last_changed, 60) and \
70 node.status != 'disabled':
71 # NOTE: if changed less than 2 months, then we can allow this.
72 # otherwise, apply 'down' status after greater than 2 months (below).
74 print "changed status from %s to %s" % (node.status, boot_state)
75 node.status = boot_state
76 node.last_changed = datetime.now()
78 if node.status not in ['offline', 'down', 'disabled']:
79 print "changed status from %s to offline" % node.status
80 node.status = 'offline'
81 node.last_changed = datetime.now()
83 if node_state == 'DEBUG' and node.status not in ['failboot', 'disabled', 'safeboot']:
84 if boot_state != 'disabled' and boot_state != 'safeboot':
85 print "changed status from %s to failboot" % (node.status)
86 node.status = "failboot"
87 node.last_changed = datetime.now()
89 print "changed status from %s to %s" % (node.status, boot_state)
90 node.status = boot_state
91 node.last_changed = datetime.now()
93 if node_state == 'BOOT' and node.status != 'online' and node.status != 'good':
94 print "changed status from %s to online" % node.status
95 node.status = 'online'
96 node.last_changed = datetime.now()
98 #################################################################
99 # Switch temporary hystersis states into their 'firm' states.
100 # online -> good after half a day
101 # offline -> down after two days
102 # failboot -> down after 30 days
103 # safeboot -> failboot after 60 days
104 # disabled -> down after 60 days
106 if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
107 print "changed status from %s to good" % node.status
109 # NOTE: do not reset last_changed, or you lose how long it's been up.
111 if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
112 print "changed status from %s to down" % node.status
114 # NOTE: do not reset last_changed, or you lose how long it's been down.
116 if node.status == 'failboot' and changed_greaterthan(node.last_changed, 30):
117 print "changed status from %s to down" % node.status
119 # NOTE: do not reset last_changed, or you lose how long it's been down.
121 if node.status == 'safeboot' and changed_greaterthan(node.last_changed, 60):
122 print "changed status from %s to down" % node.status
123 # NOTE: change an admin mode back into failboot after two months.
124 node.status = 'failboot'
125 node.last_changed = datetime.now()
127 # extreme cases of offline nodes
128 if ( boot_state == 'disabled' or last_contact == None ) and \
129 changed_greaterthan(node.last_changed, 2*30) and \
130 node.status != 'down':
131 print "changed status from %s to down" % node.status
133 node.last_changed = datetime.now()
135 def checkAndRecordState(l_nodes, l_plcnodes):
138 for nodename in l_nodes:
140 nodehist = HistoryNodeRecord.findby_or_create(hostname=nodename,
141 if_new_set={'status' : 'offline',
142 'last_changed' : datetime.now()})
143 nodehist.last_checked = datetime.now()
146 # Find the most recent record
147 noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
149 print "COULD NOT FIND %s" % nodename
152 print traceback.print_exc()
156 print "none object for %s"% nodename
159 check_node_state(noderec, nodehist)
162 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
164 # NOTE: this commits all pending operations to the DB. Do not remove.
169 if __name__ == '__main__':
170 from monitor import parser as parsermodule
171 parser = parsermodule.getParser(['nodesets'])
172 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, cachenodes=False)
173 parser = parsermodule.getParser(['defaults'], parser)
174 config = parsermodule.parse_args(parser)
178 except Exception, err:
180 print traceback.print_exc()
181 print "Exception: %s" % err