show days up to 14, display time up or down in nodegroup_dislplay
[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):
78         import math
79         now = time.time()
80         if timestamp == None:
81                 return "unknown"
82         diff = now - timestamp
83         # return the number of seconds as a difference from current time.
84         t_str = ""
85         if diff < 60: # sec in min.
86                 t = diff / 1
87                 t_str = "%s sec ago" % int(math.ceil(t))
88         elif diff < 60*60: # sec in hour
89                 t = diff / (60)
90                 t_str = "%s min ago" % int(math.ceil(t))
91         elif diff < 60*60*24: # sec in day
92                 t = diff / (60*60)
93                 t_str = "%s hrs ago" % int(math.ceil(t))
94         elif diff < 60*60*24*14: # sec in week
95                 t = diff / (60*60*24)
96                 t_str = "%s days ago" % int(math.ceil(t))
97         elif diff <= 60*60*24*30: # approx sec in month
98                 t = diff / (60*60*24*7)
99                 t_str = "%s wks ago" % int(math.ceil(t))
100         elif diff > 60*60*24*30: # approx sec in month
101                 t = diff / (60*60*24*30)
102                 t_str = "%s mnths ago" % int(t)
103         return t_str
104
105 def nodegroup_display(node, fb, conf=None):
106         if node['hostname'] in fb['nodes']:
107                 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
108         else:
109                 node['current'] = 'none'
110
111         if fb['nodes'][node['hostname']]['values'] == []:
112                 return ""
113
114         s = fb['nodes'][node['hostname']]['values']['kernel'].split()
115         if len(s) >=3:
116                 node['kernel'] = s[2]
117         else:
118                 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
119                 
120         if '2.6' not in node['kernel']: node['kernel'] = ""
121         if conf and not conf.nocolor:
122             node['boot_state']  = color_boot_state(node['boot_state'])
123             node['current']     = color_boot_state(node['current'])
124         #node['boot_state']     = node['boot_state']
125         #node['current']        = node['current']
126         node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
127         node['lastupdate'] = diff_time(node['last_contact'])
128         pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
129         node['lc'] = diff_time(pf.last_changed)
130
131         return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s" % node
132
133 from model import *
134 import database
135
136 def node_end_record(node):
137         act_all = database.dbLoad("act_all")
138         if node not in act_all:
139                 del act_all
140                 return False
141
142         if len(act_all[node]) == 0:
143                 del act_all
144                 return False
145
146         a = Action(node, act_all[node][0])
147         a.delField('rt')
148         a.delField('found_rt_ticket')
149         a.delField('second-mail-at-oneweek')
150         a.delField('second-mail-at-twoweeks')
151         a.delField('first-found')
152         rec = a.get()
153         rec['action'] = ["close_rt"]
154         rec['category'] = "UNKNOWN"
155         rec['stage'] = "monitor-end-record"
156         rec['time'] = time.time() - 7*60*60*24
157         act_all[node].insert(0,rec)
158         database.dbDump("act_all", act_all)
159         del act_all
160         return True
161
162 def datetime_fromstr(str):
163         if '-' in str:
164                 try:
165                         tup = time.strptime(str, "%Y-%m-%d")
166                 except:
167                         tup = time.strptime(str, "%Y-%m-%d-%H:%M")
168         elif '/' in str:
169                 tup = time.strptime(str, "%m/%d/%Y")
170         else:
171                 tup = time.strptime(str, "%m/%d/%Y")
172         ret = datetime.fromtimestamp(time.mktime(tup))
173         return ret