cef1247951494f7514fcdb69068f40b4c0128a98
[monitor.git] / nodecommon.py
1
2 import struct
3 import reboot
4 import time
5 from monitor import database
6 from unified_model import PersistFlags
7 esc = struct.pack('i', 27)
8 RED     = esc + "[1;31m"
9 GREEN   = esc + "[1;32m"
10 YELLOW  = esc + "[1;33m"
11 BLUE    = esc + "[1;34m"
12 LIGHTBLUE       = esc + "[1;36m"
13 NORMAL  = esc + "[0;39m"
14
15 def red(str):
16         return RED + str + NORMAL
17
18 def yellow(str):
19         return YELLOW + str + NORMAL
20
21 def green(str):
22         return GREEN + str + NORMAL
23
24 def lightblue(str):
25         return LIGHTBLUE + str + NORMAL
26
27 def blue(str):
28         return BLUE + str + NORMAL
29
30 def get_current_state(fbnode):
31         if 'state' in fbnode:
32                 state = fbnode['state']
33         else:
34                 state = "none"
35         l = state.lower()
36         if l == "debug": l = 'dbg '
37         return l
38
39 def color_pcu_state(fbnode):
40         import plc
41
42         if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
43                 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
44                 if values == None:
45                         return fbnode['pcu']
46         else:
47                 if 'pcu' not in fbnode:
48                         return 'NOPCU'
49                 else:
50                         return fbnode['pcu']
51
52         if 'reboot' in values:
53                 rb = values['reboot']
54                 if rb == 0 or rb == "0":
55                         return fbnode['pcu'] + "OK  "
56                         #return fbnode['pcu'] + "OK  "
57                         #return green(fbnode['pcu'])
58                 elif "NetDown" == rb  or "Not_Run" == rb:
59                         return fbnode['pcu'] + "DOWN"
60                         #return yellow(fbnode['pcu'])
61                 else:
62                         return fbnode['pcu'] + "BAD "
63                         #return red(fbnode['pcu'])
64         else:
65                 #return red(fbnode['pcu'])
66                 return fbnode['pcu'] + "BAD "
67
68 def color_boot_state(l):
69         if    l == "dbg": return yellow("debg")
70         elif  l == "dbg ": return yellow("debg")
71         elif  l == "diag": return lightblue(l)
72         elif  l == "disable": return red("dsbl")
73         elif  l == "down": return red(l)
74         elif  l == "boot": return green(l)
75         elif  l == "rins": return blue(l)
76         else:
77                 return l
78
79 def diff_time(timestamp, abstime=True):
80         import math
81         now = time.time()
82         if timestamp == None:
83                 return "unknown"
84         if abstime:
85                 diff = now - timestamp
86         else:
87                 diff = timestamp
88         # return the number of seconds as a difference from current time.
89         t_str = ""
90         if diff < 60: # sec in min.
91                 t = diff / 1
92                 t_str = "%s sec ago" % int(math.ceil(t))
93         elif diff < 60*60: # sec in hour
94                 t = diff / (60)
95                 t_str = "%s min ago" % int(math.ceil(t))
96         elif diff < 60*60*24: # sec in day
97                 t = diff / (60*60)
98                 t_str = "%s hrs ago" % int(math.ceil(t))
99         elif diff < 60*60*24*14: # sec in week
100                 t = diff / (60*60*24)
101                 t_str = "%s days ago" % int(math.ceil(t))
102         elif diff <= 60*60*24*30: # approx sec in month
103                 t = diff / (60*60*24*7)
104                 t_str = "%s wks ago" % int(math.ceil(t))
105         elif diff > 60*60*24*30: # approx sec in month
106                 t = diff / (60*60*24*30)
107                 t_str = "%s mnths ago" % int(t)
108         return t_str
109
110 def nodegroup_display(node, fb, conf=None):
111         if node['hostname'] in fb['nodes']:
112                 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
113         else:
114                 node['current'] = 'none'
115
116         if fb['nodes'][node['hostname']]['values'] == []:
117                 return ""
118
119         s = fb['nodes'][node['hostname']]['values']['kernel'].split()
120         if len(s) >=3:
121                 node['kernel'] = s[2]
122         else:
123                 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
124                 
125         if '2.6' not in node['kernel']: node['kernel'] = ""
126         if conf and not conf.nocolor:
127             node['boot_state']  = color_boot_state(node['boot_state'])
128             node['current']     = color_boot_state(node['current'])
129         #node['boot_state']     = node['boot_state']
130         #node['current']        = node['current']
131         node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
132         node['lastupdate'] = diff_time(node['last_contact'])
133         pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
134         node['lc'] = diff_time(pf.last_changed)
135         ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime']
136         if ut != "null":
137                 ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False)
138         node['uptime'] = ut
139
140         return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
141
142 def datetime_fromstr(str):
143         if '-' in str:
144                 try:
145                         tup = time.strptime(str, "%Y-%m-%d")
146                 except:
147                         tup = time.strptime(str, "%Y-%m-%d-%H:%M")
148         elif '/' in str:
149                 tup = time.strptime(str, "%m/%d/%Y")
150         else:
151                 tup = time.strptime(str, "%m/%d/%Y")
152         ret = datetime.fromtimestamp(time.mktime(tup))
153         return ret