AM nagios/plc2nagios.py
[monitor.git] / findbadpcu.py
index 122d8a5..399359a 100755 (executable)
@@ -8,6 +8,7 @@ import socket
 
     
 import signal
+import traceback
 
 #old_handler = signal.getsignal(signal.SIGCHLD)
 
@@ -24,30 +25,6 @@ import signal
 #
 #orig_sig_handler = signal.signal(signal.SIGCHLD, sig_handler)
 
-from config import config
-from optparse import OptionParser
-parser = OptionParser()
-parser.set_defaults(filename=None, 
-                                       increment=False, 
-                                       pcuid=None,
-                                       dbname="findbadpcus", 
-                                       cachenodes=False,
-                                       refresh=False,
-                                       )
-parser.add_option("-f", "--nodelist", dest="filename", metavar="FILE", 
-                                       help="Provide the input file for the node list")
-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", 
-                                       help="Specify the name of the database to which the information is saved")
-parser.add_option("", "--refresh", action="store_true", dest="refresh",
-                                       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()
 
 # QUERY all nodes.
 COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
@@ -102,7 +79,6 @@ def get_pcu(pcuname):
                                if i['pcu_id'] == pcuname:
                                        l_pcu = i
                except:
-                       import traceback
                        traceback.print_exc()
                        l_pcu = None
 
@@ -121,7 +97,6 @@ def get_nodes(node_ids):
                                if n['node_id'] in node_ids:
                                        l_node.append(n)
                except:
-                       import traceback
                        traceback.print_exc()
                        l_node = None
 
@@ -179,7 +154,6 @@ def get_plc_site_values(site_id):
                                        d_site = site
                                        break
                except:
-                       import traceback
                        traceback.print_exc()
                        values = None
 
@@ -218,7 +192,6 @@ def collectPingAndSSH(pcuname, cohash):
                                continue_probe = False
                except:
                        b_except = True
-                       import traceback
                        traceback.print_exc()
                        continue_probe = False
 
@@ -286,7 +259,7 @@ def collectPingAndSSH(pcuname, cohash):
                #### RUN NMAP ###############################
                if continue_probe:
                        nmap = soltesz.CMD()
-                       (oval,eval) = nmap.run_noexcept("nmap -oG - -P0 -p22,23,80,443,5869,16992 %s | grep Host:" % pcu_name(values))
+                       (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)
                else:
@@ -313,7 +286,6 @@ def collectPingAndSSH(pcuname, cohash):
                print values
                errors = values
                print "____________________________________"
-               import traceback
                errors['traceback'] = traceback.format_exc()
                print errors['traceback']
 
@@ -415,17 +387,40 @@ def main():
 
        return 0
 
-import logging
-logger = logging.getLogger("monitor")
-logger.setLevel(logging.DEBUG)
-fh = logging.FileHandler("monitor.log", mode = 'a')
-fh.setLevel(logging.DEBUG)
-formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
-fh.setFormatter(formatter)
-logger.addHandler(fh)
-
 
 if __name__ == '__main__':
+       import logging
+       logger = logging.getLogger("monitor")
+       logger.setLevel(logging.DEBUG)
+       fh = logging.FileHandler("monitor.log", mode = 'a')
+       fh.setLevel(logging.DEBUG)
+       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, 
+                                               increment=False, 
+                                               pcuid=None,
+                                               dbname="findbadpcus", 
+                                               cachenodes=False,
+                                               refresh=False,
+                                               )
+       parser.add_option("-f", "--nodelist", dest="filename", metavar="FILE", 
+                                               help="Provide the input file for the node list")
+       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", 
+                                               help="Specify the name of the database to which the information is saved")
+       parser.add_option("", "--refresh", action="store_true", dest="refresh",
+                                               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()
        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.
@@ -434,7 +429,6 @@ if __name__ == '__main__':
                main()
                time.sleep(1)
        except Exception, err:
-               import traceback
                traceback.print_exc()
                print "Exception: %s" % err
                print "Saving data... exitting."