def test_scriptnotfound_timing(self): output = ("The script 'script-monitor-test' didn't run on " "'localhost' between 2007-05-23 01:30:00 and " "2007-05-23 02:30:00 (last seen 2007-05-23 01:00:00)") self.assertEqual( check_script(self.con, self.log, 'localhost', 'script-monitor-test', '2007-05-23 01:30:00', '2007-05-23 02:30:00'), output)
def test_scriptnotfound_hostname(self): output = ("The script 'script-monitor-test' didn't run on " "'notlocalhost' between 2007-05-23 00:30:00 and " "2007-05-23 01:30:00") self.assertEqual( check_script(self.con, self.log, 'notlocalhost', 'script-monitor-test', '2007-05-23 00:30:00', '2007-05-23 01:30:00'), output)
def test_scriptfound(self): self.assertEqual( check_script(self.con, self.log, 'localhost', 'script-monitor-test', '2007-05-23 00:30:00', '2007-05-23 01:30:00'), None)
def main(): # XXX: Tom Haddon 2007-07-12 # There's a lot of untested stuff here: parsing options - # this should be moved into a testable location. # Also duplicated code in scripts/script-monitor.py parser = OptionParser( '%prog [options] (minutes) (host:scriptname) [host:scriptname]' ) db_options(parser) logger_options(parser) (options, args) = parser.parse_args() if len(args) < 2: print "Must specify time in minutes and " \ "at least one host and script" return 3 # First argument is the number of minutes into the past # we want to look for the scripts on the specified hosts try: minutes_ago, args = int(args[0]), args[1:] start_date = datetime.now() - timedelta(minutes=minutes_ago) completed_from = strftime("%Y-%m-%d %H:%M:%S", start_date.timetuple()) completed_to = strftime( "%Y-%m-%d %H:%M:%S", datetime.now().timetuple()) hosts_scripts = [] for arg in args: try: hostname, scriptname = arg.split(':') except TypeError: print "%r is not in the format 'host:scriptname'" % arg return 3 hosts_scripts.append((hostname, scriptname)) except ValueError: print "Must specify time in minutes and " \ "at least one host and script" return 3 log = logger(options) try: log.debug("Connecting to database") con = connect() error_found = False msg = [] for hostname, scriptname in hosts_scripts: failure_msg = check_script(con, log, hostname, scriptname, completed_from, completed_to) if failure_msg is not None: msg.append("%s:%s" % (hostname, scriptname)) error_found = True if error_found: # Construct our return message print "Scripts failed to run: %s" % ', '.join(msg) return 2 else: # Construct our return message print "All scripts ran as expected" return 0 except Exception as e: # Squeeze the exception type and stringification of the exception # value on to one line. print "Unhandled exception: %s %r" % (e.__class__.__name__, str(e)) return 3
def main(): # XXX: Tom Haddon 2007-07-12 # There's a lot of untested stuff here: parsing options and sending # emails - this should be moved into a testable location. # Also duplicated code in scripts/script-monitor-nagios.py parser = OptionParser( '%prog [options] (minutes) (host:scriptname) [host:scriptname]') db_options(parser) logger_options(parser) (options, args) = parser.parse_args() if len(args) < 2: parser.error("Must specify at time in minutes and " "at least one host and script") # First argument is the number of minutes into the past # we want to look for the scripts on the specified hosts try: minutes_ago, args = int(args[0]), args[1:] start_date = datetime.now() - timedelta(minutes=minutes_ago) completed_from = strftime("%Y-%m-%d %H:%M:%S", start_date.timetuple()) completed_to = strftime("%Y-%m-%d %H:%M:%S", datetime.now().timetuple()) hosts_scripts = [] for arg in args: try: hostname, scriptname = arg.split(':') except TypeError: parser.error("%r is not in the format 'host:scriptname'" % (arg, )) hosts_scripts.append((hostname, scriptname)) except ValueError: parser.error("Must specify time in minutes and " "at least one host and script") log = logger(options) try: log.debug("Connecting to database") con = connect() error_found = False msg, subj = [], [] for hostname, scriptname in hosts_scripts: failure_msg = check_script(con, log, hostname, scriptname, completed_from, completed_to) if failure_msg is not None: msg.append(failure_msg) subj.append("%s:%s" % (hostname, scriptname)) error_found = 2 if error_found: # Construct our email. msg = MIMEText('\n'.join(msg)) msg['Subject'] = "Scripts failed to run: %s" % ", ".join(subj) msg['From'] = '*****@*****.**' msg['Reply-To'] = '*****@*****.**' msg['To'] = '*****@*****.**' # Send out the email. smtp = smtplib.SMTP() smtp.connect() smtp.sendmail('*****@*****.**', ['*****@*****.**'], msg.as_string()) smtp.close() return 2 except: log.exception("Unhandled exception") return 1
def main(): # XXX: Tom Haddon 2007-07-12 # There's a lot of untested stuff here: parsing options and sending # emails - this should be moved into a testable location. # Also duplicated code in scripts/script-monitor-nagios.py parser = OptionParser( '%prog [options] (minutes) (host:scriptname) [host:scriptname]' ) db_options(parser) logger_options(parser) (options, args) = parser.parse_args() if len(args) < 2: parser.error("Must specify at time in minutes and " "at least one host and script") # First argument is the number of minutes into the past # we want to look for the scripts on the specified hosts try: minutes_ago, args = int(args[0]), args[1:] start_date = datetime.now() - timedelta(minutes=minutes_ago) completed_from = strftime("%Y-%m-%d %H:%M:%S", start_date.timetuple()) completed_to = strftime( "%Y-%m-%d %H:%M:%S", datetime.now().timetuple()) hosts_scripts = [] for arg in args: try: hostname, scriptname = arg.split(':') except TypeError: parser.error( "%r is not in the format 'host:scriptname'" % (arg,)) hosts_scripts.append((hostname, scriptname)) except ValueError: parser.error("Must specify time in minutes and " "at least one host and script") log = logger(options) try: log.debug("Connecting to database") con = connect() error_found = False msg, subj = [], [] for hostname, scriptname in hosts_scripts: failure_msg = check_script(con, log, hostname, scriptname, completed_from, completed_to) if failure_msg is not None: msg.append(failure_msg) subj.append("%s:%s" % (hostname, scriptname)) error_found = 2 if error_found: # Construct our email. msg = MIMEText('\n'.join(msg)) msg['Subject'] = "Scripts failed to run: %s" % ", ".join(subj) msg['From'] = '*****@*****.**' msg['Reply-To'] = '*****@*****.**' msg['To'] = '*****@*****.**' # Send out the email. smtp = smtplib.SMTP() smtp.connect() smtp.sendmail( '*****@*****.**', ['*****@*****.**'], msg.as_string()) smtp.close() return 2 except: log.exception("Unhandled exception") return 1