4 from monitor import reboot
5 from monitor import util
6 from monitor import database
7 from monitor.wrapper import plc, plccache
9 from datetime import datetime, timedelta
10 from monitor.model import Message
11 from monitor.database.info import HistoryNodeRecord
13 esc = struct.pack('i', 27)
15 GREEN = esc + "[1;32m"
16 YELLOW = esc + "[1;33m"
18 LIGHTBLUE = esc + "[1;36m"
19 NORMAL = esc + "[0;39m"
22 return RED + str + NORMAL
25 return YELLOW + str + NORMAL
28 return GREEN + str + NORMAL
31 return LIGHTBLUE + str + NORMAL
34 return BLUE + str + NORMAL
36 def get_current_state(fbnode):
37 if 'observed_status' in fbnode:
38 state = fbnode['observed_status']
42 if l == "debug": l = 'dbg '
45 def color_pcu_state(fbnode):
47 if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 :
48 values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0])
52 if 'pcu' not in fbnode:
57 if 'reboot' in values:
59 if rb == 0 or rb == "0":
60 return fbnode['pcu'] + "OK "
61 #return fbnode['pcu'] + "OK "
62 #return green(fbnode['pcu'])
63 elif "NetDown" == rb or "Not_Run" == rb:
64 return fbnode['pcu'] + "DOWN"
65 #return yellow(fbnode['pcu'])
67 return fbnode['pcu'] + "BAD "
68 #return red(fbnode['pcu'])
70 #return red(fbnode['pcu'])
71 return fbnode['pcu'] + "BAD "
73 def color_boot_state(l):
74 if l == "dbg": return yellow("debg")
75 elif l == "dbg ": return yellow("debg")
76 elif l == "diag": return lightblue(l)
77 elif l == "disable": return red("dsbl")
78 elif l == "down": return red(l)
79 elif l == "boot": return green(l)
80 elif l == "rins": return blue(l)
81 elif l == "reinstall": return blue(l)
85 def diff_time(timestamp, abstime=True):
90 if type(timestamp) == type(datetime.now()):
91 timestamp = time.mktime(timestamp.timetuple())
93 diff = now - timestamp
96 # return the number of seconds as a difference from current time.
98 if diff < 60: # sec in min.
100 t_str = "%s sec ago" % int(math.ceil(t))
101 elif diff < 60*60: # sec in hour
103 t_str = "%s min ago" % int(math.ceil(t))
104 elif diff < 60*60*24: # sec in day
106 t_str = "%s hrs ago" % int(math.ceil(t))
107 elif diff < 60*60*24*14: # sec in week
108 t = diff / (60*60*24)
109 t_str = "%s days ago" % int(math.ceil(t))
110 elif diff <= 60*60*24*30: # approx sec in month
111 t = diff / (60*60*24*7)
112 t_str = "%s wks ago" % int(math.ceil(t))
113 elif diff > 60*60*24*30: # approx sec in month
114 t = diff / (60*60*24*30)
115 t_str = "%s mnths ago" % int(t)
118 def getvalue(fb, path):
119 indexes = path.split("/")
121 for index in indexes:
123 values = values[index]
128 def nmap_port_status(status):
130 l_nmap = status.split()
133 continue_probe = False
135 results = port.split('/')
136 ps[results[0]] = results[1]
137 if results[1] == "open":
138 continue_probe = True
139 return (ps, continue_probe)
142 def nodegroup_display(node, fbdata, conf=None):
143 node['current'] = get_current_state(fbdata)
145 s = fbdata['kernel_version'].split()
147 node['kernel_version'] = s[2]
149 node['kernel_version'] = fbdata['kernel_version']
151 if '2.6' not in node['kernel_version']: node['kernel_version'] = ""
152 if conf and not conf.nocolor:
153 node['boot_state'] = color_boot_state(node['boot_state'])
154 node['current'] = color_boot_state(node['current'])
156 if type(fbdata['plc_node_stats']['pcu_ids']) == type([]):
158 node['lastupdate'] = diff_time(node['last_contact'])
160 pf = HistoryNodeRecord.get_by(hostname=node['hostname'])
162 node['lc'] = diff_time(pf.last_changed)
166 ut = fbdata['comon_stats']['uptime']
168 ut = diff_time(float(fbdata['comon_stats']['uptime']), False)
171 return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel_version)35.35s %(lastupdate)12s, %(lc)s, %(uptime)s" % node
173 def datetime_fromstr(str):
176 tup = time.strptime(str, "%Y-%m-%d")
178 tup = time.strptime(str, "%Y-%m-%d-%H:%M")
180 tup = time.strptime(str, "%m/%d/%Y")
182 tup = time.strptime(str, "%m/%d/%Y")
183 ret = datetime.fromtimestamp(time.mktime(tup))
186 def get_nodeset(config):
188 Given the config values passed in, return the set of hostnames that it
191 api = plc.getAuthAPI()
192 l_nodes = plccache.l_nodes
195 f_nodes = util.file.getListFromFile(config.nodelist)
196 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
198 f_nodes = [config.node]
199 l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
200 elif config.nodegroup:
201 ng = api.GetNodeGroups({'name' : config.nodegroup})
202 l_nodes = api.GetNodes(ng[0]['node_ids'], ['hostname'])
204 site = api.GetSites(config.site)
205 l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
207 l_nodes = [node['hostname'] for node in l_nodes]
209 # perform this query after the above options, so that the filter above
211 if config.nodeselect:
212 fbquery = FindbadNodeRecord.get_all_latest()
213 node_list = [ n.hostname for n in fbquery ]
214 l_nodes = node_select(config.nodeselect, node_list, None)
218 def email_exception(content=None):
220 from monitor.model import Message
222 msg=traceback.format_exc()
224 msg = content + "\n" + msg
225 m=Message("exception running monitor", msg, False)
226 m.send([config.cc_email])
229 def changed_lessthan(last_changed, days):
230 if datetime.now() - last_changed <= timedelta(days):
231 #print "last changed less than %s" % timedelta(days)
234 #print "last changed more than %s" % timedelta(days)
237 def changed_greaterthan(last_changed, days):
238 if datetime.now() - last_changed > timedelta(days):
239 #print "last changed more than %s" % timedelta(days)
242 #print "last changed less than %s" % timedelta(days)
245 def found_between(recent_actions, action_type, lower, upper):
246 return found_before(recent_actions, action_type, upper) and found_within(recent_actions, action_type, lower)
248 def found_before(recent_actions, action_type, within):
249 for action in recent_actions:
250 if action_type == action.action_type and \
251 action.date_created < (datetime.now() - timedelta(within)):
255 def found_within(recent_actions, action_type, within):
256 for action in recent_actions:
257 #print "%s - %s %s > %s - %s (%s) ==> %s" % (action.loginbase, action.action_type, action.date_created, datetime.now(), timedelta(within), datetime.now()-timedelta(within), action.date_created > (datetime.now() - timedelta(within)) )
258 if action_type == action.action_type and \
259 action.date_created > (datetime.now() - timedelta(within)):
260 #datetime.now() - action.date_created < timedelta(within):
261 # recent action of given type.
262 #print "%s found_within %s in recent_actions from %s" % (action_type, timedelta(within), action.date_created)
265 print "%s NOT found_within %s in recent_actions" % (action_type, timedelta(within) )