X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=nodequery.py;h=dfe3f9580babb483330780e48cbb47480bcd9616;hb=ca65ff1cd55b2a285320ec8562e8ca1e29fbc4f0;hp=16c0bad8a242d62cd81cad0845808a87738042c1;hpb=156cb49ba49cc7cc8a91e7319d3c6f765328108b;p=monitor.git diff --git a/nodequery.py b/nodequery.py index 16c0bad..dfe3f95 100755 --- a/nodequery.py +++ b/nodequery.py @@ -1,25 +1,26 @@ #!/usr/bin/python -import plc -api = plc.getAuthAPI() import sys -import database -from nodecommon import * -#from policy import Diagnose -from unified_model import Record +from monitor import database +from monitor.common import * +from monitor.model import Record import glob import os -from reboot import pcu_name -import reboot -import util.file +import traceback import time import re +import string + +from pcucontrol import reboot +from monitor.wrapper import plc, plccache +api = plc.getAuthAPI() + +from monitor.database.info.model import FindbadNodeRecordSync, FindbadNodeRecord, FindbadPCURecord, session +from monitor import util +from monitor import config -#fb = {} -fb = None -fbpcu = None class NoKeyException(Exception): pass @@ -31,20 +32,26 @@ def daysdown_print_nodeinfo(fbnode, hostname): print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode def fb_print_nodeinfo(fbnode, hostname, fields=None): - fbnode['hostname'] = hostname - fbnode['checked'] = diff_time(fbnode['checked']) - if fbnode['bootcd']: - fbnode['bootcd'] = fbnode['bootcd'].split()[-1] + #fbnode['hostname'] = hostname + #fbnode['checked'] = diff_time(fbnode['checked']) + if fbnode['bootcd_version']: + fbnode['bootcd_version'] = fbnode['bootcd_version'].split()[-1] else: - fbnode['bootcd'] = "unknown" + fbnode['bootcd_version'] = "unknown" fbnode['pcu'] = color_pcu_state(fbnode) if not fields: - if 'ERROR' in fbnode['category']: - fbnode['kernel'] = "" + if ( fbnode['observed_status'] is not None and \ + 'DOWN' in fbnode['observed_status'] ) or \ + fbnode['kernel_version'] is None: + fbnode['kernel_version'] = "" else: - fbnode['kernel'] = fbnode['kernel'].split()[2] - fbnode['boot_state'] = fbnode['plcnode']['boot_state'] + fbnode['kernel_version'] = fbnode['kernel_version'].split()[2] + + if fbnode['plc_node_stats'] is not None: + fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state'] + else: + fbnode['boot_state'] = "unknown" try: if len(fbnode['nodegroups']) > 0: @@ -53,21 +60,25 @@ def fb_print_nodeinfo(fbnode, hostname, fields=None): #print "ERROR!!!!!!!!!!!!!!!!!!!!!" pass - print "%(hostname)-45s | %(checked)11.11s | %(boot_state)5.5s| %(state)8.8s | %(ssh)5.5s | %(pcu)6.6s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode + print "%(hostname)-45s | %(date_checked)11.11s | %(boot_state)5.5s| %(observed_status)8.8s | %(ssh_status)5.5s | %(pcu)6.6s | %(bootcd_version)6.6s | %(kernel_version)s" % fbnode else: format = "" for f in fields: format += "%%(%s)s " % f print format % fbnode +def first(path): + indexes = path.split(".") + return indexes[0] + def get(fb, path): - indexes = path.split("/") + indexes = path.split(".") values = fb for index in indexes: - if index in values: - values = values[index] - else: - raise NoKeyException(index) + if values and index in values: + values = values[index] + else: + raise NoKeyException(index) return values def verifyType(constraints, data): @@ -133,11 +144,37 @@ def verifyType(constraints, data): return con_or_true -def verify(constraints, data): +def verifyDBrecord(constraints, record): """ constraints is a list of key, value pairs. # [ {... : ...}==AND , ... , ... , ] == OR """ + def has_key(obj, key): + try: + x = obj.__getattribute__(key) + return True + except: + return False + + def get_val(obj, key): + try: + return obj.__getattribute__(key) + except: + return None + + def get(obj, path): + indexes = path.split("/") + value = get_val(obj,indexes[0]) + if value is not None and len(indexes) > 1: + for key in indexes[1:]: + if key in value: + value = value[key] + else: + raise NoKeyException(key) + return value + + #print constraints, record + con_or_true = False for con in constraints: #print "con: %s" % con @@ -148,20 +185,51 @@ def verify(constraints, data): for key in con.keys(): #print "looking at key: %s" % key - if key in data: + if has_key(record, key): value_re = re.compile(con[key]) - if type([]) == type(data[key]): + if type([]) == type(get(record,key)): local_or_true = False - for val in data[key]: + for val in get(record,key): local_or_true = local_or_true | (value_re.search(val) is not None) con_and_true = con_and_true & local_or_true else: - con_and_true = con_and_true & (value_re.search(data[key]) is not None) - elif key not in data: + if get(record,key) is not None: + con_and_true = con_and_true & (value_re.search(get(record,key)) is not None) + else: print "missing key %s" % key, pass - #print "missing key %s" % key - #con_and_true = False + + con_or_true = con_or_true | con_and_true + + return con_or_true + +def verify(constraints, data): + """ + constraints is a list of key, value pairs. + # [ {... : ...}==AND , ... , ... , ] == OR + """ + con_or_true = False + for con in constraints: + #print "con: %s" % con + if len(con.keys()) == 0: + con_and_true = False + else: + con_and_true = True + + for key in con.keys(): + #print "looking at key: %s" % key + if first(key) in data: + value_re = re.compile(con[key]) + if type([]) == type(get(data,key)): + local_or_true = False + for val in get(data,key): + local_or_true = local_or_true | (value_re.search(val) is not None) + con_and_true = con_and_true & local_or_true + else: + if get(data,key) is not None: + con_and_true = con_and_true & (value_re.search(get(data,key)) is not None) + elif first(key) not in data: + print "missing key %s" % first(key) con_or_true = con_or_true | con_and_true @@ -186,45 +254,52 @@ def query_to_dict(query): return ad def pcu_in(fbdata): - if 'plcnode' in fbdata: - if 'pcu_ids' in fbdata['plcnode']: - if len(fbdata['plcnode']['pcu_ids']) > 0: + #if 'plcnode' in fbdata: + if 'plc_node_stats' in fbdata: + if fbdata['plc_node_stats'] and 'pcu_ids' in fbdata['plc_node_stats']: + if len(fbdata['plc_node_stats']['pcu_ids']) > 0: return True return False def pcu_select(str_query, nodelist=None): - global fb - global fbpcu pcunames = [] nodenames = [] if str_query is None: return (nodenames, pcunames) - if fb is None: - fb = database.dbLoad("findbad") - if fbpcu is None: - fbpcu = database.dbLoad("findbadpcus") + if True: + fbquery = FindbadNodeRecord.get_all_latest() + fb_nodelist = [ n.hostname for n in fbquery ] + if True: + fbpcuquery = FindbadPCURecord.get_all_latest() + fbpcu_list = [ p.plc_pcuid for p in fbpcuquery ] - #print str_query dict_query = query_to_dict(str_query) - #print dict_query - - for node in fb['nodes'].keys(): - if nodelist is not None: - if node not in nodelist: continue - - fb_nodeinfo = fb['nodes'][node]['values'] - if pcu_in(fb_nodeinfo): - pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values'] - if verify(dict_query, pcuinfo): - nodenames.append(node) - str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \ - (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password']) - #pcunames.append(str) - pcunames.append(pcuinfo['pcu_id']) + print "dict_query", dict_query + print 'length %s' % len(fbpcuquery.all()) + + for pcurec in fbpcuquery: + pcuinfo = pcurec.to_dict() + if verify(dict_query, pcuinfo): + #nodenames.append(noderec.hostname) + #print 'appending %s' % pcuinfo['plc_pcuid'] + pcunames.append(pcuinfo['plc_pcuid']) + + #for noderec in fbquery: + # if nodelist is not None: + # if noderec.hostname not in nodelist: continue +# +# fb_nodeinfo = noderec.to_dict() +# if pcu_in(fb_nodeinfo): +# pcurec = FindbadPCURecord.get_latest_by(plc_pcuid=get(fb_nodeinfo, +# 'plc_node_stats.pcu_ids')[0]).first() +# if pcurec: +# pcuinfo = pcurec.to_dict() +# if verify(dict_query, pcuinfo): +# nodenames.append(noderec.hostname) +# pcunames.append(pcuinfo['plc_pcuid']) return (nodenames, pcunames) -def node_select(str_query, nodelist=None, fbdb=None): - global fb +def node_select(str_query, nodelist=None, fb=None): hostnames = [] if str_query is None: return hostnames @@ -233,42 +308,43 @@ def node_select(str_query, nodelist=None, fbdb=None): dict_query = query_to_dict(str_query) #print dict_query - if fbdb is not None: - fb = fbdb - - for node in fb['nodes'].keys(): - if nodelist is not None: - if node not in nodelist: continue - - fb_nodeinfo = fb['nodes'][node]['values'] + for node in nodelist: + #if nodelist is not None: + # if node not in nodelist: continue - if fb_nodeinfo == []: - #print node, "has lost values" + try: + fb_noderec = None + #fb_noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==node).order_by(FindbadNodeRecord.date_checked.desc()).first() + fb_noderec = FindbadNodeRecord.get_latest_by(hostname=node) + except: + print traceback.print_exc() continue - #sys.exit(1) - fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo) - fb_nodeinfo['hostname'] = node - if 'plcnode' in fb_nodeinfo: - fb_nodeinfo.update(fb_nodeinfo['plcnode']) - - if verify(dict_query, fb_nodeinfo): - #print node #fb_nodeinfo - hostnames.append(node) - else: - #print "NO MATCH", node - pass + + if fb_noderec: + fb_nodeinfo = fb_noderec.to_dict() + + #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo) + #if 'plcnode' in fb_nodeinfo: + # fb_nodeinfo.update(fb_nodeinfo['plcnode']) + + #if verifyDBrecord(dict_query, fb_nodeinfo): + if verify(dict_query, fb_nodeinfo): + #print fb_nodeinfo.keys() + #print node #fb_nodeinfo + hostnames.append(node) + else: + #print "NO MATCH", node + pass return hostnames def main(): - global fb - global fbpcu - import parser as parsermodule + from monitor import parser as parsermodule parser = parsermodule.getParser() - parser.set_defaults(node=None, fromtime=None, select=None, list=None, + parser.set_defaults(node=None, fromtime=None, select=None, list=None, listkeys=False, pcuselect=None, nodelist=None, daysdown=None, fields=None) parser.add_option("", "--daysdown", dest="daysdown", action="store_true", help="List the node state and days down...") @@ -282,6 +358,8 @@ def main(): help="List all nodes with the given key=value pattern") parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", help="A list of nodes to bring out of debug mode.") + parser.add_option("", "--listkeys", dest="listkeys", action="store_true", + help="A list of nodes to bring out of debug mode.") parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD", help="Specify a starting date from which to begin the query.") @@ -300,15 +378,19 @@ def main(): os.chdir("..") fb = archive.load(file[:-4]) else: - fb = database.dbLoad("findbad") + #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed() + fb = None - fbpcu = database.dbLoad("findbadpcus") - reboot.fb = fbpcu + #reboot.fb = fbpcu if config.nodelist: nodelist = util.file.getListFromFile(config.nodelist) else: - nodelist = fb['nodes'].keys() + # NOTE: list of nodes should come from findbad db. Otherwise, we + # don't know for sure that there's a record in the db.. + plcnodes = plccache.l_nodes + nodelist = [ node['hostname'] for node in plcnodes ] + #nodelist = ['planetlab-1.cs.princeton.edu'] pculist = None if config.select is not None and config.pcuselect is not None: @@ -326,10 +408,23 @@ def main(): for node in nodelist: config.node = node - if node not in fb['nodes']: + if node not in nodelist: continue - fb_nodeinfo = fb['nodes'][node]['values'] + try: + # Find the most recent record + fb_noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==node).order_by(FindbadNodeRecord.date_checked.desc()).first() + except: + print traceback.print_exc() + pass + + if config.listkeys: + fb_nodeinfo = fb_noderec.to_dict() + print "Primary keys available in the findbad object:" + for key in fb_nodeinfo.keys(): + print "\t",key + sys.exit(0) + if config.list: print node @@ -337,6 +432,7 @@ def main(): if config.daysdown: daysdown_print_nodeinfo(fb_nodeinfo, node) else: + fb_nodeinfo = fb_noderec.to_dict() if config.select: if config.fields: fields = config.fields.split(",")