merge recent v2 changes; migrating to v3
[monitor.git] / monitor / common.py
1
2 import time
3 import struct
4 from monitor import reboot
5 from monitor import util
6 from monitor import database
7 from monitor.wrapper import plc, plccache
8
9 from datetime import datetime, timedelta
10 from monitor.model import Message
11 from monitor.database.info import HistoryNodeRecord
12
13 esc = struct.pack('i', 27)
14 RED     = esc + "[1;31m"
15 GREEN   = esc + "[1;32m"
16 YELLOW  = esc + "[1;33m"
17 BLUE    = esc + "[1;34m"
18 LIGHTBLUE       = esc + "[1;36m"
19 NORMAL  = esc + "[0;39m"
20
21 def red(str):
22         return RED + str + NORMAL
23
24 def yellow(str):
25         return YELLOW + str + NORMAL
26
27 def green(str):
28         return GREEN + str + NORMAL
29
30 def lightblue(str):
31         return LIGHTBLUE + str + NORMAL
32
33 def blue(str):
34         return BLUE + str + NORMAL
35
36 def get_current_state(fbnode):
37         if 'observed_status' in fbnode:
38                 state = fbnode['observed_status']
39         else:
40                 state = "none"
41         l = state.lower()
42         if l == "debug": l = 'dbg '
43         return l
44
45 def color_pcu_state(fbnode):
46
47         if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
48                 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
49                 if values == None:
50                         return fbnode['pcu']
51         else:
52                 if 'pcu' not in fbnode:
53                         return 'NOPCU'
54                 else:
55                         return fbnode['pcu']
56
57         if 'reboot' in values:
58                 rb = values['reboot']
59                 if rb == 0 or rb == "0":
60                         return fbnode['pcu'] + "OK  "
61                         #return fbnode['pcu'] + "OK  "
62                         #return green(fbnode['pcu'])
63                 elif "NetDown" == rb  or "Not_Run" == rb:
64                         return fbnode['pcu'] + "DOWN"
65                         #return yellow(fbnode['pcu'])
66                 else:
67                         return fbnode['pcu'] + "BAD "
68                         #return red(fbnode['pcu'])
69         else:
70                 #return red(fbnode['pcu'])
71                 return fbnode['pcu'] + "BAD "
72
73 def color_boot_state(l):
74         if    l == "dbg": return yellow("debg")
75         elif  l == "dbg ": return yellow("debg")
76         elif  l == "diag": return lightblue(l)
77         elif  l == "disable": return red("dsbl")
78         elif  l == "down": return red(l)
79         elif  l == "boot": return green(l)
80         elif  l == "rins": return blue(l)
81         elif  l == "reinstall": return blue(l)
82         else:
83                 return l
84
85 def diff_time(timestamp, abstime=True):
86         import math
87         now = time.time()
88         if timestamp == None:
89                 return "unknown"
90         if type(timestamp) == type(datetime.now()):
91                 timestamp = time.mktime(timestamp.timetuple())
92         if abstime:
93                 diff = now - timestamp
94         else:
95                 diff = timestamp
96         # return the number of seconds as a difference from current time.
97         t_str = ""
98         if diff < 60: # sec in min.
99                 t = diff / 1
100                 t_str = "%s sec ago" % int(math.ceil(t))
101         elif diff < 60*60: # sec in hour
102                 t = diff / (60)
103                 t_str = "%s min ago" % int(math.ceil(t))
104         elif diff < 60*60*24: # sec in day
105                 t = diff / (60*60)
106                 t_str = "%s hrs ago" % int(math.ceil(t))
107         elif diff < 60*60*24*14: # sec in week
108                 t = diff / (60*60*24)
109                 t_str = "%s days ago" % int(math.ceil(t))
110         elif diff <= 60*60*24*30: # approx sec in month
111                 t = diff / (60*60*24*7)
112                 t_str = "%s wks ago" % int(math.ceil(t))
113         elif diff > 60*60*24*30: # approx sec in month
114                 t = diff / (60*60*24*30)
115                 t_str = "%s mnths ago" % int(t)
116         return t_str
117
118 def getvalue(fb, path):
119     indexes = path.split("/")
120     values = fb
121     for index in indexes:
122         if index in values:
123             values = values[index]
124         else:
125             return None
126     return values
127
128 def nmap_port_status(status):
129         ps = {}
130         l_nmap = status.split()
131         ports = l_nmap[4:]
132
133         continue_probe = False
134         for port in ports:
135                 results = port.split('/')
136                 ps[results[0]] = results[1]
137                 if results[1] == "open":
138                         continue_probe = True
139         return (ps, continue_probe)
140
141
142 def nodegroup_display(node, fbdata, conf=None):
143         node['current'] = get_current_state(fbdata)
144
145         s = fbdata['kernel_version'].split()
146         if len(s) >=3:
147                 node['kernel_version'] = s[2]
148         else:
149                 node['kernel_version'] = fbdata['kernel_version']
150                 
151         if '2.6' not in node['kernel_version']: node['kernel_version'] = ""
152         if conf and not conf.nocolor:
153             node['boot_state']  = color_boot_state(node['boot_state'])
154             node['current']     = color_boot_state(node['current'])
155
156         if type(fbdata['plc_node_stats']['pcu_ids']) == type([]):
157                 node['pcu'] = "PCU"
158         node['lastupdate'] = diff_time(node['last_contact'])
159
160         pf = HistoryNodeRecord.get_by(hostname=node['hostname'])
161         try:
162                 node['lc'] = diff_time(pf.last_changed)
163         except:
164                 node['lc'] = "err"
165
166         ut = fbdata['comon_stats']['uptime']
167         if ut != "null":
168                 ut = diff_time(float(fbdata['comon_stats']['uptime']), False)
169         node['uptime'] = ut
170
171         return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel_version)35.35s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
172
173 def datetime_fromstr(str):
174         if '-' in str:
175                 try:
176                         tup = time.strptime(str, "%Y-%m-%d")
177                 except:
178                         tup = time.strptime(str, "%Y-%m-%d-%H:%M")
179         elif '/' in str:
180                 tup = time.strptime(str, "%m/%d/%Y")
181         else:
182                 tup = time.strptime(str, "%m/%d/%Y")
183         ret = datetime.fromtimestamp(time.mktime(tup))
184         return ret
185
186 def get_nodeset(config):
187         """
188                 Given the config values passed in, return the set of hostnames that it
189                 evaluates to.
190         """
191         api = plc.getAuthAPI()
192         l_nodes = plccache.l_nodes
193
194         if config.nodelist:
195                 f_nodes = util.file.getListFromFile(config.nodelist)
196                 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
197         elif config.node:
198                 f_nodes = [config.node]
199                 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
200         elif config.nodegroup:
201                 ng = api.GetNodeGroups({'name' : config.nodegroup})
202                 l_nodes = api.GetNodes(ng[0]['node_ids'], ['hostname'])
203         elif config.site:
204                 site = api.GetSites(config.site)
205                 l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
206                 
207         l_nodes = [node['hostname'] for node in l_nodes]
208
209         # perform this query after the above options, so that the filter above
210         # does not break.
211         if config.nodeselect:
212                 fbquery = FindbadNodeRecord.get_all_latest()
213                 node_list = [ n.hostname for n in fbquery ]
214                 l_nodes = node_select(config.nodeselect, node_list, None)
215
216         return l_nodes
217
218 def email_exception(content=None):
219     import config
220     from monitor.model import Message
221     import traceback
222     msg=traceback.format_exc()
223     if content:
224         msg = content + "\n" + msg
225     m=Message("exception running monitor", msg, False)
226     m.send([config.cc_email])
227     return
228
229 def changed_lessthan(last_changed, days):
230         if datetime.now() - last_changed <= timedelta(days):
231                 #print "last changed less than %s" % timedelta(days)
232                 return True
233         else:
234                 #print "last changed more than %s" % timedelta(days)
235                 return False
236
237 def changed_greaterthan(last_changed, days):
238         if datetime.now() - last_changed > timedelta(days):
239                 #print "last changed more than %s" % timedelta(days)
240                 return True
241         else:
242                 #print "last changed less than %s" % timedelta(days)
243                 return False
244
245 def found_between(recent_actions, action_type, lower, upper):
246         return found_before(recent_actions, action_type, upper) and found_within(recent_actions, action_type, lower)
247
248 def found_before(recent_actions, action_type, within):
249         for action in recent_actions:
250                 if action_type == action.action_type and \
251                                 action.date_created < (datetime.now() - timedelta(within)):
252                         return True
253         return False
254         
255 def found_within(recent_actions, action_type, within):
256         for action in recent_actions:
257                 #print "%s - %s %s > %s - %s (%s) ==> %s" % (action.loginbase, action.action_type, action.date_created, datetime.now(), timedelta(within), datetime.now()-timedelta(within), action.date_created > (datetime.now() - timedelta(within)) )
258                 if action_type == action.action_type and \
259                                 action.date_created > (datetime.now() - timedelta(within)):
260                                 #datetime.now() - action.date_created < timedelta(within):
261                         # recent action of given type.
262                         #print "%s found_within %s in recent_actions from %s" % (action_type, timedelta(within), action.date_created)
263                         return True
264
265         print "%s NOT found_within %s in recent_actions" % (action_type, timedelta(within) )
266         return False
267