acd5007a901ce7bbcc7963b18918bdc9195f48a0
[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                 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']
46         else:
47                 boot_state = "unknown"
48                 last_contact = None
49
50         if boot_state == 'disable': boot_state = 'disabled'
51         if boot_state == 'diag' or boot_state == 'diagnose': boot_state = 'safeboot'
52
53         if len(rec.plc_node_stats['pcu_ids']) > 0:
54                 node.haspcu = True
55         else:
56                 node.haspcu = False
57
58         node.firewall = rec.firewall
59         node.plc_siteid = rec.plc_node_stats['site_id']
60
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
67
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).
73
74                         print "changed status from %s to %s" % (node.status, boot_state)
75                         node.status = boot_state
76                         node.last_changed = datetime.now()
77
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()
82                         
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()
88                 else:
89                         print "changed status from %s to %s" % (node.status, boot_state)
90                         node.status = boot_state
91                         node.last_changed = datetime.now()
92
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()
97
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
105
106         if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
107                 print "changed status from %s to good" % node.status
108                 node.status = 'good'
109                 # NOTE: do not reset last_changed, or you lose how long it's been up.
110
111         if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
112                 print "changed status from %s to down" % node.status
113                 node.status = 'down'
114                 # NOTE: do not reset last_changed, or you lose how long it's been down.
115
116         if node.status == 'failboot' and changed_greaterthan(node.last_changed, 30):
117                 print "changed status from %s to down" % node.status
118                 node.status = 'down'
119                 # NOTE: do not reset last_changed, or you lose how long it's been down.
120
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()
126
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
132                 node.status = 'down'
133                 node.last_changed = datetime.now()
134
135 def checkAndRecordState(l_nodes, l_plcnodes):
136         global count
137
138         for nodename in l_nodes:
139
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()
144
145                 try:
146                         # Find the most recent record
147                         noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
148                 except:
149                         print "COULD NOT FIND %s" % nodename
150                         import traceback
151                         email_exception()
152                         print traceback.print_exc()
153                         continue
154
155                 if not noderec:
156                         print "none object for %s"% nodename
157                         continue
158
159                 check_node_state(noderec, nodehist)
160
161                 count += 1
162                 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
163
164         # NOTE: this commits all pending operations to the DB.  Do not remove. 
165         session.flush()
166
167         return True
168
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)
175
176         try:
177                 main2(config)
178         except Exception, err:
179                 import traceback
180                 print traceback.print_exc()
181                 print "Exception: %s" % err
182                 sys.exit(0)