5 from monitor import database
6 from unified_model import PersistFlags
7 esc = struct.pack('i', 27)
10 YELLOW = esc + "[1;33m"
12 LIGHTBLUE = esc + "[1;36m"
13 NORMAL = esc + "[0;39m"
16 return RED + str + NORMAL
19 return YELLOW + str + NORMAL
22 return GREEN + str + NORMAL
25 return LIGHTBLUE + str + NORMAL
28 return BLUE + str + NORMAL
30 def get_current_state(fbnode):
32 state = fbnode['state']
36 if l == "debug": l = 'dbg '
39 def color_pcu_state(fbnode):
42 if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
43 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
47 if 'pcu' not in fbnode:
52 if 'reboot' in values:
54 if rb == 0 or rb == "0":
55 return fbnode['pcu'] + "OK "
56 #return fbnode['pcu'] + "OK "
57 #return green(fbnode['pcu'])
58 elif "NetDown" == rb or "Not_Run" == rb:
59 return fbnode['pcu'] + "DOWN"
60 #return yellow(fbnode['pcu'])
62 return fbnode['pcu'] + "BAD "
63 #return red(fbnode['pcu'])
65 #return red(fbnode['pcu'])
66 return fbnode['pcu'] + "BAD "
68 def color_boot_state(l):
69 if l == "dbg": return yellow("debg")
70 elif l == "dbg ": return yellow("debg")
71 elif l == "diag": return lightblue(l)
72 elif l == "disable": return red("dsbl")
73 elif l == "down": return red(l)
74 elif l == "boot": return green(l)
75 elif l == "rins": return blue(l)
79 def diff_time(timestamp, abstime=True):
85 diff = now - timestamp
88 # return the number of seconds as a difference from current time.
90 if diff < 60: # sec in min.
92 t_str = "%s sec ago" % int(math.ceil(t))
93 elif diff < 60*60: # sec in hour
95 t_str = "%s min ago" % int(math.ceil(t))
96 elif diff < 60*60*24: # sec in day
98 t_str = "%s hrs ago" % int(math.ceil(t))
99 elif diff < 60*60*24*14: # sec in week
100 t = diff / (60*60*24)
101 t_str = "%s days ago" % int(math.ceil(t))
102 elif diff <= 60*60*24*30: # approx sec in month
103 t = diff / (60*60*24*7)
104 t_str = "%s wks ago" % int(math.ceil(t))
105 elif diff > 60*60*24*30: # approx sec in month
106 t = diff / (60*60*24*30)
107 t_str = "%s mnths ago" % int(t)
110 def nodegroup_display(node, fb, conf=None):
111 if node['hostname'] in fb['nodes']:
112 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
114 node['current'] = 'none'
116 if fb['nodes'][node['hostname']]['values'] == []:
119 s = fb['nodes'][node['hostname']]['values']['kernel'].split()
121 node['kernel'] = s[2]
123 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
125 if '2.6' not in node['kernel']: node['kernel'] = ""
126 if conf and not conf.nocolor:
127 node['boot_state'] = color_boot_state(node['boot_state'])
128 node['current'] = color_boot_state(node['current'])
129 #node['boot_state'] = node['boot_state']
130 #node['current'] = node['current']
131 node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
132 node['lastupdate'] = diff_time(node['last_contact'])
133 pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
134 node['lc'] = diff_time(pf.last_changed)
135 ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime']
137 ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False)
140 return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
142 def datetime_fromstr(str):
145 tup = time.strptime(str, "%Y-%m-%d")
147 tup = time.strptime(str, "%Y-%m-%d-%H:%M")
149 tup = time.strptime(str, "%m/%d/%Y")
151 tup = time.strptime(str, "%m/%d/%Y")
152 ret = datetime.fromtimestamp(time.mktime(tup))