4 from monitor.pcu import reboot
6 from monitor import util
7 from monitor import database
8 from monitor.wrapper import plc
10 from datetime import datetime
11 from unified_model import PersistFlags
13 esc = struct.pack('i', 27)
15 GREEN = esc + "[1;32m"
16 YELLOW = esc + "[1;33m"
18 LIGHTBLUE = esc + "[1;36m"
19 NORMAL = esc + "[0;39m"
22 return RED + str + NORMAL
25 return YELLOW + str + NORMAL
28 return GREEN + str + NORMAL
31 return LIGHTBLUE + str + NORMAL
34 return BLUE + str + NORMAL
36 def get_current_state(fbnode):
38 state = fbnode['state']
42 if l == "debug": l = 'dbg '
45 def color_pcu_state(fbnode):
47 if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
48 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
52 if 'pcu' not in fbnode:
57 if 'reboot' in values:
59 if rb == 0 or rb == "0":
60 return fbnode['pcu'] + "OK "
61 #return fbnode['pcu'] + "OK "
62 #return green(fbnode['pcu'])
63 elif "NetDown" == rb or "Not_Run" == rb:
64 return fbnode['pcu'] + "DOWN"
65 #return yellow(fbnode['pcu'])
67 return fbnode['pcu'] + "BAD "
68 #return red(fbnode['pcu'])
70 #return red(fbnode['pcu'])
71 return fbnode['pcu'] + "BAD "
73 def color_boot_state(l):
74 if l == "dbg": return yellow("debg")
75 elif l == "dbg ": return yellow("debg")
76 elif l == "diag": return lightblue(l)
77 elif l == "disable": return red("dsbl")
78 elif l == "down": return red(l)
79 elif l == "boot": return green(l)
80 elif l == "rins": return blue(l)
84 def diff_time(timestamp, abstime=True):
90 diff = now - timestamp
93 # return the number of seconds as a difference from current time.
95 if diff < 60: # sec in min.
97 t_str = "%s sec ago" % int(math.ceil(t))
98 elif diff < 60*60: # sec in hour
100 t_str = "%s min ago" % int(math.ceil(t))
101 elif diff < 60*60*24: # sec in day
103 t_str = "%s hrs ago" % int(math.ceil(t))
104 elif diff < 60*60*24*14: # sec in week
105 t = diff / (60*60*24)
106 t_str = "%s days ago" % int(math.ceil(t))
107 elif diff <= 60*60*24*30: # approx sec in month
108 t = diff / (60*60*24*7)
109 t_str = "%s wks ago" % int(math.ceil(t))
110 elif diff > 60*60*24*30: # approx sec in month
111 t = diff / (60*60*24*30)
112 t_str = "%s mnths ago" % int(t)
115 def nodegroup_display(node, fb, conf=None):
116 if node['hostname'] in fb['nodes']:
117 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
119 node['current'] = 'none'
121 if fb['nodes'][node['hostname']]['values'] == []:
124 s = fb['nodes'][node['hostname']]['values']['kernel'].split()
126 node['kernel'] = s[2]
128 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
130 if '2.6' not in node['kernel']: node['kernel'] = ""
131 if conf and not conf.nocolor:
132 node['boot_state'] = color_boot_state(node['boot_state'])
133 node['current'] = color_boot_state(node['current'])
134 #node['boot_state'] = node['boot_state']
135 #node['current'] = node['current']
136 node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
137 node['lastupdate'] = diff_time(node['last_contact'])
138 pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
140 node['lc'] = diff_time(pf.last_changed)
143 ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime']
145 ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False)
148 return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)35.35s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
150 def datetime_fromstr(str):
153 tup = time.strptime(str, "%Y-%m-%d")
155 tup = time.strptime(str, "%Y-%m-%d-%H:%M")
157 tup = time.strptime(str, "%m/%d/%Y")
159 tup = time.strptime(str, "%m/%d/%Y")
160 ret = datetime.fromtimestamp(time.mktime(tup))
163 def get_nodeset(config):
165 Given the config values passed in, return the set of hostnames that it
168 api = plc.getAuthAPI()
169 l_nodes = database.dbLoad("l_plcnodes")
172 f_nodes = util.file.getListFromFile(config.nodelist)
173 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
175 f_nodes = [config.node]
176 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
177 elif config.nodegroup:
178 ng = api.GetNodeGroups({'name' : config.nodegroup})
179 l_nodes = api.GetNodes(ng[0]['node_ids'], ['hostname'])
181 site = api.GetSites(config.site)
182 l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
184 l_nodes = [node['hostname'] for node in l_nodes]
186 # perform this query after the above options, so that the filter above
188 if config.nodeselect:
189 fb = database.dbLoad("findbad")
190 l_nodes = node_select(config.nodeselect, fb['nodes'].keys(), fb)