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' and ( node.status != 'offline' and node.status != 'down' ) and boot_state != 'disabled' :
67 print "changed status from %s to offline" % node.status
68 node.status = 'offline'
69 node.last_changed = datetime.now()
71 if node_state == 'DEBUG' and node.status != 'monitordebug' and \
72 node.status != 'disabled' and \
73 node.status != 'safeboot':
74 if boot_state != 'disabled' and boot_state != 'safeboot':
76 print "changed status from %s to monitordebug" % (node.status)
77 node.status = "monitordebug"
78 node.last_changed = datetime.now()
80 print "changed status from %s to %s" % (node.status, boot_state)
81 node.status = boot_state
82 node.last_changed = datetime.now()
84 if node_state == 'BOOT' and node.status != 'online' and node.status != 'good':
85 print "changed status from %s to online" % node.status
86 node.status = 'online'
87 node.last_changed = datetime.now()
89 #################################################################
90 # Switch temporary hystersis states into their 'firm' states.
91 # online -> good after half a day
92 # offline -> down after two days
93 # monitordebug -> down after 30 days
94 # safeboot -> monitordebug after 60 days
95 # disabled -> down after 60 days
97 if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
98 print "changed status from %s to good" % node.status
100 # NOTE: do not reset last_changed, or you lose how long it's been up.
102 if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
103 print "changed status from %s to down" % node.status
105 # NOTE: do not reset last_changed, or you lose how long it's been down.
107 if node.status == 'monitordebug' and changed_greaterthan(node.last_changed, 30):
108 print "changed status from %s to down" % node.status
110 # NOTE: do not reset last_changed, or you lose how long it's been down.
112 if node.status == 'safeboot' and changed_greaterthan(node.last_changed, 60):
113 print "changed status from %s to down" % node.status
114 # NOTE: change an admin mode back into monitordebug after two months.
115 node.status = 'monitordebug'
116 node.last_changed = datetime.now()
118 # extreme cases of offline nodes
119 if ( boot_state == 'disabled' or last_contact == None ) and \
120 changed_greaterthan(node.last_changed, 2*30) and \
121 node.status != 'down':
122 print "changed status from %s to down" % node.status
124 node.last_changed = datetime.now()
126 def checkAndRecordState(l_nodes, l_plcnodes):
129 for nodename in l_nodes:
131 nodehist = HistoryNodeRecord.findby_or_create(hostname=nodename,
132 if_new_set={'status' : 'offline',
133 'last_changed' : datetime.now()})
134 nodehist.last_checked = datetime.now()
137 # Find the most recent record
138 noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
140 print "COULD NOT FIND %s" % nodename
143 print traceback.print_exc()
147 print "none object for %s"% nodename
150 check_node_state(noderec, nodehist)
153 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
155 # NOTE: this commits all pending operations to the DB. Do not remove.
160 if __name__ == '__main__':
161 from monitor import parser as parsermodule
162 parser = parsermodule.getParser(['nodesets'])
163 parser.set_defaults(filename=None, node=None, nodeselect=False, nodegroup=None, cachenodes=False)
164 parser = parsermodule.getParser(['defaults'], parser)
165 config = parsermodule.parse_args(parser)
169 except Exception, err:
171 print traceback.print_exc()
172 print "Exception: %s" % err