The most current version of everything.
[monitor.git] / findbad.py
index e08b554..141f9ac 100755 (executable)
@@ -9,7 +9,7 @@ import time
 # QUERY all nodes.
 COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
                                        "table=table_nodeview&" + \
-                                   "dumpcols='name,resptime,sshstatus,uptime,lastcotop'&" + \
+                                   "dumpcols='name,resptime,sshstatus,uptime,lastcotop,cpuspeed,memsize,disksize'&" + \
                                    "formatcsv"
                                    #"formatcsv&" + \
                                        #"select='lastcotop!=0'"
@@ -25,6 +25,7 @@ import soltesz
 import comon
 import threadpool
 import syncplcdb
+from nodequery import verify,query_to_dict,node_select
 
 import plc
 import auth
@@ -33,7 +34,7 @@ api = plc.PLC(auth.auth, auth.plc)
 def collectPingAndSSH(nodename, cohash):
        ### RUN PING ######################
        ping = soltesz.CMD()
-       (oval,eval) = ping.run_noexcept("ping -c 1 -q %s | grep rtt" % nodename)
+       (oval,errval) = ping.run_noexcept("ping -c 1 -q %s | grep rtt" % nodename)
 
        values = {}
 
@@ -43,31 +44,62 @@ def collectPingAndSSH(nodename, cohash):
        else:
                values['ping'] = "PING"
 
-       #uptime = soltesz.SSH('root', nodename)
-       #(oval,eval) = uptime.run_noexcept("uptime | awk '{print $3,$4}' | tr , ' '")
+       try:
+               for port in [22, 806]: 
+                       ssh = soltesz.SSH('root', nodename, port)
+
+                       (oval, errval) = ssh.run_noexcept2(""" <<\EOF
+                               echo "{"
+                               echo '  "kernel":"'`uname -a`'",'
+                               echo '  "bmlog":"'`ls /tmp/bm.log`'",'
+                               echo '  "bootcd":"'`cat /mnt/cdrom/bootme/ID`'",'
+                               echo '  "nm":"'`ps ax | grep nm.py | grep -v grep`'",'
+                               echo '  "princeton_comon":"'`ls -d /vservers/princeton_comon`'",'
+
+                               ID=`grep princeton_comon /etc/passwd | awk -F : '{if ( $3 > 500 ) { print $3}}'` 
+
+                               echo '  "princeton_comon_running":"'`ls -d /proc/virtual/$ID`'",'
+                               echo '  "princeton_comon_procs":"'`vps ax | grep $ID | grep -v grep | wc -l`'",'
+                               echo "}"
+EOF                    """)
+                       
+                       if len(oval) > 0:
+                               values.update(eval(oval))
+                               values['sshport'] = port
+                               break
+                       else:
+                               values.update({'kernel': "", 'bmlog' : "", 'bootcd' : '', 'nm' :
+                               '', 'princeton_comon' : '', 'princeton_comon_running' : '',
+                               'princeton_comon_procs' : '', 'sshport' : None})
+       except:
+               import traceback; print traceback.print_exc()
+               sys.exit(1)
 
        ### RUN SSH ######################
        b_getbootcd_id = True
-       ssh = soltesz.SSH('root', nodename)
-       oval = ""
-       eval = ""
-       (oval, eval) = ssh.run_noexcept('echo `uname -a ; ls /tmp/bm.log`')
-       val = oval
+       #ssh = soltesz.SSH('root', nodename)
+       #oval = ""
+       #errval = ""
+       #(oval, errval) = ssh.run_noexcept('echo `uname -a ; ls /tmp/bm.log`')
+
+       oval = values['kernel']
        if "2.6.17" in oval or "2.6.2" in oval:
                values['ssh'] = 'SSH'
                values['category'] = 'ALPHA'
-               if "bm.log" in oval:
+               if "bm.log" in values['bmlog']:
                        values['state'] = 'DEBUG'
                else:
                        values['state'] = 'BOOT'
        elif "2.6.12" in oval or "2.6.10" in oval:
                values['ssh'] = 'SSH'
                values['category'] = 'PROD'
-               if "bm.log" in oval:
+               if "bm.log" in values['bmlog']:
                        values['state'] = 'DEBUG'
                else:
                        values['state'] = 'BOOT'
-       elif "2.4" in oval:
+       
+       # NOTE: on 2.6.8 kernels, with 4.2 bootstrapfs, the chroot command fails.  I have no idea why.
+       elif "2.4" in oval or "2.6.8" in oval:
                b_getbootcd_id = False
                values['ssh'] = 'SSH'
                values['category'] = 'OLDBOOTCD'
@@ -75,7 +107,7 @@ def collectPingAndSSH(nodename, cohash):
        elif oval != "":
                values['ssh'] = 'SSH'
                values['category'] = 'UNKNOWN'
-               if "bm.log" in oval:
+               if "bm.log" in values['bmlog']:
                        values['state'] = 'DEBUG'
                else:
                        values['state'] = 'BOOT'
@@ -85,17 +117,18 @@ def collectPingAndSSH(nodename, cohash):
                values['ssh'] = 'NOSSH'
                values['category'] = 'ERROR'
                values['state'] = 'DOWN'
