X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nodecommon.py;h=2250492ad1e4eb9dcd1acffc50fa090679514457;hb=refs%2Fheads%2F1.0;hp=cef1247951494f7514fcdb69068f40b4c0128a98;hpb=944d143a6528c4157b71f51ed480aec806cbaa06;p=monitor.git diff --git a/nodecommon.py b/nodecommon.py index cef1247..2250492 100644 --- a/nodecommon.py +++ b/nodecommon.py @@ -2,6 +2,9 @@ import struct import reboot import time +import util.file +import plc +from datetime import datetime from monitor import database from unified_model import PersistFlags esc = struct.pack('i', 27) @@ -37,7 +40,6 @@ def get_current_state(fbnode): return l def color_pcu_state(fbnode): - import plc if 'plcnode' in fbnode and 'pcu_ids' in fbnode['plcnode'] and len(fbnode['plcnode']['pcu_ids']) > 0 : values = reboot.get_pcu_values(fbnode['plcnode']['pcu_ids'][0]) @@ -73,6 +75,7 @@ def color_boot_state(l): elif l == "down": return red(l) elif l == "boot": return green(l) elif l == "rins": return blue(l) + elif l == "reinstall": return blue(l) else: return l @@ -107,6 +110,16 @@ def diff_time(timestamp, abstime=True): t_str = "%s mnths ago" % int(t) return t_str +def getvalue(fb, path): + indexes = path.split("/") + values = fb + for index in indexes: + if index in values: + values = values[index] + else: + return None + return values + def nodegroup_display(node, fb, conf=None): if node['hostname'] in fb['nodes']: node['current'] = get_current_state(fb['nodes'][node['hostname']]['values']) @@ -131,13 +144,16 @@ def nodegroup_display(node, fb, conf=None): node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu'] node['lastupdate'] = diff_time(node['last_contact']) pf = PersistFlags(node['hostname'], 1, db='node_persistflags') - node['lc'] = diff_time(pf.last_changed) + try: + node['lc'] = diff_time(pf.last_changed) + except: + node['lc'] = "err" ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime'] if ut != "null": ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False) node['uptime'] = ut - return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)33s %(lastupdate)12s, %(lc)s, %(uptime)s" % node + return "%(hostname)-42s %(boot_state)8s %(current)5s %(pcu)6s %(key)10.10s... %(kernel)35.35s %(lastupdate)12s, %(lc)s, %(uptime)s" % node def datetime_fromstr(str): if '-' in str: @@ -151,3 +167,45 @@ def datetime_fromstr(str): tup = time.strptime(str, "%m/%d/%Y") ret = datetime.fromtimestamp(time.mktime(tup)) return ret + +def get_nodeset(config): + """ + Given the config values passed in, return the set of hostnames that it + evaluates to. + """ + api = plc.getAuthAPI() + l_nodes = database.dbLoad("l_plcnodes") + + if config.nodelist: + f_nodes = util.file.getListFromFile(config.nodelist) + l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes) + elif config.node: + f_nodes = [config.node] + l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes) + elif config.nodegroup: + ng = api.GetNodeGroups({'groupname' : config.nodegroup}) + l_nodes = api.GetNodes(ng[0]['node_ids'], ['hostname']) + elif config.site: + site = api.GetSites(config.site) + l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname']) + + l_nodes = [node['hostname'] for node in l_nodes] + + # perform this query after the above options, so that the filter above + # does not break. + if config.nodeselect: + fb = database.dbLoad("findbad") + l_nodes = node_select(config.nodeselect, fb['nodes'].keys(), fb) + + return l_nodes + +def email_exception(content=None): + import config + from unified_model import Message + import traceback + msg=traceback.format_exc() + if content: + msg = content + "\n" + msg + m=Message("exception running monitor", msg, False) + m.send([config.cc_email]) + return