changes for 3.0
[monitor.git] / nodequery.py
index a982e20..16c0bad 100755 (executable)
@@ -6,24 +6,27 @@ api = plc.getAuthAPI()
 import sys
 import database
 from nodecommon import *
-from policy import Diagnose
+#from policy import Diagnose
+from unified_model import Record
 import glob
 import os
 from reboot import pcu_name
+import reboot
+import util.file
 
 import time
 import re
 
 #fb = {}
-fb = database.dbLoad("findbad")
-fbpcu = {}
+fb = None
+fbpcu = None
 
 class NoKeyException(Exception): pass
 
 def daysdown_print_nodeinfo(fbnode, hostname):
        fbnode['hostname'] = hostname
-       fbnode['daysdown'] = Diagnose.getStrDaysDown(fbnode)
-       fbnode['intdaysdown'] = Diagnose.getDaysDown(fbnode)
+       fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
+       fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
 
        print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
 
@@ -43,7 +46,14 @@ def fb_print_nodeinfo(fbnode, hostname, fields=None):
                        fbnode['kernel'] = fbnode['kernel'].split()[2]
                fbnode['boot_state'] = fbnode['plcnode']['boot_state']
 
-               print "%(hostname)-39s | %(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
+               try:
+                       if len(fbnode['nodegroups']) > 0:
+                               fbnode['category'] = fbnode['nodegroups'][0]
+               except:
+                       #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
        else:
                format = ""
                for f in fields:
@@ -140,7 +150,13 @@ def verify(constraints, data):
                        #print "looking at key: %s" % key
                        if key in data: 
                                value_re = re.compile(con[key])
-                               con_and_true = con_and_true & (value_re.search(data[key]) is not None)
+                               if type([]) == type(data[key]):
+                                       local_or_true = False
+                                       for val in 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:
+                                       con_and_true = con_and_true & (value_re.search(data[key]) is not None)
                        elif key not in data:
                                print "missing key %s" % key,
                                pass
@@ -177,10 +193,17 @@ def pcu_in(fbdata):
        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")
+
        #print str_query
        dict_query = query_to_dict(str_query)
        #print dict_query
@@ -196,17 +219,19 @@ def pcu_select(str_query, nodelist=None):
                                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(str)
+                               pcunames.append(pcuinfo['pcu_id'])
        return (nodenames, pcunames)
 
 def node_select(str_query, nodelist=None, fbdb=None):
+       global fb
+
        hostnames = []
        if str_query is None: return hostnames
 
        #print str_query
        dict_query = query_to_dict(str_query)
        #print dict_query
-       global fb
 
        if fbdb is not None:
                fb = fbdb
@@ -240,10 +265,11 @@ def main():
        global fb
        global fbpcu
 
-       from config import config
-       from optparse import OptionParser
-       parser = OptionParser()
-       parser.set_defaults(node=None, fromtime=None, select=None, list=None, pcuselect=None, nodelist=None, daysdown=None, fields=None)
+       import parser as parsermodule
+       parser = parsermodule.getParser()
+
+       parser.set_defaults(node=None, fromtime=None, select=None, list=None, 
+                                               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...")
        parser.add_option("", "--select", dest="select", metavar="key=value", 
@@ -258,8 +284,9 @@ def main():
                                                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.")
-       config = config(parser)
-       config.parse_args()
+
+       parser = parsermodule.getParser(['defaults'], parser)
+       config = parsermodule.parse_args(parser)
        
        if config.fromtime:
                path = "archive-pdb"
@@ -276,22 +303,22 @@ def main():
                fb = database.dbLoad("findbad")
 
        fbpcu = database.dbLoad("findbadpcus")
+       reboot.fb = fbpcu
 
        if config.nodelist:
-               nodelist = config.getListFromFile(config.nodelist)
+               nodelist = util.file.getListFromFile(config.nodelist)
        else:
                nodelist = fb['nodes'].keys()
 
        pculist = None
        if config.select is not None and config.pcuselect is not None:
-               nodelist = node_select(config.select, nodelist)
+               nodelist = node_select(config.select, nodelist, fb)
                nodelist, pculist = pcu_select(config.pcuselect, nodelist)
        elif config.select is not None:
-               nodelist = node_select(config.select, nodelist)
+               nodelist = node_select(config.select, nodelist, fb)
        elif config.pcuselect is not None:
                nodelist, pculist = pcu_select(config.pcuselect, nodelist)
 
-
        if pculist:
                for pcu in pculist:
                        print pcu