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_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_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 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 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)
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)
) plugin.add_arg( '-r', '--result', type = int, metavar = 'INTEGER', dest = 'result', help = help_result, ) plugin.parse_args() verbose = plugin.argparser.args.verbose init_root_logger(verbose) plugin.set_thresholds( warning = plugin.argparser.args.warning, critical = plugin.argparser.args.critical, ) result = plugin.argparser.args.result if result is None: result = random.randint(1, 20) log.debug("Checking result value of %d.", result) else: if result < 0 or result > 20: plugin.die((" invalid number supplied for the -r option, " + "must be between 0 and 20")) plugin.add_perfdata( label = 'Result', value = result, threshold = plugin.threshold,