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