X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nodecommon.py;h=051cd61aea9e23e3cdf8b59f987e97e860b07ef4;hb=bbdd1222ad57a915bbb3d872a1cf1da759ef85e3;hp=cef1247951494f7514fcdb69068f40b4c0128a98;hpb=944d143a6528c4157b71f51ed480aec806cbaa06;p=monitor.git diff --git a/nodecommon.py b/nodecommon.py index cef1247..051cd61 100644 --- a/nodecommon.py +++ b/nodecommon.py @@ -1,9 +1,15 @@ -import struct -import reboot import time +import struct +from pcucontrol import reboot + +from monitor import util from monitor import database -from unified_model import PersistFlags +from monitor.wrapper import plc, plccache + +from datetime import datetime +from monitor.model import PersistFlags + esc = struct.pack('i', 27) RED = esc + "[1;31m" GREEN = esc + "[1;32m" @@ -28,8 +34,8 @@ def blue(str): return BLUE + str + NORMAL def get_current_state(fbnode): - if 'state' in fbnode: - state = fbnode['state'] + if 'observed_status' in fbnode: + state = fbnode['observed_status'] else: state = "none" l = state.lower() @@ -37,7 +43,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]) @@ -107,37 +112,60 @@ def diff_time(timestamp, abstime=True): t_str = "%s mnths ago" % int(t) return t_str -def nodegroup_display(node, fb, conf=None): - if node['hostname'] in fb['nodes']: - node['current'] = get_current_state(fb['nodes'][node['hostname']]['values']) - else: - node['current'] = 'none' - - if fb['nodes'][node['hostname']]['values'] == []: - return "" - - s = fb['nodes'][node['hostname']]['values']['kernel'].split() +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 nmap_port_status(status): + ps = {} + l_nmap = status.split() + ports = l_nmap[4:] + + continue_probe = False + for port in ports: + results = port.split('/') + ps[results[0]] = results[1] + if results[1] == "open": + continue_probe = True + return (ps, continue_probe) + + +def nodegroup_display(node, fbdata, conf=None): + node['current'] = get_current_state(fbdata) + + s = fbdata['kernel_version'].split() if len(s) >=3: - node['kernel'] = s[2] + node['kernel_version'] = s[2] else: - node['kernel'] = fb['nodes'][node['hostname']]['values']['kernel'] + node['kernel_version'] = fbdata['kernel_version'] - if '2.6' not in node['kernel']: node['kernel'] = "" + if '2.6' not in node['kernel_version']: node['kernel_version'] = "" if conf and not conf.nocolor: node['boot_state'] = color_boot_state(node['boot_state']) node['current'] = color_boot_state(node['current']) - #node['boot_state'] = node['boot_state'] - #node['current'] = node['current'] - node['pcu'] = fb['nodes'][node['hostname']]['values']['pcu'] + + if type(fbdata['plc_node_stats']['pcu_ids']) == type([]): + node['pcu'] = "PCU" node['lastupdate'] = diff_time(node['last_contact']) + pf = PersistFlags(node['hostname'], 1, db='node_persistflags') - node['lc'] = diff_time(pf.last_changed) - ut = fb['nodes'][node['hostname']]['values']['comonstats']['uptime'] + try: + node['lc'] = diff_time(pf.last_changed) + except: + node['lc'] = "err" + + ut = fbdata['comon_stats']['uptime'] if ut != "null": - ut = diff_time(float(fb['nodes'][node['hostname']]['values']['comonstats']['uptime']), False) + ut = diff_time(float(fbdata['comon_stats']['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_version)35.35s %(lastupdate)12s, %(lc)s, %(uptime)s" % node def datetime_fromstr(str): if '-' in str: @@ -151,3 +179,36 @@ 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 = plccache.l_nodes + + 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({'name' : 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: + fbquery = FindbadNodeRecord.get_all_latest() + node_list = [ n.hostname for n in fbquery ] + l_nodes = node_select(config.nodeselect, node_list, None) + + return l_nodes +