c3aae39619d18335e985f1d62f2da2584d053ce5
[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                 print rec.plc_node_stats
42                 boot_state = rec.plc_node_stats['boot_state']
43                 last_contact = rec.plc_node_stats['last_contact']
44         else:
45                 boot_state = "unknown"
46                 last_contact = None
47
48         if boot_state == 'disable': boot_state = 'disabled'
49         if boot_state == 'diag':        boot_state = 'diagnose'
50
51         if len(rec.plc_node_stats['pcu_ids']) > 0:
52                 node.haspcu = True
53         else:
54                 node.haspcu = False
55
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)
60
61         #################################################################
62         # "Initialize" the findbad states into nodebad status if they are not already set
63
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()
68
69         if node_state == 'DEBUG' and node.status != 'monitordebug' and \
70                                                                  node.status != 'disabled' and \
71                                                                  node.status != 'diagnose':
72                 if boot_state != 'disabled' and boot_state != 'diagnose':
73
74                         print "changed status from %s to monitordebug" % (node.status)
75                         node.status = "monitordebug"
76                         node.last_changed = datetime.now()
77                 else:
78                         print "changed status from %s to %s" % (node.status, boot_state)
79                         node.status = boot_state
80                         node.last_changed = datetime.now()
81
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()
86
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         #         diagnose -> monitordebug after 60 days
93         #         disabled -> down              after 60 days
94
95         if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
96                 print "changed status from %s to good" % node.status
97                 node.status = 'good'
98                 # NOTE: do not reset last_changed, or you lose how long it's been up.
99
100         if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
101                 print "changed status from %s to down" % node.status
102                 node.status = 'down'
103                 # NOTE: do not reset last_changed, or you lose how long it's been down.
104
105         if node.status == 'monitordebug' and changed_greaterthan(node.last_changed, 30):
106                 print "changed status from %s to down" % node.status
107                 node.status = 'down'
108                 # NOTE: do not reset last_changed, or you lose how long it's been down.
109
110         if node.status == 'diagnose' 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()
115
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
121                 node.status = 'down'
122                 node.last_changed = datetime.now()
123
124 def checkAndRecordState(l_nodes, l_plcnodes):
125         global count
126
127         for nodename in l_nodes:
128
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()
133
134                 try:
135                         # Find the most recent record
136                         noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
137                 except:
138                         print "COULD NOT FIND %s" % nodename
139                         import traceback
140                         email_exception()
141                         print traceback.print_exc()
142                         continue
143
144                 if not noderec:
145                         print "none object for %s"% nodename
146                         continue
147
148                 check_node_state(noderec, nodehist)
149
150                 count += 1
151                 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
152
153         # NOTE: this commits all pending operations to the DB.  Do not remove. 
154         session.flush()
155
156         return True
157
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)
164
165         try:
166                 main2(config)
167         except Exception, err:
168                 import traceback
169                 print traceback.print_exc()
170                 print "Exception: %s" % err
171                 sys.exit(0)