def test_cache(self): """Test the caching""" (handle, filename) = tempfile.mkstemp() os.unlink(filename) n = SimpleNagios(_cache=filename, _cache_user=self.nagios_user) message = "mywarning" n.warning(message) os.close(handle) self.buffo.seek(0) self.buffo.truncate(0) raised_exception = None try: reporter_test = NagiosReporter('test_cache', filename, -1, self.nagios_user) reporter_test.report_and_exit() except SystemExit as err: raised_exception = err bo = self.buffo.getvalue().rstrip() self.assertEqual(bo, "WARNING %s" % message) self.assertEqual(raised_exception.code, NAGIOS_EXIT_WARNING[0]) statres = os.stat(filename) self.assertFalse(statres.st_mode & stat.S_IROTH)
def test_cache(self): """Test the caching""" (handle, filename) = tempfile.mkstemp() os.unlink(filename) n = SimpleNagios(_cache=filename, _cache_user=self.nagios_user) message = "mywarning" n.warning(message) os.close(handle) self.buffo.seek(0) self.buffo.truncate(0) try: reporter_test = NagiosReporter('test_cache', filename, -1, self.nagios_user) reporter_test.report_and_exit() except SystemExit, e: pass
class ExtendedSimpleOption(SimpleOption): """ Extends the SimpleOption class to allow other checks to occur at script prologue and epilogue. - nagios reporting - checking if running on the designated HA host - locking on a file The prologue should be called at the start of the script; the epilogue at the end. """ def __init__(self, options, run_prologue=True, excepthook=None, **kwargs): """Initialise. If run_prologue is True (default), we immediately execute the prologue. Note that if taking a lock is requested (default), and the lock cannot be acquire for some reason, the program will exit, """ options_ = _merge_options(options) super(ExtendedSimpleOption, self).__init__(options_, **kwargs) self.nagios_reporter = None self.lockfile = None if run_prologue: self.prologue() if not excepthook: sys.excepthook = self.critical_exception_handler else: sys.excepthook = excepthook self.log = fancylogger.getLogger() def prologue(self): """Checks the options given for settings and takes appropriate action. See _merge_options for the format. - if nagios_report is set, creates a SimpleNagios instance and prints the report. - if ha is set, checks if running on the correct host, set the appropriate nagios message and bail if not. - if locking_filename is set, take a lock. If the lock fails, bork and set the nagios exit accordingly. """ # bail if nagios report is requested self.nagios_reporter = SimpleNagios(_cache=self.options.nagios_check_filename, _report_and_exit=self.options.nagios_report, _threshold=self.options.nagios_check_interval_threshold, _cache_user=self.options.nagios_user, ) # check for HA host if self.options.ha and not proceed_on_ha_service(self.options.ha): self.log.warning("Not running on the target host %s in the HA setup. Stopping." % (self.options.ha,)) self.nagios_reporter.ok("Not running on the HA master.") sys.exit(NAGIOS_EXIT_OK) if not self.options.disable_locking and not self.options.dry_run: self.lockfile = TimestampedPidLockfile(self.options.locking_filename, threshold=self.options.nagios_check_interval_threshold * 2) lock_or_bork(self.lockfile, self.nagios_reporter) self.log.info("%s has started" % (_script_name(sys.argv[0]))) def _epilogue(self): if not self.options.disable_locking and not self.options.dry_run: release_or_bork(self.lockfile, self.nagios_reporter) def epilogue(self, nagios_message, nagios_thresholds=None): """Run at the end of a script, quitting gracefully if possible.""" if nagios_thresholds is None: nagios_thresholds = {} self._epilogue() nagios_thresholds['message'] = nagios_message self.nagios_reporter._eval_and_exit(**nagios_thresholds) self.log.info("%s has finished" % (_script_name(sys.argv[0]))) # may not be reached def ok(self, nagios_message): """Run at the end of a script and force an OK exit.""" self._epilogue() self.nagios_reporter.ok(nagios_message) def warning(self, nagios_message): """Run at the end of a script and force a Warning exit.""" self._epilogue() self.nagios_reporter.warning(nagios_message) def critical(self, nagios_message): """Run at the end of a script and force a Critical exit.""" self._epilogue() self.nagios_reporter.critical(nagios_message) def critical_exception_handler(self, tp, value, traceback): """ Run at the end of a script and force a Critical exit. This function is meant to be used as sys.excepthook """ self.log.exception("unhandled exception detected: %s - %s", tp, value) self.log.debug("traceback %s", traceback) message = "Script failure: %s - %s" % (tp, value) sys.exc_clear() self.critical(message)
class ExtendedSimpleOption(SimpleOption): """ Extends the SimpleOption class to allow other checks to occur at script prologue and epilogue. - nagios reporting - checking if running on the designated HA host - locking on a file The prologue should be called at the start of the script; the epilogue at the end. """ def __init__(self, options, run_prologue=True, excepthook=None, **kwargs): """Initialise. If run_prologue is True (default), we immediately execute the prologue. Note that if taking a lock is requested (default), and the lock cannot be acquire for some reason, the program will exit, """ options_ = _merge_options(options) super(ExtendedSimpleOption, self).__init__(options_, **kwargs) self.nagios_reporter = None self.lockfile = None if run_prologue: self.prologue() if not excepthook: sys.excepthook = self.critical_exception_handler else: sys.excepthook = excepthook self.log = fancylogger.getLogger() def prologue(self): """Checks the options given for settings and takes appropriate action. See _merge_options for the format. - if nagios_report is set, creates a SimpleNagios instance and prints the report. - if ha is set, checks if running on the correct host, set the appropriate nagios message and bail if not. - if locking_filename is set, take a lock. If the lock fails, bork and set the nagios exit accordingly. """ # bail if nagios report is requested self.nagios_reporter = SimpleNagios( _cache=self.options.nagios_check_filename, _report_and_exit=self.options.nagios_report, _threshold=self.options.nagios_check_interval_threshold, _cache_user=self.options.nagios_user, ) # check for HA host if self.options.ha and not proceed_on_ha_service(self.options.ha): self.log.warning( "Not running on the target host %s in the HA setup. Stopping." % (self.options.ha, )) self.nagios_reporter.ok("Not running on the HA master.") sys.exit(NAGIOS_EXIT_OK) if not self.options.disable_locking and not self.options.dry_run: self.lockfile = TimestampedPidLockfile( self.options.locking_filename, threshold=self.options.nagios_check_interval_threshold * 2) lock_or_bork(self.lockfile, self.nagios_reporter) self.log.info("%s has started" % (_script_name(sys.argv[0]))) def _epilogue(self): if not self.options.disable_locking and not self.options.dry_run: release_or_bork(self.lockfile, self.nagios_reporter) def epilogue(self, nagios_message, nagios_thresholds=None): """Run at the end of a script, quitting gracefully if possible.""" if nagios_thresholds is None: nagios_thresholds = {} self._epilogue() nagios_thresholds['message'] = nagios_message self.nagios_reporter._eval_and_exit(**nagios_thresholds) self.log.info("%s has finished" % (_script_name(sys.argv[0]))) # may not be reached def ok(self, nagios_message): """Run at the end of a script and force an OK exit.""" self._epilogue() self.nagios_reporter.ok(nagios_message) def warning(self, nagios_message): """Run at the end of a script and force a Warning exit.""" self._epilogue() self.nagios_reporter.warning(nagios_message) def critical(self, nagios_message): """Run at the end of a script and force a Critical exit.""" self._epilogue() self.nagios_reporter.critical(nagios_message) def critical_exception_handler(self, tp, value, traceback): """ Run at the end of a script and force a Critical exit. This function is meant to be used as sys.excepthook """ self.log.exception("unhandled exception detected: %s - %s", tp, value) self.log.debug("traceback %s", traceback) message = "Script failure: %s - %s" % (tp, value) sys.exc_clear() self.critical(message)