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):
82 diff = now - timestamp
83 # return the number of seconds as a difference from current time.
85 if diff < 60: # sec in min.
87 t_str = "%s sec ago" % int(math.ceil(t))
88 elif diff < 60*60: # sec in hour
90 t_str = "%s min ago" % int(math.ceil(t))
91 elif diff < 60*60*24: # sec in day
93 t_str = "%s hrs ago" % int(math.ceil(t))
94 elif diff < 60*60*24*14: # sec in week
96 t_str = "%s days ago" % int(math.ceil(t))
97 elif diff <= 60*60*24*30: # approx sec in month
98 t = diff / (60*60*24*7)
99 t_str = "%s wks ago" % int(math.ceil(t))
100 elif diff > 60*60*24*30: # approx sec in month
101 t = diff / (60*60*24*30)
102 t_str = "%s mnths ago" % int(t)
105 def nodegroup_display(node, fb, conf=None):
106 if node['hostname'] in fb['nodes']:
107 node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
109 node['current'] = 'none'
111 if fb['nodes'][node['hostname']]['values'] == []:
114 s = fb['nodes'][node['hostname']]['values']['kernel'].split()
116 node['kernel'] = s[2]
118 node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel']
120 if '2.6' not in node['kernel']: node['kernel'] = ""
121 if conf and not conf.nocolor:
122 node['boot_state'] = color_boot_state(node['boot_state'])
123 node['current'] = color_boot_state(node['current'])
124 #node['boot_state'] = node['boot_state']
125 #node['current'] = node['current']
126 node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu']
127 node['lastupdate'] = diff_time(node['last_contact'])
128 pf = PersistFlags(node['hostname'], 1, db='node_persistflags')
129 node['lc'] = diff_time(pf.last_changed)
131 return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s" % node
136 def node_end_record(node):
137 act_all = database.dbLoad("act_all")
138 if node not in act_all:
142 if len(act_all[node]) == 0:
146 a = Action(node, act_all[node][0])
148 a.delField('found_rt_ticket')
149 a.delField('second-mail-at-oneweek')
150 a.delField('second-mail-at-twoweeks')
151 a.delField('first-found')
153 rec['action'] = ["close_rt"]
154 rec['category'] = "UNKNOWN"
155 rec['stage'] = "monitor-end-record"
156 rec['time'] = time.time() - 7*60*60*24
157 act_all[node].insert(0,rec)
158 database.dbDump("act_all", act_all)
162 def datetime_fromstr(str):
165 tup = time.strptime(str, "%Y-%m-%d")
167 tup = time.strptime(str, "%Y-%m-%d-%H:%M")
169 tup = time.strptime(str, "%m/%d/%Y")
171 tup = time.strptime(str, "%m/%d/%Y")
172 ret = datetime.fromtimestamp(time.mktime(tup))