changes for 3.0
[monitor.git] / findbadpcu.py
index 017b4c4..114c48b 100755 (executable)
@@ -5,9 +5,14 @@ import sys
 import string
 import time
 import socket
+import util.file
+import plc
+import sets
 
     
 import signal
+import traceback
+from nodequery import pcu_select
 
 #old_handler = signal.getsignal(signal.SIGCHLD)
 
@@ -43,7 +48,8 @@ count = 0
 import reboot
 from reboot import pcu_name
 
-import soltesz
+import database
+import moncommands
 import plc
 import comon
 import threadpool
@@ -73,13 +79,14 @@ def get_pcu(pcuname):
        except:
                try:
                        print "GetPCU from file %s" % pcuname
-                       l_pcus = soltesz.dbLoad("pculist")
+                       l_pcus = database.dbLoad("pculist")
                        for i in l_pcus:
                                if i['pcu_id'] == pcuname:
                                        l_pcu = i
                except:
-                       import traceback
                        traceback.print_exc()
+                       from nodecommon import email_exception
+                       email_exception()
                        l_pcu = None
 
        plc_lock.release()
@@ -92,13 +99,14 @@ def get_nodes(node_ids):
                l_node = plc.getNodes(node_ids, ['hostname', 'last_contact', 'node_id', 'ports'])
        except:
                try:
-                       plc_nodes = soltesz.dbLoad("l_plcnodes")
+                       plc_nodes = database.dbLoad("l_plcnodes")
                        for n in plc_nodes:
                                if n['node_id'] in node_ids:
                                        l_node.append(n)
                except:
-                       import traceback
                        traceback.print_exc()
+                       from nodecommon import email_exception
+                       email_exception()
                        l_node = None
 
        plc_lock.release()
@@ -149,14 +157,15 @@ def get_plc_site_values(site_id):
                        d_site = d_site[0]
        except:
                try:
-                       plc_sites = soltesz.dbLoad("l_plcsites")
+                       plc_sites = database.dbLoad("l_plcsites")
                        for site in plc_sites:
                                if site['site_id'] == site_id:
                                        d_site = site
                                        break
                except:
-                       import traceback
                        traceback.print_exc()
+                       from nodecommon import email_exception
+                       email_exception()
                        values = None
 
        plc_lock.release()
@@ -194,8 +203,9 @@ def collectPingAndSSH(pcuname, cohash):
                                continue_probe = False
                except:
                        b_except = True
-                       import traceback
                        traceback.print_exc()
+                       from nodecommon import email_exception
+                       email_exception()
                        continue_probe = False
 
                if b_except or not continue_probe: return (None, None, None)
@@ -261,7 +271,7 @@ def collectPingAndSSH(pcuname, cohash):
 
                #### RUN NMAP ###############################
                if continue_probe:
-                       nmap = soltesz.CMD()
+                       nmap = moncommands.CMD()
                        (oval,eval) = nmap.run_noexcept("nmap -oG - -P0 -p22,23,80,443,5869,9100,16992 %s | grep Host:" % pcu_name(values))
                        # NOTE: an empty / error value for oval, will still work.
                        (values['portstatus'], continue_probe) = nmap_portstatus(oval)
@@ -289,7 +299,6 @@ def collectPingAndSSH(pcuname, cohash):
                print values
                errors = values
                print "____________________________________"
-               import traceback
                errors['traceback'] = traceback.format_exc()
                print errors['traceback']
 
@@ -310,12 +319,12 @@ def recordPingAndSSH(request, result):
 
                count += 1
                print "%d %s %s" % (count, nodename, externalState['nodes'][pcu_id]['values'])
-               soltesz.dbDump(config.dbname, externalState)
+               database.dbDump(config.dbname, externalState)
 
        if errors is not None:
                pcu_id = "id_%s" % nodename
                errorState[pcu_id] = errors
-               soltesz.dbDump("findbadpcu_errors", errorState)
+               database.dbDump("findbadpcu_errors", errorState)
 
 # this will be called when an exception occurs within a thread
 def handle_exception(request, result):
@@ -329,7 +338,7 @@ def checkAndRecordState(l_pcus, cohash):
        global count
        global_round = externalState['round']
 
-       tp = threadpool.ThreadPool(20)
+       tp = threadpool.ThreadPool(10)
 
        # CREATE all the work requests
        for pcuname in l_pcus:
@@ -353,10 +362,16 @@ def checkAndRecordState(l_pcus, cohash):
                        pass
 
        # WAIT while all the work requests are processed.
