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 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 test_eval(self): """Test the evaluation of the warning/critical level.""" nagios = SimpleNagios( foo=100, foo_critical=90, bar=20, ) nagios._eval()
def _basic_test_single_instance(self, kwargs, message, nagios_exit): """Basic test""" self.buffo.seek(0) self.buffo.truncate(0) try: SimpleNagios(**kwargs) except SystemExit, e: pass
def _basic_test_single_instance_and_exit(self, fn, msg, message, nagios_exit): """Basic test""" self.buffo.seek(0) self.buffo.truncate(0) n = SimpleNagios() f = getattr(n, fn) try: f(msg) except SystemExit, e: pass
def _basic_test_single_instance(self, kwargs, message, nagios_exit): """Basic test""" self.buffo.seek(0) self.buffo.truncate(0) raised_exception = None try: SimpleNagios(**kwargs) except SystemExit as err: raised_exception = err bo = self.buffo.getvalue().rstrip() self.assertEqual(bo, message) self.assertEqual(raised_exception.code, nagios_exit[0])
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
def _basic_test_single_instance_and_exit(self, fn, msg, message, nagios_exit): """Basic test""" self.buffo.seek(0) self.buffo.truncate(0) nagios = SimpleNagios() func = getattr(nagios, fn) raised_exception = None try: func(msg) except SystemExit as err: raised_exception = err bo = self.buffo.getvalue().rstrip() self.assertEqual(bo, message) self.assertEqual(raised_exception.code, nagios_exit[0])
def test_world_readable(self): """Test world readable cache""" (handle, filename) = tempfile.mkstemp() os.unlink(filename) n = SimpleNagios(_cache=filename, _cache_user=self.nagios_user, _world_readable=True) n.ok("test") os.close(handle) try: reporter_test = NagiosReporter('test_cache', filename, -1, self.nagios_user) reporter_test.report_and_exit() except SystemExit: pass statres = os.stat(filename) self.assertTrue(statres.st_mode & stat.S_IROTH)