merge from 2.0 branch
[monitor.git] / nodequery.py
index 5287e48..e9001a6 100755 (executable)
@@ -3,8 +3,8 @@
 
 import sys
 from monitor import database
-from nodecommon import *
-from unified_model import Record
+from monitor.common import *
+from monitor.model import Record
 import glob
 import os
 import traceback
@@ -13,12 +13,10 @@ import time
 import re
 import string
 
-from monitor.pcu import reboot
 from monitor.wrapper import plc, plccache
 api = plc.getAuthAPI()
 
-from monitor.database.infovacuum import FindbadNodeRecordSync, FindbadNodeRecord
-from monitor.database.dborm import mon_session as session
+from monitor.database.info.model import FindbadNodeRecord, FindbadPCURecord, session
 from monitor import util
 from monitor import config
 
@@ -76,10 +74,10 @@ def get(fb, path):
     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):
@@ -255,9 +253,10 @@ 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
 
@@ -270,24 +269,35 @@ def pcu_select(str_query, nodelist=None):
                fbquery = FindbadNodeRecord.get_all_latest()
                fb_nodelist = [ n.hostname for n in fbquery ]
        if True:
+               # NOTE: this doesn't work when there are only a few records current.
+               # pcu_select should apply to all pcus globally, not just the most recent records.
                fbpcuquery = FindbadPCURecord.get_all_latest()
                fbpcu_list = [ p.plc_pcuid for p in fbpcuquery ]
 
        dict_query = query_to_dict(str_query)
-
-       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])
-                       pcuinfo = pcurec.to_dict()
-                       if verify(dict_query, pcuinfo):
-                               nodenames.append(noderec.hostname)
-                               str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
-                                                       (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
-                               pcunames.append(pcuinfo['plc_pcuid'])
+       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, fb=None):
@@ -372,8 +382,6 @@ def main():
                #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
                fb = None
 
-       #reboot.fb = fbpcu
-
        if config.nodelist:
                nodelist = util.file.getListFromFile(config.nodelist)
        else:
@@ -404,13 +412,14 @@ def main():
 
                try:
                        # Find the most recent record
-                       fb_noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==node).order_by(FindbadNodeRecord.date_checked.desc()).first()
+                       fb_noderec = FindbadNodeRecord.get_latest_by(hostname=node) 
+                       if not fb_noderec: continue
+                       fb_nodeinfo = fb_noderec.to_dict()
                except:
                        print traceback.print_exc()
-                       pass
+                       continue
 
                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
@@ -423,7 +432,6 @@ 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(",")