예제 #1
0
    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',
        }
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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))
예제 #5
0
    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))
예제 #6
0
    def test_no_permissions(self):

        log.info("Testing trying to read a file without permissions.")
        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("Chmod 000 to %r ...", self.tmp_file)
        os.chmod(self.tmp_file, 0)
        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 file without permissions: %s",
                e.__class__.__name__, e)
예제 #7
0
    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)
예제 #8
0
    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',
        }
예제 #9
0
    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)
예제 #10
0
    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)
예제 #11
0
    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')
예제 #12
0
class TestNagiosPlugin3(NeedConfig):

    #--------------------------------------------------------------------------
    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_plugin_object(self):

        log.info("Testing NagiosPlugin object shortname %r.", self.plugin_name)
        self.assertEqual(self.plugin.shortname, self.plugin_name)

    #--------------------------------------------------------------------------
    def test_codes(self):

        log.info("Testing check_messages() return code.")
        codes = [
            [['Critical'], ['Warning'], nagios.state.critical],
            [[],           ['Warning'], nagios.state.warning],
            [[],           [],          nagios.state.ok],
        ]

        i = 0
        for fields in codes:

            c_msgs = fields[0]
            w_msgs = fields[1]
            exp_code = fields[2]
            i += 1

            (got_code, message) = self.plugin.check_messages(
                    critical = c_msgs, warning = w_msgs)
            log.debug(("Test %d: Crit messages: %r, Warn messages: %r, " +
                    "got code %d, got message: %r."), i, c_msgs, w_msgs,
                    got_code, message)
            self.assertEqual(got_code, exp_code)

    #--------------------------------------------------------------------------
    def test_messages(self):

        log.info("Testing check_messages() return message.")
        arrays = {
            'critical': ['A', 'B', 'C'],
            'warning':  ['D', 'E', 'F'],
            'ok':       ['G', 'H', 'I'],
        }

        messages = {}
        for key in arrays:
            messages[key] = ' '.join(arrays[key])

        (code, message) = self.plugin.check_messages(
                critical = arrays['critical'], warning = arrays['warning'])
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, messages['critical'])

        (code, message) = self.plugin.check_messages(
                critical = arrays['critical'], warning = arrays['warning'],
                ok = 'G H I')
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, messages['critical'])

        (code, message) = self.plugin.check_messages(
                warning = arrays['warning'])
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, messages['warning'])

        (code, message) = self.plugin.check_messages(
                warning = arrays['warning'], ok = 'G H I')
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, messages['warning'])

        (code, message) = self.plugin.check_messages(ok = arrays['ok'])
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, messages['ok'])

        (code, message) = self.plugin.check_messages(ok = 'G H I')
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, messages['ok'])

        # explicit join
        join = '+'
        (code, message) = self.plugin.check_messages(
                critical = arrays['critical'], warning = arrays['warning'],
                join = join)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(message, join.join(arrays['critical']))

        join = ''
        (code, message) = self.plugin.check_messages(
                warning = arrays['warning'], join = join)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(message, join.join(arrays['warning']))

        join = None
        (code, message) = self.plugin.check_messages(
                ok = arrays['ok'], join = join)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(message, ' '.join(arrays['ok']))

        #join_all messages
        join_all = ' :: '
        msg_all_cwo = join_all.join([messages[x] for x in ('critical', 'warning', 'ok')])
        msg_all_cw = join_all.join([messages[x] for x in ('critical', 'warning')])
        msg_all_wo = join_all.join([messages[x] for x in ('warning', 'ok')])

        log.debug("Checking join_all critical, warning, ok.")
        (code, message) = self.plugin.check_messages(
                critical = arrays['critical'], warning = arrays['warning'],
                ok = arrays['ok'], join_all = join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, msg_all_cwo)

        log.debug("Checking join_all critical, warning.")
        (code, message) = self.plugin.check_messages(
                critical = arrays['critical'], warning = arrays['warning'],
                join_all = join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, msg_all_cw)

        log.debug("Checking join_all warning, ok.")
        (code, message) = self.plugin.check_messages(warning = arrays['warning'],
                ok = arrays['ok'], join_all = join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, msg_all_wo)

        log.debug("Checking join_all warning.")
        (code, message) = self.plugin.check_messages(warning = arrays['warning'],
                join_all = join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, messages['warning'])

    #--------------------------------------------------------------------------
    def test_add_message01(self):

        log.info("Testing add_message() 01 ...")

        self.plugin.add_message('critical', self.msgs['critical'])
        self.plugin.add_message('warning', self.msgs['warning'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, self.msgs['critical'])

    #--------------------------------------------------------------------------
    def test_add_message02(self):

        log.info("Testing add_message() 02 ...")

        self.plugin.add_message('critical', self.msgs['critical'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, self.msgs['critical'])

    #--------------------------------------------------------------------------
    def test_add_message03(self):

        log.info("Testing add_message() 03 ...")

        self.plugin.add_message('warning', self.msgs['warning'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, self.msgs['warning'])

    #--------------------------------------------------------------------------
    def test_add_message04(self):

        log.info("Testing add_message() 04 ...")

        self.plugin.add_message('warning', self.msgs['warning'])
        self.plugin.add_message('ok', self.msgs['ok'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, self.msgs['warning'])

    #--------------------------------------------------------------------------
    def test_add_message05(self):

        log.info("Testing add_message() 05 ...")

        self.plugin.add_message('ok', self.msgs['ok'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, self.msgs['ok'])

    #--------------------------------------------------------------------------
    def test_add_message06(self):

        log.info("Testing add_message() 06 ...")

        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, '')

    #--------------------------------------------------------------------------
    def test_add_message_invalid(self):

        log.info("Testing add_message() with invalid codes ...")

        for key in ('foobar', 'OKAY', 'unknown', 'dependent'):
            log.debug("Trying to add a message with code %r ...", key)
            with self.assertRaises(NagiosPluginError) as cm:
                self.plugin.add_message(key, 'hi mum')
            log.debug("Got a NagiosPluginError: %s", str(cm.exception))
예제 #13
0
class TestNagiosPlugin3(NeedConfig):

    #--------------------------------------------------------------------------
    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_plugin_object(self):

        log.info("Testing NagiosPlugin object shortname %r.", self.plugin_name)
        self.assertEqual(self.plugin.shortname, self.plugin_name)

    #--------------------------------------------------------------------------
    def test_codes(self):

        log.info("Testing check_messages() return code.")
        codes = [
            [['Critical'], ['Warning'], nagios.state.critical],
            [[], ['Warning'], nagios.state.warning],
            [[], [], nagios.state.ok],
        ]

        i = 0
        for fields in codes:

            c_msgs = fields[0]
            w_msgs = fields[1]
            exp_code = fields[2]
            i += 1

            (got_code, message) = self.plugin.check_messages(critical=c_msgs,
                                                             warning=w_msgs)
            log.debug(("Test %d: Crit messages: %r, Warn messages: %r, " +
                       "got code %d, got message: %r."), i, c_msgs, w_msgs,
                      got_code, message)
            self.assertEqual(got_code, exp_code)

    #--------------------------------------------------------------------------
    def test_messages(self):

        log.info("Testing check_messages() return message.")
        arrays = {
            'critical': ['A', 'B', 'C'],
            'warning': ['D', 'E', 'F'],
            'ok': ['G', 'H', 'I'],
        }

        messages = {}
        for key in arrays:
            messages[key] = ' '.join(arrays[key])

        (code,
         message) = self.plugin.check_messages(critical=arrays['critical'],
                                               warning=arrays['warning'])
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, messages['critical'])

        (code,
         message) = self.plugin.check_messages(critical=arrays['critical'],
                                               warning=arrays['warning'],
                                               ok='G H I')
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, messages['critical'])

        (code, message) = self.plugin.check_messages(warning=arrays['warning'])
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, messages['warning'])

        (code, message) = self.plugin.check_messages(warning=arrays['warning'],
                                                     ok='G H I')
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, messages['warning'])

        (code, message) = self.plugin.check_messages(ok=arrays['ok'])
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, messages['ok'])

        (code, message) = self.plugin.check_messages(ok='G H I')
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, messages['ok'])

        # explicit join
        join = '+'
        (code,
         message) = self.plugin.check_messages(critical=arrays['critical'],
                                               warning=arrays['warning'],
                                               join=join)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(message, join.join(arrays['critical']))

        join = ''
        (code, message) = self.plugin.check_messages(warning=arrays['warning'],
                                                     join=join)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(message, join.join(arrays['warning']))

        join = None
        (code, message) = self.plugin.check_messages(ok=arrays['ok'],
                                                     join=join)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(message, ' '.join(arrays['ok']))

        #join_all messages
        join_all = ' :: '
        msg_all_cwo = join_all.join(
            [messages[x] for x in ('critical', 'warning', 'ok')])
        msg_all_cw = join_all.join(
            [messages[x] for x in ('critical', 'warning')])
        msg_all_wo = join_all.join([messages[x] for x in ('warning', 'ok')])

        log.debug("Checking join_all critical, warning, ok.")
        (code,
         message) = self.plugin.check_messages(critical=arrays['critical'],
                                               warning=arrays['warning'],
                                               ok=arrays['ok'],
                                               join_all=join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, msg_all_cwo)

        log.debug("Checking join_all critical, warning.")
        (code,
         message) = self.plugin.check_messages(critical=arrays['critical'],
                                               warning=arrays['warning'],
                                               join_all=join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, msg_all_cw)

        log.debug("Checking join_all warning, ok.")
        (code, message) = self.plugin.check_messages(warning=arrays['warning'],
                                                     ok=arrays['ok'],
                                                     join_all=join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, msg_all_wo)

        log.debug("Checking join_all warning.")
        (code, message) = self.plugin.check_messages(warning=arrays['warning'],
                                                     join_all=join_all)
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, messages['warning'])

    #--------------------------------------------------------------------------
    def test_add_message01(self):

        log.info("Testing add_message() 01 ...")

        self.plugin.add_message('critical', self.msgs['critical'])
        self.plugin.add_message('warning', self.msgs['warning'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, self.msgs['critical'])

    #--------------------------------------------------------------------------
    def test_add_message02(self):

        log.info("Testing add_message() 02 ...")

        self.plugin.add_message('critical', self.msgs['critical'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.critical)
        self.assertEqual(message, self.msgs['critical'])

    #--------------------------------------------------------------------------
    def test_add_message03(self):

        log.info("Testing add_message() 03 ...")

        self.plugin.add_message('warning', self.msgs['warning'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, self.msgs['warning'])

    #--------------------------------------------------------------------------
    def test_add_message04(self):

        log.info("Testing add_message() 04 ...")

        self.plugin.add_message('warning', self.msgs['warning'])
        self.plugin.add_message('ok', self.msgs['ok'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.warning)
        self.assertEqual(message, self.msgs['warning'])

    #--------------------------------------------------------------------------
    def test_add_message05(self):

        log.info("Testing add_message() 05 ...")

        self.plugin.add_message('ok', self.msgs['ok'])
        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, self.msgs['ok'])

    #--------------------------------------------------------------------------
    def test_add_message06(self):

        log.info("Testing add_message() 06 ...")

        (code, message) = self.plugin.check_messages()
        log.debug("Checking code %d, message %r.", code, message)
        self.assertEqual(code, nagios.state.ok)
        self.assertEqual(message, '')

    #--------------------------------------------------------------------------
    def test_add_message_invalid(self):

        log.info("Testing add_message() with invalid codes ...")

        for key in ('foobar', 'OKAY', 'unknown', 'dependent'):
            log.debug("Trying to add a message with code %r ...", key)
            with self.assertRaises(NagiosPluginError) as cm:
                self.plugin.add_message(key, 'hi mum')
            log.debug("Got a NagiosPluginError: %s", str(cm.exception))
예제 #14
0
        %(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.
    """
예제 #15
0
    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')
예제 #16
0
class TestNagiosPlugin2(NeedConfig):

    #--------------------------------------------------------------------------
    def setUp(self):

        super(TestNagiosPlugin2, self).setUp()

        self.plugin_name = 'TEST_PLUGIN'
        self.plugin = NagiosPlugin(shortname = self.plugin_name)

    #--------------------------------------------------------------------------
    def test_plugin_object(self):

        log.info("Testing NagiosPlugin object shortname.")
        self.assertEqual(self.plugin.shortname, self.plugin_name)

    #--------------------------------------------------------------------------
    def test_nagios_exit(self):

        log.info("Testing nagios_exit() ...")
        ok = [
            [nagios.state.ok,        'OK',        'test the first', ],
            [nagios.state.warning,   'WARNING',   'test the second' ],
            [nagios.state.critical,  'CRITICAL',  'test the third', ],
            [nagios.state.unknown,   'UNKNOWN',   'test the fourth',],
            [nagios.state.dependent, 'DEPENDENT', 'test the fifth', ],
        ]

        for fields in ok:

            code = fields[0]
            marker = fields[1]
            msg = fields[2]

            # Test for numeric return codes
            with self.assertRaises(FakeExitError) as cm:
                self.plugin.nagios_exit(code, msg)
            e = cm.exception
            ret_code = e.exit_value
            e_msg = e.msg
            log.debug("Exit with value %d and the message %r.", ret_code, e_msg)
            self.assertEqual(ret_code, code)
            pattern = r'%s\b.*%s\b.*\b%s$' % (self.plugin_name, marker, msg)
            log.debug("Checking for pattern %r.", pattern)
            regex = re.compile(pattern)
            self.assertRegexpMatches(e_msg, regex)

            # Test for string return codes
            with self.assertRaises(FakeExitError) as cm:
                self.plugin.nagios_exit(marker, msg)
            e = cm.exception
            ret_code = e.exit_value
            e_msg = e.msg
            log.debug("Exit with value %d and the message %r.", ret_code, e_msg)
            self.assertEqual(ret_code, code)
            pattern = r'%s\b.*%s\b.*\b%s$' % (self.plugin_name, marker, msg)
            log.debug("Checking for pattern %r.", pattern)
            regex = re.compile(pattern)
            self.assertRegexpMatches(e_msg, regex)

    #--------------------------------------------------------------------------
    def test_nagios_exit_ugly_code(self):

        log.info("Testing nagios_exit() with ugly codes ...")
        ugly = [
            [      -1, 'testing code -1'],
            [       7, 'testing code 7'],
            [    None, 'testing code None'],
            [      '', "testing code ''"],
            ['string', "testing code 'string'"],
        ]

        for fields in ugly:

            code = fields[0]
            msg = fields[1]

            with self.assertRaises(FakeExitError) as cm:
                self.plugin.nagios_exit(code, msg)
            e = cm.exception
            ret_code = e.exit_value
            e_msg = e.msg
            log.debug("Exit with value %d and the message %r.", ret_code, e_msg)
            self.assertEqual(ret_code, nagios.state.unknown)
            pattern = r'%s\b.*UNKNOWN\b.*\b%s$' % (self.plugin_name, msg)
            log.debug("Checking for pattern %r.", pattern)
            regex = re.compile(pattern)
            self.assertRegexpMatches(e_msg, regex)

    #--------------------------------------------------------------------------
    def test_nagios_exit_ugly_msg(self):

        log.info("Testing nagios_exit() with ugly messages ...")

        ugly = ['', None, nagios.state.unknown]

        for msg in ugly:

            with self.assertRaises(FakeExitError) as cm:
                self.plugin.nagios_exit(nagios.state.critical, msg)
            e = cm.exception
            ret_code = e.exit_value
            e_msg = e.msg
            log.debug("Exit with value %d and the message %r.", ret_code, e_msg)
            self.assertEqual(ret_code, nagios.state.critical)
            display = msg
            if display is None:
                display = ''
            pattern = r'%s\b.*CRITICAL\b.*\b%s$' % (self.plugin_name, display)
            log.debug("Checking for pattern %r.", pattern)
            regex = re.compile(pattern)
            self.assertRegexpMatches(e_msg, regex)

    #--------------------------------------------------------------------------
    def test_nagios_die(self):

        log.info("Testing nagios_die() ...")

        ugly = ['die you dog', '', None]

        for msg in ugly:

            with self.assertRaises(FakeExitError) as cm:
                self.plugin.nagios_die(msg)
            e = cm.exception
            ret_code = e.exit_value
            e_msg = e.msg
            log.debug("Exit with value %d and the message %r.", ret_code, e_msg)
            self.assertEqual(ret_code, nagios.state.unknown)
            display = msg
            if display is None:
                display = ''
            pattern = r'%s\b.*UNKNOWN\b.*\b%s$' % (self.plugin_name, display)
            log.debug("Checking for pattern %r.", pattern)
            regex = re.compile(pattern)
            self.assertRegexpMatches(e_msg, regex)
예제 #17
0
    def setUp(self):

        super(TestNagiosPlugin2, self).setUp()

        self.plugin_name = 'TEST_PLUGIN'
        self.plugin = NagiosPlugin(shortname = self.plugin_name)