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