4 from unified_model import PersistFlags
5 esc = struct.pack('i', 27)
8 YELLOW = esc + "[1;33m"
10 LIGHTBLUE = esc + "[1;36m"
11 NORMAL = esc + "[0;39m"
14 return RED + str + NORMAL
17 return YELLOW + str + NORMAL
20 return GREEN + str + NORMAL
23 return LIGHTBLUE + str + NORMAL
26 return BLUE + str + NORMAL
28 def get_current_state(fbnode):
30 state = fbnode['state']
34 if l == "debug": l = 'dbg '
37 def color_pcu_state(fbnode):
40 if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
41 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
45 if 'pcu' not in fbnode:
50 if 'reboot' in values:
52 if rb == 0 or rb == "0":
53 return fbnode['pcu'] + "OK "
54 #return fbnode['pcu'] + "OK "
55 #return green(fbnode['pcu'])
56 elif "NetDown" == rb or "Not_Run" == rb:
57 return fbnode['pcu'] + "DOWN"
58 #return yellow(fbnode['pcu'])
60 return fbnode['pcu'] + "BAD "
61 #return red(fbnode['pcu'])
63 #return red(fbnode['pcu'])
64 return fbnode['pcu'] + "BAD "
66 def color_boot_state(l):
67 if l == "dbg": return yellow("debg")
68 elif l == "dbg ": return yellow("debg")
69 elif l == "diag": return lightblue(l)
70 elif l == "disable": return red("dsbl")
71 elif l == "down": return red(l)
72 elif l == "boot": return green(l)
73 elif l == "rins": return blue(l)
77 def diff_time(timestamp, abstime=True):
83 diff = now - timestamp
86 # return the number of seconds as a difference from current time.
88 if diff < 60: # sec in min.
90 t_str = "%s sec ago" % int(math.ceil(t))
91 elif diff < 60*60: # sec in hour
93 t_str = "%s min ago" % int(math.ceil(t))
94 elif diff < 60*60*24: # sec in day
96 t_str = "%s hrs ago" % int(math.ceil(t))
97 elif diff < 60*60*24*14: # sec in week
99 t_str = "%s days ago" % int(math.ceil(t))
100 elif diff <= 60*60*24*30: # approx sec in month
101 t = diff / (60*60*24*7)
102 t_str = "%s wks ago" % int(math.ceil(t))
103 elif diff > 60*60*24*30: # approx sec in month
104 t = diff / (60*60*24*30)
105 t_str = "%s mnths ago" % int(t)
108 def nodegroup_display(node, fb, conf=None):
109 if node['hostname'] in fb['nodes']:
110 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
112 node['current'] = 'none'
114 if fb['nodes'][node['hostname']]['values'] == []:
117 s = fb['nodes'][node['hostname']]['values']['kernel'].split()
119 node['kernel'] = s[2]
121 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
123 if '2.6' not in node['kernel']: node['kernel'] = ""
124 if conf and not conf.nocolor:
125 node['boot_state'] = color_boot_state(node['boot_state'])
126 node['current'] = color_boot_state(node['current'])
127 #node['boot_state'] = node['boot_state']
128 #node['current'] = node['current']
129 node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
130 node['lastupdate'] = diff_time(node['last_contact'])
131 pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
132 node['lc'] = diff_time(pf.last_changed)
133 ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime']
135 ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False)
138 return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
143 def node_end_record(node):
144 act_all = database.dbLoad("act_all")
145 if node not in act_all:
149 if len(act_all[node]) == 0:
153 a = Action(node, act_all[node][0])
155 a.delField('found_rt_ticket')
156 a.delField('second-mail-at-oneweek')
157 a.delField('second-mail-at-twoweeks')
158 a.delField('first-found')
160 rec['action'] = ["close_rt"]
161 rec['category'] = "UNKNOWN"
162 rec['stage'] = "monitor-end-record"
163 rec['time'] = time.time() - 7*60*60*24
164 act_all[node].insert(0,rec)
165 database.dbDump("act_all", act_all)
169 def datetime_fromstr(str):
172 tup = time.strptime(str, "%Y-%m-%d")
174 tup = time.strptime(str, "%Y-%m-%d-%H:%M")
176 tup = time.strptime(str, "%m/%d/%Y")
178 tup = time.strptime(str, "%m/%d/%Y")
179 ret = datetime.fromtimestamp(time.mktime(tup))