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