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