www/database.php
[monitor.git] / nodecommon.py
1
2 import struct
3 import reboot
4 import time
5 import util.file
6 import plc
7 from monitor import database
8 from unified_model import PersistFlags
9 esc = struct.pack('i', 27)
10 RED     = esc + "[1;31m"
11 GREEN   = esc + "[1;32m"
12 YELLOW  = esc + "[1;33m"
13 BLUE    = esc + "[1;34m"
14 LIGHTBLUE       = esc + "[1;36m"
15 NORMAL  = esc + "[0;39m"
16
17 def red(str):
18         return RED + str + NORMAL
19
20 def yellow(str):
21         return YELLOW + str + NORMAL
22
23 def green(str):
24         return GREEN + str + NORMAL
25
26 def lightblue(str):
27         return LIGHTBLUE + str + NORMAL
28
29 def blue(str):
30         return BLUE + str + NORMAL
31
32 def get_current_state(fbnode):
33         if 'state' in fbnode:
34                 state = fbnode['state']
35         else:
36                 state = "none"
37         l = state.lower()
38         if l == "debug": l = 'dbg '
39         return l
40
41 def color_pcu_state(fbnode):
42
43         if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
44                 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
45                 if values == None:
46                         return fbnode['pcu']
47         else:
48                 if 'pcu' not in fbnode:
49                         return 'NOPCU'
50                 else:
51                         return fbnode['pcu']
52
53         if 'reboot' in values:
54                 rb = values['reboot']
55                 if rb == 0 or rb == "0":
56                         return fbnode['pcu'] + "OK  "
57                         #return fbnode['pcu'] + "OK  "
58                         #return green(fbnode['pcu'])
59                 elif "NetDown" == rb  or "Not_Run" == rb:
60                         return fbnode['pcu'] + "DOWN"
61                         #return yellow(fbnode['pcu'])
62                 else:
63                         return fbnode['pcu'] + "BAD "
64                         #return red(fbnode['pcu'])
65         else:
66                 #return red(fbnode['pcu'])
67                 return fbnode['pcu'] + "BAD "
68
69 def color_boot_state(l):
70         if    l == "dbg": return yellow("debg")
71         elif  l == "dbg ": return yellow("debg")
72         elif  l == "diag": return lightblue(l)
73         elif  l == "disable": return red("dsbl")
74         elif  l == "down": return red(l)
75         elif  l == "boot": return green(l)
76         elif  l == "rins": return blue(l)
77         else:
78                 return l
79
80 def diff_time(timestamp, abstime=True):
81         import math
82         now = time.time()
83         if timestamp == None:
84                 return "unknown"
85         if abstime:
86                 diff = now - timestamp
87         else:
88                 diff = timestamp
89         # return the number of seconds as a difference from current time.
90         t_str = ""
91         if diff < 60: # sec in min.
92                 t = diff / 1
93                 t_str = "%s sec ago" % int(math.ceil(t))
94         elif diff < 60*60: # sec in hour
95                 t = diff / (60)
96                 t_str = "%s min ago" % int(math.ceil(t))
97         elif diff < 60*60*24: # sec in day
98                 t = diff / (60*60)
99                 t_str = "%s hrs ago" % int(math.ceil(t))
100         elif diff < 60*60*24*14: # sec in week
101                 t = diff / (60*60*24)
102                 t_str = "%s days ago" % int(math.ceil(t))
103         elif diff <= 60*60*24*30: # approx sec in month
104                 t = diff / (60*60*24*7)
105                 t_str = "%s wks ago" % int(math.ceil(t))
106         elif diff > 60*60*24*30: # approx sec in month
107                 t = diff / (60*60*24*30)
108                 t_str = "%s mnths ago" % int(t)
109         return t_str
110
111 def nodegroup_display(node, fb, conf=None):
112         if node['hostname'] in fb['nodes']:
113                 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
114         else:
115                 node['current'] = 'none'
116
117         if fb['nodes'][node['hostname']]['values'] == []:
118                 return ""
119
120         s = fb['nodes'][node['hostname']]['values']['kernel'].split()
121         if len(s) >=3:
122                 node['kernel'] = s[2]
123         else:
124                 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
125                 
126         if '2.6' not in node['kernel']: node['kernel'] = ""
127         if conf and not conf.nocolor:
128             node['boot_state']  = color_boot_state(node['boot_state'])
129             node['current']     = color_boot_state(node['current'])
130         #node['boot_state']     = node['boot_state']
131         #node['current']        = node['current']
132         node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
133         node['lastupdate'] = diff_time(node['last_contact'])
134         pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
135         node['lc'] = diff_time(pf.last_changed)
136         ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime']
137         if ut != "null":
138                 ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False)
139         node['uptime'] = ut
140
141         return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
142
143 def datetime_fromstr(str):
144         if '-' in str:
145                 try:
146                         tup = time.strptime(str, "%Y-%m-%d")
147                 except:
148                         tup = time.strptime(str, "%Y-%m-%d-%H:%M")
149         elif '/' in str:
150                 tup = time.strptime(str, "%m/%d/%Y")
151         else:
152                 tup = time.strptime(str, "%m/%d/%Y")
153         ret = datetime.fromtimestamp(time.mktime(tup))
154         return ret
155
156 def get_nodeset(config):
157         """
158                 Given the config values passed in, return the set of hostnames that it
159                 evaluates to.
160         """
161         api = plc.getAuthAPI()
162         l_nodes = database.dbLoad("l_plcnodes")
163
164         if config.nodelist:
165                 f_nodes = util.file.getListFromFile(config.nodelist)
166                 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
167         elif config.node:
168                 f_nodes = [config.node]
169                 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
170         elif config.nodegroup:
171                 ng = api.GetNodeGroups({'name' : config.nodegroup})
172                 l_nodes = api.GetNodes(ng[0]['node_ids'], ['hostname'])
173         elif config.site:
174                 site = api.GetSites(config.site)
175                 l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
176                 
177         l_nodes = [node['hostname'] for node in l_nodes]
178
179         # perform this query after the above options, so that the filter above
180         # does not break.
181         if config.nodeselect:
182                 fb = database.dbLoad("findbad")
183                 l_nodes = node_select(config.nodeselect, fb['nodes'].keys(), fb)
184
185         return l_nodes
186