(no commit message)
[monitor.git] / findbad.py
index d1ee263..e08b554 100755 (executable)
@@ -5,20 +5,6 @@ import sys
 import string
 import time
 
-from config import config
-from optparse import OptionParser
-parser = OptionParser()
-parser.set_defaults(filename="", increment=False, dbname="findbadnodes", cachenodes=False)
-parser.add_option("-f", "--nodes", dest="filename", metavar="FILE", 
-                                       help="Provide the input file for the node list")
-parser.add_option("", "--cachenodes", action="store_true",
-                                       help="Cache node lookup from PLC")
-parser.add_option("", "--dbname", dest="dbname", metavar="FILE", 
-                                       help="Specify the name of the database to which the information is saved")
-parser.add_option("-i", "--increment", action="store_true", dest="increment", 
-                                       help="Increment round number to force refresh or retry")
-config = config(parser)
-config.parse_args()
 
 # QUERY all nodes.
 COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
@@ -36,11 +22,14 @@ count = 0
 
 
 import soltesz
-import plc
 import comon
 import threadpool
 import syncplcdb
 
+import plc
+import auth
+api = plc.PLC(auth.auth, auth.plc)
+
 def collectPingAndSSH(nodename, cohash):
        ### RUN PING ######################
        ping = soltesz.CMD()
@@ -54,6 +43,9 @@ def collectPingAndSSH(nodename, cohash):
        else:
                values['ping'] = "PING"
 
+       #uptime = soltesz.SSH('root', nodename)
+       #(oval,eval) = uptime.run_noexcept("uptime | awk '{print $3,$4}' | tr , ' '")
+
        ### RUN SSH ######################
        b_getbootcd_id = True
        ssh = soltesz.SSH('root', nodename)
@@ -61,13 +53,12 @@ def collectPingAndSSH(nodename, cohash):
        eval = ""
        (oval, eval) = ssh.run_noexcept('echo `uname -a ; ls /tmp/bm.log`')
        val = oval
-       if "2.6.17" in oval or "2.6.20" in oval:
+       if "2.6.17" in oval or "2.6.2" in oval:
                values['ssh'] = 'SSH'
+               values['category'] = 'ALPHA'
                if "bm.log" in oval:
-                       values['category'] = 'ALPHA'
                        values['state'] = 'DEBUG'
                else:
-                       values['category'] = 'ALPHA'
                        values['state'] = 'BOOT'
        elif "2.6.12" in oval or "2.6.10" in oval:
                values['ssh'] = 'SSH'
@@ -104,7 +95,9 @@ def collectPingAndSSH(nodename, cohash):
                val = oval
                if "BootCD" in val:
                        values['bootcd'] = val
-                       if "v2" in val and nodename is not "planetlab1.cs.unc.edu":
+                       if "v2" in val and \
+                               ( nodename is not "planetlab1.cs.unc.edu" and \
+                                 nodename is not "planetlab2.cs.unc.edu" ):
                                values['category'] = 'OLDBOOTCD'
                else:
                        values['bootcd'] = ""
@@ -127,8 +120,10 @@ def collectPingAndSSH(nodename, cohash):
        plc_lock.acquire()
 
        try:
-               d_node = plc.getNodes({'hostname': nodename}, ['pcu_ids', 'site_id', 'last_contact', 'boot_state'])
+               d_node = plc.getNodes({'hostname': nodename}, ['pcu_ids', 'site_id', 'last_contact', 'boot_state', 'nodegroup_ids'])
        except:
+               b_except = True
+               import traceback
                b_except = True
                import traceback
                traceback.print_exc()
@@ -145,10 +140,12 @@ def collectPingAndSSH(nodename, cohash):
                        values['pcu'] = "NOPCU"
                site_id = d_node[0]['site_id']
                last_contact = d_node[0]['last_contact']
+               nodegroups = d_node[0]['nodegroup_ids']
                values['plcnode'] = {'status' : 'SUCCESS', 
                                                        'pcu_ids': pcu, 
                                                        'boot_state' : d_node[0]['boot_state'],
                                                        'site_id': site_id,
+                                                       'nodegroups' : nodegroups,
                                                        'last_contact': last_contact}
        else:
                values['pcu']     = "UNKNOWN"
@@ -183,6 +180,8 @@ def collectPingAndSSH(nodename, cohash):
        else:
                values['plcsite'] = {'status' : "GS_FAILED"}
 
+       values['checked'] = time.time()
+
        return (nodename, values)
 
 def recordPingAndSSH(request, result):
@@ -261,13 +260,20 @@ def main():
 
        # history information for all nodes
        cohash = cotop.coget(cotop_url)
+       l_nodes = syncplcdb.create_plcdb()
+       if config.filename:
+               f_nodes = config.getListFromFile(config.filename)
+               l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
+       elif config.node:
+               f_nodes = [config.node]
+               l_nodes = filter(lambda x: x['hostname'] in f_nodes, l_nodes)
+       elif config.nodegroup:
+               ng = api.GetNodeGroups({'name' : config.nodegroup})
+               l_nodes = api.GetNodes(ng[0]['node_ids'])
 
-       if config.filename == "":
-               l_nodes = syncplcdb.create_plcdb()
-               l_nodes = [node['hostname'] for node in l_nodes]
-               #l_nodes = cohash.keys()
-       else:
-               l_nodes = config.getListFromFile(config.filename)
+       l_nodes = [node['hostname'] for node in l_nodes]
+
+       print "fetching %s hosts" % len(l_nodes)
 
        checkAndRecordState(l_nodes, cohash)
 
@@ -275,9 +281,31 @@ def main():
 
 
 if __name__ == '__main__':
+       from config import config
+       from optparse import OptionParser
+       parser = OptionParser()
+       parser.set_defaults(filename=None, node=None, nodegroup=None, increment=False, dbname="findbadnodes", cachenodes=False)
+       parser.add_option("", "--node", dest="node", metavar="hostname", 
+                                               help="Provide a single node to operate on")
+       parser.add_option("-f", "--nodelist", dest="filename", metavar="FILE", 
+                                               help="Provide the input file for the node list")
+       parser.add_option("", "--nodegroup", dest="nodegroup", metavar="FILE", 
+                                               help="Provide the nodegroup for the list of nodes.")
+
+       parser.add_option("", "--cachenodes", action="store_true",
+                                               help="Cache node lookup from PLC")
+       parser.add_option("", "--dbname", dest="dbname", metavar="FILE", 
+                                               help="Specify the name of the database to which the information is saved")
+       parser.add_option("-i", "--increment", action="store_true", dest="increment", 
+                                               help="Increment round number to force refresh or retry")
+       config = config(parser)
+       config.parse_args()
+
        try:
                main()
        except Exception, err:
+               import traceback
+               print traceback.print_exc()
                print "Exception: %s" % err
                print "Saving data... exitting."
                soltesz.dbDump(config.dbname, externalState)