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