updates to monitor-server.spec and notes in 'todo' based on first attempt to
[monitor.git] / nodequery.py
index 465c309..b030344 100755 (executable)
@@ -1,28 +1,31 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import sys
-import soltesz
+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 util.file
 
 import time
 import re
 
 #fb = {}
-fb = soltesz.dbLoad("findbad")
+fb = database.dbLoad("findbad")
 fbpcu = {}
 
+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
 
@@ -49,6 +52,79 @@ def fb_print_nodeinfo(fbnode, hostname, fields=None):
                        format += "%%(%s)s " % f
                print format % fbnode
 
+def get(fb, path):
+    indexes = path.split("/")
+    values = fb
+    for index in indexes:
+        if index in values:
+            values = values[index]
+        else:
+            raise NoKeyException(index)
+    return values
+
+def verifyType(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 data is None:
+                               con_and_true = False
+                               break
+
+                       try:
+                               get(data,key)
+                               o = con[key]
+                               if o.name() == "Match":
+                                       if get(data,key) is not None:
+                                               value_re = re.compile(o.value)
+                                               con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
+                                       else:
+                                               con_and_true = False
+                               elif o.name() == "ListMatch":
+                                       if get(data,key) is not None:
+                                               match = False
+                                               for listitem in get(data,key):
+                                                       value_re = re.compile(o.value)
+                                                       if value_re.search(listitem) is not None:
+                                                               match = True
+                                                               break
+                                               con_and_true = con_and_true & match
+                                       else:
+                                               con_and_true = False
+                               elif o.name() == "Is":
+                                       con_and_true = con_and_true & (get(data,key) == o.value)
+                               elif o.name() == "FilledIn":
+                                       con_and_true = con_and_true & (len(get(data,key)) > 0)
+                               elif o.name() == "PortOpen":
+                                       if get(data,key) is not None:
+                                               v = get(data,key)
+                                               con_and_true = con_and_true & (v[str(o.value)] == "open")
+                                       else:
+                                               con_and_true = False
+                               else:
+                                       value_re = re.compile(o.value)
+                                       con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
+
+                       except NoKeyException, key:
+                               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.
@@ -95,7 +171,7 @@ def query_to_dict(query):
        
        return ad
 
-def _pcu_in(fbdata):
+def pcu_in(fbdata):
        if 'plcnode' in fbdata:
                if 'pcu_ids' in fbdata['plcnode']:
                        if len(fbdata['plcnode']['pcu_ids']) > 0:
@@ -116,7 +192,7 @@ def pcu_select(str_query, nodelist=None):
                        if node not in nodelist: continue
        
                fb_nodeinfo  = fb['nodes'][node]['values']
-               if _pcu_in(fb_nodeinfo):
+               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)
@@ -125,7 +201,7 @@ def pcu_select(str_query, nodelist=None):
                                pcunames.append(str)
        return (nodenames, pcunames)
 
-def node_select(str_query, nodelist=None):
+def node_select(str_query, nodelist=None, fbdb=None):
        hostnames = []
        if str_query is None: return hostnames
 
@@ -134,6 +210,9 @@ def node_select(str_query, nodelist=None):
        #print dict_query
        global fb
 
+       if fbdb is not None:
+               fb = fbdb
+
        for node in fb['nodes'].keys():
                if nodelist is not None: 
                        if node not in nodelist: continue
@@ -163,10 +242,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", 
@@ -181,12 +261,13 @@ 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"
-               archive = soltesz.SPickle(path)
+               archive = database.SPickle(path)
                d = datetime_fromstr(config.fromtime)
                glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
                os.chdir(path)
@@ -196,12 +277,12 @@ def main():
                os.chdir("..")
                fb = archive.load(file[:-4])
        else:
-               fb = soltesz.dbLoad("findbad")
+               fb = database.dbLoad("findbad")
 
-       fbpcu = soltesz.dbLoad("findbadpcus")
+       fbpcu = database.dbLoad("findbadpcus")
 
        if config.nodelist:
-               nodelist = config.getListFromFile(config.nodelist)
+               nodelist = util.file.getListFromFile(config.nodelist)
        else:
                nodelist = fb['nodes'].keys()
 
@@ -214,7 +295,6 @@ def main():
        elif config.pcuselect is not None:
                nodelist, pculist = pcu_select(config.pcuselect, nodelist)
 
-
        if pculist:
                for pcu in pculist:
                        print pcu