+       begin = time.time()
        while 1:
                try:
                        time.sleep(1)
                        tp.poll()
+                       # if more than two hours
+                       if time.time() - begin > (60*60*1):
+                               print "findbadpcus.py has run out of time!!!!!!"
+                               database.dbDump(config.dbname, externalState)
+                               os._exit(1)
                except KeyboardInterrupt:
                        print "Interrupted!"
                        break
@@ -369,19 +384,33 @@ def checkAndRecordState(l_pcus, cohash):
 def main():
        global externalState
 
-       l_pcus = soltesz.if_cached_else_refresh(1, config.refresh, "pculist", lambda : plc.GetPCUs())
-       externalState = soltesz.if_cached_else(1, config.dbname, lambda : externalState) 
+       l_pcus = database.if_cached_else_refresh(1, config.refresh, "pculist", lambda : plc.GetPCUs())
+       externalState = database.if_cached_else(1, config.dbname, lambda : externalState) 
        cohash = {}
 
        if config.increment:
                # update global round number to force refreshes across all nodes
                externalState['round'] += 1
 
-       if config.filename == None and config.pcuid == None:
+       if config.site is not None:
+               api = plc.getAuthAPI()
+               site = api.GetSites(config.site)
+               l_nodes = api.GetNodes(site[0]['node_ids'], ['pcu_ids'])
+               pcus = []
+               for node in l_nodes:
+                       pcus += node['pcu_ids']
+               # clear out dups.
+               l_pcus = [pcu for pcu in sets.Set(pcus)]
+       elif config.pcuselect is not None:
+               n, pcus = pcu_select(config.pcuselect)
+               # clear out dups.
+               l_pcus = [pcu for pcu in sets.Set(pcus)]
+
+       elif config.nodelist == None and config.pcuid == None:
                print "Calling API GetPCUs() : refresh(%s)" % config.refresh
                l_pcus  = [pcu['pcu_id'] for pcu in l_pcus]
-       elif config.filename is not None:
-               l_pcus = config.getListFromFile(config.filename)
+       elif config.nodelist is not None:
+               l_pcus = util.file.getListFromFile(config.nodelist)
                l_pcus = [int(pcu) for pcu in l_pcus]
        elif config.pcuid is not None:
                l_pcus = [ config.pcuid ] 
@@ -401,20 +430,26 @@ if __name__ == '__main__':
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        fh.setFormatter(formatter)
        logger.addHandler(fh)
-       from config import config
-       from optparse import OptionParser
-       parser = OptionParser()
-       parser.set_defaults(filename=None, 
+       import parser as parsermodule
+       parser = parsermodule.getParser()
+       parser.set_defaults(nodelist=None, 
                                                increment=False, 
                                                pcuid=None,
+                                               pcuselect=None,
+                                               site=None,
                                                dbname="findbadpcus", 
                                                cachenodes=False,
                                                refresh=False,
                                                )
-       parser.add_option("-f", "--nodelist", dest="filename", metavar="FILE", 
+       parser.add_option("-f", "--nodelist", dest="nodelist", metavar="FILE", 
                                                help="Provide the input file for the node list")
+       parser.add_option("", "--site", dest="site", metavar="FILE", 
+                                               help="Get all pcus associated with the given site's nodes")
+       parser.add_option("", "--pcuselect", dest="pcuselect", metavar="FILE", 
+                                               help="Query string to apply to the findbad pcus")
        parser.add_option("", "--pcuid", dest="pcuid", metavar="id", 
                                                help="Provide the id for a single pcu")
+
        parser.add_option("", "--cachenodes", action="store_true",
                                                help="Cache node lookup from PLC")
        parser.add_option("", "--dbname", dest="dbname", metavar="FILE", 
@@ -423,8 +458,8 @@ if __name__ == '__main__':
                                                help="Refresh the cached values")
        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()
+       parser = parsermodule.getParser(['defaults'], parser)
+       config = parsermodule.parse_args(parser)
        try:
                # NOTE: evidently, there is a bizarre interaction between iLO and ssh
                # when LANG is set... Do not know why.  Unsetting LANG, fixes the problem.
@@ -433,9 +468,10 @@ if __name__ == '__main__':
                main()
                time.sleep(1)
        except Exception, err:
-               import traceback
                traceback.print_exc()
+               from nodecommon import email_exception
+               email_exception()
                print "Exception: %s" % err
                print "Saving data... exitting."
-               soltesz.dbDump(config.dbname, externalState)
+               database.dbDump(config.dbname, externalState)
                sys.exit(0)