4 esc = struct.pack('i', 27)
7 YELLOW = esc + "[1;33m"
9 LIGHTBLUE = esc + "[1;36m"
10 NORMAL = esc + "[0;39m"
13 return RED + str + NORMAL
16 return YELLOW + str + NORMAL
19 return GREEN + str + NORMAL
22 return LIGHTBLUE + str + NORMAL
25 return BLUE + str + NORMAL
27 def get_current_state(fbnode):
29 state = fbnode['state']
33 if l == "debug": l = 'dbg '
36 def color_pcu_state(fbnode):
39 if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
40 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
44 if 'pcu' not in fbnode:
49 if 'reboot' in values:
51 if rb == 0 or rb == "0":
52 return fbnode['pcu'] + "OK "
53 #return fbnode['pcu'] + "OK "
54 #return green(fbnode['pcu'])
55 elif "NetDown" == rb or "Not_Run" == rb:
56 return fbnode['pcu'] + "DOWN"
57 #return yellow(fbnode['pcu'])
59 return fbnode['pcu'] + "BAD "
60 #return red(fbnode['pcu'])
62 #return red(fbnode['pcu'])
63 return fbnode['pcu'] + "BAD "
65 def color_boot_state(l):
66 if l == "dbg": return yellow("debg")
67 elif l == "dbg ": return yellow("debg")
68 elif l == "diag": return lightblue(l)
69 elif l == "disable": return red("dsbl")
70 elif l == "down": return red(l)
71 elif l == "boot": return green(l)
72 elif l == "rins": return blue(l)
76 def diff_time(timestamp):
81 diff = now - timestamp
82 # return the number of seconds as a difference from current time.
84 if diff < 60: # sec in min.
86 t_str = "%s sec ago" % int(math.ceil(t))
87 elif diff < 60*60: # sec in hour
89 t_str = "%s min ago" % int(math.ceil(t))
90 elif diff < 60*60*24: # sec in day
92 t_str = "%s hrs ago" % int(math.ceil(t))
93 elif diff < 60*60*24*7: # sec in week
95 t_str = "%s days ago" % int(math.ceil(t))
96 elif diff <= 60*60*24*30: # approx sec in month
97 t = diff / (60*60*24*7)
98 t_str = "%s wks ago" % int(math.ceil(t))
99 elif diff > 60*60*24*30: # approx sec in month
100 t = diff / (60*60*24*30)
101 t_str = "%s mnths ago" % int(t)
104 def nodegroup_display(node, fb, conf=None):
105 if node['hostname'] in fb['nodes']:
106 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
108 node['current'] = 'none'
110 if fb['nodes'][node['hostname']]['values'] == []:
113 s = fb['nodes'][node['hostname']]['values']['kernel'].split()
115 node['kernel'] = s[2]
117 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
119 if '2.6' not in node['kernel']: node['kernel'] = ""
120 if conf and not conf.nocolor:
121 node['boot_state'] = color_boot_state(node['boot_state'])
122 node['current'] = color_boot_state(node['current'])
123 #node['boot_state'] = node['boot_state']
124 #node['current'] = node['current']
125 node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
126 node['lastupdate'] = diff_time(node['last_contact'])
128 return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)20.20s... %(kernel)43s %(lastupdate)12s " % node
133 def node_end_record(node):
134 act_all = database.dbLoad("act_all")
135 if node not in act_all:
139 if len(act_all[node]) == 0:
143 a = Action(node, act_all[node][0])
145 a.delField('found_rt_ticket')
146 a.delField('second-mail-at-oneweek')
147 a.delField('second-mail-at-twoweeks')
148 a.delField('first-found')
150 rec['action'] = ["close_rt"]
151 rec['category'] = "UNKNOWN"
152 rec['stage'] = "monitor-end-record"
153 rec['time'] = time.time() - 7*60*60*24
154 act_all[node].insert(0,rec)
155 database.dbDump("act_all", act_all)
159 def datetime_fromstr(str):
162 tup = time.strptime(str, "%Y-%m-%d")
164 tup = time.strptime(str, "%Y-%m-%d-%H:%M")
166 tup = time.strptime(str, "%m/%d/%Y")
168 tup = time.strptime(str, "%m/%d/%Y")
169 ret = datetime.fromtimestamp(time.mktime(tup))