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']
44 node.plc_nodeid = rec.plc_node_stats['node_id']
46 boot_state = "unknown"
49 if boot_state == 'disable': boot_state = 'disabled'
50 if boot_state == 'diag' or boot_state == 'diagnose': boot_state = 'safeboot'
52 if len(rec.plc_node_stats['pcu_ids']) > 0:
58 # NOTE: 'DOWN' and 'DEBUG' are temporary states, so only need
59 # 'translations' into the node.status state
60 # 'BOOT' is a permanent state, but we want it to have a bit of
61 # hysteresis (less than 0.5 days)
63 #################################################################
64 # "Initialize" the findbad states into nodebad status if they are not already set
66 if node_state == 'DOWN':
67 if boot_state == 'disabled' and changed_lessthan(node.last_changed, 60) and \
68 node.status != 'disabled':
69 # NOTE: if changed less than 2 months, then we can allow this.
70 # otherwise, apply 'down' status after greater than 2 months (below).
72 print "changed status from %s to %s" % (node.status, boot_state)
73 node.status = boot_state
74 node.last_changed = datetime.now()
76 if node.status not in ['offline', 'down', 'disabled']:
77 print "changed status from %s to offline" % node.status
78 node.status = 'offline'
79 node.last_changed = datetime.now()
82 #if node_state == 'DOWN' and node.status not in ['offline', 'down', 'disabled']:
83 # if boot_state != 'disabled':
84 # print "changed status from %s to offline" % node.status
85 # node.status = 'offline'
86 # node.last_changed = datetime.now()
88 # print "changed status from %s to %s" % (node.status, boot_state)
89 # node.status = boot_state
90 # node.last_changed = datetime.now()
92 if node_state == 'DEBUG' and node.status != 'monitordebug' and \
93 node.status != 'disabled' and \
94 node.status != 'safeboot':
95 if boot_state != 'disabled' and boot_state != 'safeboot':
97 print "changed status from %s to monitordebug" % (node.status)
98 node.status = "monitordebug"
99 node.last_changed = datetime.now()
101 print "changed status from %s to %s" % (node.status, boot_state)
102 node.status = boot_state
103 node.last_changed = datetime.now()
105 if node_state == 'BOOT' and node.status != 'online' and node.status != 'good':
106 print "changed status from %s to online" % node.status
107 node.status = 'online'
108 node.last_changed = datetime.now()
110 #################################################################
111 # Switch temporary hystersis states into their 'firm' states.
112 # online -> good after half a day
113 # offline -> down after two days
114 # monitordebug -> down after 30 days
115 # safeboot -> monitordebug after 60 days
116 # disabled -> down after 60 days
118 if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
119 print "changed status from %s to good" % node.status
121 # NOTE: do not reset last_changed, or you lose how long it's been up.
123 if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
124 print "changed status from %s to down" % node.status
126 # NOTE: do not reset last_changed, or you lose how long it's been down.
128 if node.status == 'monitordebug' and changed_greaterthan(node.last_changed, 30):
129 print "changed status from %s to down" % node.status
131 # NOTE: do not reset last_changed, or you lose how long it's been down.
133 if node.status == 'safeboot' and changed_greaterthan(node.last_changed, 60):
134 print "changed status from %s to down" % node.status
135 # NOTE: change an admin mode back into monitordebug after two months.
136 node.status = 'monitordebug'
137 node.last_changed = datetime.now()
139 # extreme cases of offline nodes
140 if ( boot_state == 'disabled' or last_contact == None ) and \
141 changed_greaterthan(node.last_changed, 2*30) and \
142 node.status != 'down':
143 print "changed status from %s to down" % node.status
145 node.last_changed = datetime.now()
147 def checkAndRecordState(l_nodes, l_plcnodes):
150 for nodename in l_nodes:
152 nodehist = HistoryNodeRecord.findby_or_create(hostname=nodename,
153 if_new_set={'status' : 'offline',
154 'last_changed' : datetime.now()})
155 nodehist.last_checked = datetime.now()
158 # Find the most recent record
159 noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
161 print "COULD NOT FIND %s" % nodename
164 print traceback.print_exc()
168 print "none object for %s"% nodename
171 check_node_state(noderec, nodehist)
174 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
176 # NOTE: this commits all pending operations to the DB. Do not remove.
181 if __name__ == '__main__':
182 from monitor import parser as parsermodule
183 parser = parsermodule.getParser(['nodesets'])
184 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, cachenodes=False)
185 parser = parsermodule.getParser(['defaults'], parser)
186 config = parsermodule.parse_args(parser)
190 except Exception, err:
192 print traceback.print_exc()
193 print "Exception: %s" % err