Tagging module Monitor - Monitor-2.0-6
[monitor.git] / nodebad.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7 from datetime import datetime,timedelta
8
9 from nodequery import verify,query_to_dict,node_select
10
11 from monitor.common import *
12
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
18
19 from monitor.model import *
20
21 api = plc.getAuthAPI()
22
23 round = 1
24 count = 0
25 def main():
26         main2(config)
27
28 def main2(config):
29
30         l_plcnodes = plccache.l_nodes
31         l_nodes = get_nodeset(config)
32         
33         checkAndRecordState(l_nodes, l_plcnodes)
34
35 # Node states:
36
37 def check_node_state(rec, node):
38
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']
43         else:
44                 boot_state = "unknown"
45                 last_contact = None
46
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()
51
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()
56
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
60                 node.status = 'good'
61                 # NOTE: do not reset last_changed, or you lose how long it's been up.
62
63         #if node.status == 'offline' and changed_greaterthan(node.last_changed, 1): #  and pcu.status == 'good' 
64         #       # attempt reboots
65         #       pass
66         #if node.status == 'offline' and changed_greaterthan(node.last_changed, 1.5): # and node.has_pcu
67         #       # send PCU failure message
68         #       pass
69
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
73                 node.status = 'down'
74                 node.last_changed = datetime.now()
75
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
80                 node.status = 'down'
81                 node.last_changed = datetime.now()
82
83 def checkAndRecordState(l_nodes, l_plcnodes):
84         global count
85
86         for nodename in l_nodes:
87
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()
92
93                 try:
94                         # Find the most recent record
95                         noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==nodename).order_by(FindbadNodeRecord.date_checked.desc()).first()
96                 except:
97                         print "COULD NOT FIND %s" % nodename
98                         import traceback
99                         print traceback.print_exc()
100                         continue
101
102                 if not noderec:
103                         print "none object for %s"% nodename
104                         continue
105
106                 check_node_state(noderec, nodehist)
107
108                 count += 1
109                 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
110
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
114         session.flush()
115         print HistoryNodeRecord.query.count()
116
117         return True
118
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)
125
126         try:
127                 main2(config)
128         except Exception, err:
129                 import traceback
130                 print traceback.print_exc()
131                 print "Exception: %s" % err
132                 sys.exit(0)