-               val = eval.strip()
+               val = errval.strip()
+               values['kernel'] = val
 
-       values['kernel'] = val
+       #values['kernel'] = val
 
        if b_getbootcd_id:
                # try to get BootCD for all nodes that are not 2.4 nor inaccessible
-               (oval, eval) = ssh.run_noexcept('cat /mnt/cdrom/bootme/ID')
-               val = oval
-               if "BootCD" in val:
-                       values['bootcd'] = val
-                       if "v2" in val and \
+               #(oval, errval) = ssh.run_noexcept('cat /mnt/cdrom/bootme/ID')
+               oval = values['bootcd']
+               if "BootCD" in oval:
+                       values['bootcd'] = oval
+                       if "v2" in oval and \
                                ( nodename is not "planetlab1.cs.unc.edu" and \
                                  nodename is not "planetlab2.cs.unc.edu" ):
                                values['category'] = 'OLDBOOTCD'
@@ -106,6 +139,41 @@ def collectPingAndSSH(nodename, cohash):
 
        # TODO: get bm.log for debug nodes.
        # 'zcat /tmp/bm.log'
+       
+       #(oval, errval) = ssh.run_noexcept('ps ax | grep nm.py | grep -v grep')
+       oval = values['nm']
+       if "nm.py" in oval:
+               values['nm'] = "Y"
+       else:
+               values['nm'] = "N"
+
+       continue_slice_check = True
+       #(oval, errval) = ssh.run_noexcept('ls -d /vservers/princeton_comon')
+       oval = values['princeton_comon']
+       if "princeton_comon" in oval:
+               values['princeton_comon'] = "Y"
+       else:
+               values['princeton_comon'] = "N"
+               continue_slice_check = False
+
+       if continue_slice_check:
+               #(oval, errval) = ssh.run_noexcept('ID=`grep princeton_comon /etc/passwd | awk -F : "{if ( \\\$3 > 500 ) { print \\\$3}}"`; ls -d /proc/virtual/$ID')
+               oval = values['princeton_comon_running']
+               if len(oval) > len('/proc/virtual/'):
+                       values['princeton_comon_running'] = "Y"
+               else:
+                       values['princeton_comon_running'] = "N"
+                       continue_slice_check = False
+       else:
+               values['princeton_comon_running'] = "-"
+               
+       if continue_slice_check:
+               #(oval, errval) = ssh.run_noexcept('ID=`grep princeton_comon /etc/passwd | awk -F : "{if ( \\\$3 > 500 ) { print \\\$3}}"`; vps ax | grep $ID | grep -v grep | wc -l')
+               oval = values['princeton_comon_procs']
+               values['princeton_comon_procs'] = oval
+       else:
+               values['princeton_comon_procs'] = "-"
+
                
        if nodename in cohash: 
                values['comonstats'] = cohash[nodename]
@@ -196,7 +264,8 @@ def recordPingAndSSH(request, result):
 
                count += 1
                print "%d %s %s" % (count, nodename, externalState['nodes'][nodename]['values'])
-               soltesz.dbDump(config.dbname, externalState)
+               if count % 20 == 0:
+                       soltesz.dbDump(config.dbname, externalState)
 
 # this will be called when an exception occurs within a thread
 def handle_exception(request, result):
@@ -242,6 +311,8 @@ def checkAndRecordState(l_nodes, cohash):
                        print "All results collected."
                        break
 
+       soltesz.dbDump(config.dbname, externalState)
+
 
 
 def main():
@@ -259,6 +330,7 @@ def main():
        cotop_url = COMON_COTOPURL
 
        # history information for all nodes
+       #cohash = {}
        cohash = cotop.coget(cotop_url)
        l_nodes = syncplcdb.create_plcdb()
        if config.filename:
@@ -270,9 +342,17 @@ def main():
        elif config.nodegroup:
                ng = api.GetNodeGroups({'name' : config.nodegroup})
                l_nodes = api.GetNodes(ng[0]['node_ids'])
-
+       elif config.site:
+               site = api.GetSites(config.site)
+               l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
+               
        l_nodes = [node['hostname'] for node in l_nodes]
 
+       # perform this query after the above options, so that the filter above
+       # does not break.
+       if config.nodeselect:
+               l_nodes = node_select(config.nodeselect)
+
        print "fetching %s hosts" % len(l_nodes)
 
        checkAndRecordState(l_nodes, cohash)
@@ -284,13 +364,18 @@ 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.set_defaults(filename=None, node=None, site=None, nodeselect=False, 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("", "--nodeselect", dest="nodeselect", metavar="query string", 
+                                               help="Provide a selection string to return a node list.")
        parser.add_option("", "--nodegroup", dest="nodegroup", metavar="FILE", 
                                                help="Provide the nodegroup for the list of nodes.")
+       parser.add_option("", "--site", dest="site", metavar="site name",
+                                               help="Specify a site to view node status")
 
        parser.add_option("", "--cachenodes", action="store_true",
                                                help="Cache node lookup from PLC")