added templates for emailTxt
[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                 node.plc_nodeid = rec.plc_node_stats['node_id']
45         else:
46                 boot_state = "unknown"
47                 last_contact = None
48
49         if boot_state == 'disable': boot_state = 'disabled'
50         if boot_state == 'diag' or boot_state == 'diagnose': boot_state = 'safeboot'
51
52         if len(rec.plc_node_stats['pcu_ids']) > 0:
53                 node.haspcu = True
54         else:
55                 node.haspcu = False
56
57
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)
62
63         #################################################################
64         # "Initialize" the findbad states into nodebad status if they are not already set
65
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).
71
72                         print "changed status from %s to %s" % (node.status, boot_state)
73                         node.status = boot_state
74                         node.last_changed = datetime.now()
75
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()
80                         
81
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()
87         #       else:
88         #               print "changed status from %s to %s" % (node.status, boot_state)
89         #               node.status = boot_state
90         #               node.last_changed = datetime.now()
91
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':
96
97                         print "changed status from %s to monitordebug" % (node.status)
98                         node.status = "monitordebug"
99                         node.last_changed = datetime.now()
100                 else:
101                         print "changed status from %s to %s" % (node.status, boot_state)
102                         node.status = boot_state
103                         node.last_changed = datetime.now()
104
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()
109
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
117
118         if node.status == 'online' and changed_greaterthan(node.last_changed, 0.5):
119                 print "changed status from %s to good" % node.status
120                 node.status = 'good'
121                 # NOTE: do not reset last_changed, or you lose how long it's been up.
122
123         if node.status == 'offline' and changed_greaterthan(node.last_changed, 2):
124                 print "changed status from %s to down" % node.status
125                 node.status = 'down'
126                 # NOTE: do not reset last_changed, or you lose how long it's been down.
127
128         if node.status == 'monitordebug' and changed_greaterthan(node.last_changed, 30):
129                 print "changed status from %s to down" % node.status
130                 node.status = 'down'
131                 # NOTE: do not reset last_changed, or you lose how long it's been down.
132
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()
138
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
144                 node.status = 'down'
145                 node.last_changed = datetime.now()
146
147 def checkAndRecordState(l_nodes, l_plcnodes):
148         global count
149
150         for nodename in l_nodes:
151
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()
156
157                 try:
158                         # Find the most recent record
159                         noderec = FindbadNodeRecord.get_latest_by(hostname=nodename)
160                 except:
161                         print "COULD NOT FIND %s" % nodename
162                         import traceback
163                         email_exception()
164                         print traceback.print_exc()
165                         continue
166
167                 if not noderec:
168                         print "none object for %s"% nodename
169                         continue
170
171                 check_node_state(noderec, nodehist)
172
173                 count += 1
174                 print "%d %35s %s since(%s)" % (count, nodename, nodehist.status, diff_time(time.mktime(nodehist.last_changed.timetuple())))
175
176         # NOTE: this commits all pending operations to the DB.  Do not remove. 
177         session.flush()
178
179         return True
180
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)
187
188         try:
189                 main2(config)
190         except Exception, err:
191                 import traceback
192                 print traceback.print_exc()
193                 print "Exception: %s" % err
194                 sys.exit(0)