use cached values more consistently.
[monitor.git] / monitor / scanapi.py
index 8097bac..f7939e6 100644 (file)
@@ -20,7 +20,7 @@ from monitor.sources import comon
 from monitor.wrapper import plc, plccache
 
 import traceback
-from monitor.common import nmap_port_status
+from monitor.common import nmap_port_status, email_exception
 
 COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
                        "table=table_nodeview&" + \
@@ -112,7 +112,7 @@ class ScanInterface(object):
        syncclass = None
        primarykey = 'hostname'
 
-       def __init__(self, round):
+       def __init__(self, round=1):
                self.round = round
                self.count = 1
 
@@ -133,34 +133,38 @@ class ScanInterface(object):
                try:
                        if values is None:
                                return
-
-                       fbnodesync = self.syncclass.findby_or_create(
-                                                                                               if_new_set={'round' : self.round},
+                       
+                       if self.syncclass:
+                               fbnodesync = self.syncclass.findby_or_create(
+                                                                                               #if_new_set={'round' : self.round},
                                                                                                **{ self.primarykey : nodename})
                        # NOTE: This code will either add a new record for the new self.round, 
                        #       OR it will find the previous value, and update it with new information.
                        #       The data that is 'lost' is not that important, b/c older
                        #       history still exists.  
                        fbrec = self.recordclass.findby_or_create(
-                                               **{'round':self.round, self.primarykey:nodename})
+                                               **{ self.primarykey:nodename})
 
                        fbrec.set( **values ) 
 
                        fbrec.flush()
-                       fbnodesync.round = self.round
-                       fbnodesync.flush()
+                       if self.syncclass:
+                               fbnodesync.round = self.round
+                               fbnodesync.flush()
 
                        print "%d %s %s" % (self.count, nodename, values)
                        self.count += 1
 
                except:
                        print "ERROR:"
+                       email_exception(nodename)
                        print traceback.print_exc()
                        pass
 
 class ScanNodeInternal(ScanInterface):
        recordclass = FindbadNodeRecord
-       syncclass = FindbadNodeRecordSync
+       #syncclass = FindbadNodeRecordSync
+       syncclass = None
        primarykey = 'hostname'
 
        def collectNMAP(self, nodename, cohash):
@@ -208,7 +212,7 @@ class ScanNodeInternal(ScanInterface):
                                                echo '  "princeton_comon_running":"'`ls -d /proc/virtual/$ID`'",'
                                                echo '  "princeton_comon_procs":"'`vps ax | grep $ID | grep -v grep | wc -l`'",'
                                                echo "}"
-       EOF                             """)
+EOF                            """)
                                        
                                        values['ssh_error'] = errval
                                        if len(oval) > 0:
@@ -331,9 +335,10 @@ class ScanNodeInternal(ScanInterface):
                        plc_lock.acquire()
                        d_node = None
                        try:
-                               d_node = plc.getNodes({'hostname': nodename}, ['pcu_ids', 'site_id', 
-                                                                               'date_created', 'last_updated', 
-                                                                               'last_contact', 'boot_state', 'nodegroup_ids'])[0]
+                               d_node = plccache.GetNodeByName(nodename)
+                               #d_node = plc.getNodes({'hostname': nodename}, ['pcu_ids', 'site_id', 
+                               #                                               'date_created', 'last_updated', 
+                               #                                               'last_contact', 'boot_state', 'nodegroup_ids'])[0]
                        except:
                                traceback.print_exc()
                        plc_lock.release()
@@ -360,8 +365,9 @@ class ScanNodeInternal(ScanInterface):
                        d_site = None
                        values['loginbase'] = ""
                        try:
-                               d_site = plc.getSites({'site_id': site_id}, 
-                                                                       ['max_slices', 'slice_ids', 'node_ids', 'login_base'])[0]
+                               d_site = plccache.GetSitesById([ site_id ])[0]
+                               #d_site = plc.getSites({'site_id': site_id}, 
+                               #                                       ['max_slices', 'slice_ids', 'node_ids', 'login_base'])[0]
                                values['loginbase'] = d_site['login_base']
                        except:
                                traceback.print_exc()
@@ -375,9 +381,9 @@ class ScanNodeInternal(ScanInterface):
                return (nodename, values)
 
 def internalprobe(hostname):
-       fbsync = FindbadNodeRecordSync.findby_or_create(hostname="global", 
-                                                                                                       if_new_set={'round' : 1})
-       scannode = ScanNodeInternal(fbsync.round)
+       #fbsync = FindbadNodeRecordSync.findby_or_create(hostname="global", 
+       #                                                                                               if_new_set={'round' : 1})
+       scannode = ScanNodeInternal() # fbsync.round)
        try:
                (nodename, values) = scannode.collectInternal(hostname, {})
                scannode.record(None, (nodename, values))
@@ -388,9 +394,9 @@ def internalprobe(hostname):
                return False
 
 def externalprobe(hostname):
-       fbsync = FindbadNodeRecordSync.findby_or_create(hostname="global", 
-                                                                                                       if_new_set={'round' : 1})
-       scannode = ScanNodeInternal(fbsync.round)
+       #fbsync = FindbadNodeRecordSync.findby_or_create(hostname="global", 
+       #                                                                                               if_new_set={'round' : 1})
+       scannode = ScanNodeInternal() # fbsync.round)
        try:
                (nodename, values) = scannode.collectNMAP(hostname, {})
                scannode.record(None, (nodename, values))
@@ -402,7 +408,7 @@ def externalprobe(hostname):
 
 class ScanPCU(ScanInterface):
        recordclass = FindbadPCURecord
-       syncclass = FindbadPCURecordSync
+       syncclass = None
        primarykey = 'plc_pcuid'
 
        def collectInternal(self, pcuname, cohash):