def test_plugin_object_props(self): log.info("Testing NagiosPlugin object properties.") plugin = NagiosPlugin() self.assertIsInstance(plugin, NagiosPlugin) log.debug("Setting shortname explicitly to 'PAGESIZE'.") plugin.shortname = "PAGESIZE" self.assertEqual(plugin.shortname, "PAGESIZE") log.debug("Resetting plugin to default.") plugin = NagiosPlugin() self.assertEqual(plugin.shortname, "TEST_PLUGIN_01") log.debug("Creating plugin with a shortname 'SIZE' on init.") plugin = NagiosPlugin(shortname='SIZE') self.assertEqual(plugin.shortname, "SIZE") log.debug("Creating plugin with a plugin name 'check_stuff'") plugin = NagiosPlugin(plugin='check_stuff') self.assertEqual(plugin.shortname, "STUFF") log.debug("Creating plugin with a shortname 'SIZE' and a plugin " + "name 'check_stuff' on init.") plugin = NagiosPlugin(shortname='SIZE', plugin='check_stuff') self.assertEqual(plugin.shortname, "SIZE") log.debug("Setting thresholds to warn if < 10, critical if > 25.") t = plugin.set_thresholds(warning="10:25", critical="~:25") self.assertIsInstance(t, NagiosThreshold) log.debug("Adding performance data size ...") plugin.add_perfdata(label="size", value=1, uom="kB", threshold=t) pdata = plugin.all_perfoutput() log.debug("Got performance data: %r", pdata) self.assertEqual(pdata, 'size=1kB;10:25;~:25') log.debug("Adding performance data time ...") plugin.add_perfdata(label="time", value=3.52, threshold=t) pdata = plugin.all_perfoutput() log.debug("Got performance data: %r", pdata) self.assertEqual(pdata, 'size=1kB;10:25;~:25 time=3.52;10:25;~:25')
def setUp(self): super(TestNagiosPlugin3, self).setUp() self.plugin_name = 'TEST_CHECK_MESSAGES' self.plugin = NagiosPlugin(shortname=self.plugin_name) self.msgs = { 'critical': 'A B C', 'warning': 'D E F', 'ok': 'G I H', }
def test_read_file(self): log.info("Testing reading a file by a Nagios plugin.") plugin = NagiosPlugin( usage='%(prog)s --hello', url='http://www.profitbricks.com', blurb='Sample Nagios plugin for reading a file.', licence= 'Licence: GNU Lesser General Public License (LGPL), Version 3', extra='Bla blub', ) if self.verbose > 3: log.debug("NagiosPluginArgparse object: %s", str(plugin)) log.debug("Reading file %r ...", self.tmp_file) content = plugin.read_file(self.tmp_file) self.assertEqual(content, TEST_CONTENT)
def test_plugin_object(self): log.info("Testing NagiosPlugin object.") plugin = NagiosPlugin( usage='%(prog)s --hello', url='http://www.profitbricks.com', blurb='Senseless sample Nagios plugin.', licence= 'Licence: GNU Lesser General Public License (LGPL), Version 3', extra='Bla blub', ) plugin.add_perfdata('bla', 10, 'MByte', warning='20', critical='30') plugin.set_thresholds(warning='10:25', critical="~:25") plugin.add_message(nagios.state.ok, 'bli', 'bla') plugin.add_message('warning', 'blub') log.debug("NagiosPluginArgparse object: %r", plugin) log.debug("NagiosPluginArgparse object: %s", str(plugin))
def test_read_non_existing(self): log.info("Testing trying to read a non existing file.") plugin = NagiosPlugin( usage='%(prog)s --hello', url='http://www.profitbricks.com', blurb='Sample Nagios plugin for reading a file.', licence= 'Licence: GNU Lesser General Public License (LGPL), Version 3', extra='Bla blub', ) if self.verbose > 3: log.debug("NagiosPluginArgparse object: %s", str(plugin)) log.debug("Removing %r ...", self.tmp_file) os.remove(self.tmp_file) log.debug("Reading file %r ...", self.tmp_file) with self.assertRaises(IOError) as cm: content = plugin.read_file(self.tmp_file) e = cm.exception log.debug("%s raised on read_file() with a non existing file: %s", e.__class__.__name__, e)
def test_plugin_threshold_checks(self): log.info("Testing plugin threshold checks ...") plugin = NagiosPlugin(shortname='SIZE', plugin='check_stuff') log.debug("Setting thresholds to warn if < 10, critical if > 25.") t = plugin.set_thresholds(warning="10:25", critical="~:25") expected_results = OrderedDict() expected_results[-1] = nagios.state.warning expected_results[1] = nagios.state.warning expected_results[20] = nagios.state.ok expected_results[25] = nagios.state.ok expected_results[26] = nagios.state.critical expected_results[30] = nagios.state.critical for value in list(expected_results.keys()): ecpected_result = expected_results[value] got_result = t.get_status(value) log.debug("Checking value %d, expect result %d, got result %d.", value, ecpected_result, got_result) self.assertEqual(got_result, ecpected_result)
%(prog)s -w 10: -c 4: Returns a warning, if the resulting number is less than 10, or a critical error, if it is less than 4. """ progname = os.path.basename(sys.argv[0]) extra = textwrap.dedent(extra).strip() % {'prog': progname} plugin = NagiosPlugin( usage = '''%(prog)s [ -v|--verbose ] [-t <timeout>] -c|--critical <critical threshold> -w|--warning <warning threshold> [-r|--result <INTEGER>]''', version = __version__, url = 'http://www.profitbricks.com', blurb = blurb, extra = extra, ) help_warn = """\ Minimum and maximum number of allowable result, outside of which a warning will be generated. If omitted, no warning is generated. """ help_warn = textwrap.dedent(help_warn) help_crit = """\ Minimum and maximum number of allowable result, outside of which a critical will be generated. """
def setUp(self): super(TestNagiosPlugin2, self).setUp() self.plugin_name = 'TEST_PLUGIN' self.plugin = NagiosPlugin(shortname = self.plugin_name)