Example #1
0
def test_mgmt_config_ruleset_overidden_by_explicit_setting(web, protocol, cred_attribute,
                                                           folder_credentials, ruleset_credentials):
    web.set_ruleset(
        "management_board_config",
        {
            "ruleset": {
                "": [  # "" -> folder
                    {
                        'condition': {},
                        'options': {},
                        'value': (protocol, ruleset_credentials),
                    },
                ],
            }
        })

    web.add_folder("folder1", attributes={
        cred_attribute: folder_credentials,
    })

    web.add_host("mgmt-host",
                 folder="folder1",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "management_protocol": protocol,
                 })

    config_cache = reload_config()
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board
    assert host_config.management_protocol == protocol
    assert host_config.management_address == "127.0.0.1"
    assert host_config.management_credentials == folder_credentials
Example #2
0
def test_mgmt_config_ruleset_order(web):
    web.set_ruleset(
        "management_board_config",
        {
            "ruleset": {
                "": [  # "" -> folder
                    {
                        'condition': {},
                        'options': {},
                        'value': ("snmp", "RULESET1"),
                    },
                    {
                        'condition': {},
                        'options': {},
                        'value': ("snmp", "RULESET2"),
                    },
                ],
            }
        })

    web.add_folder("folder1")

    web.add_host("mgmt-host",
                 folder="folder1",
                 attributes={
                     "ipaddress": "127.0.0.1",
                     "management_protocol": "snmp",
                 })

    config_cache = reload_config()
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board
    assert host_config.management_protocol == "snmp"
    assert host_config.management_address == "127.0.0.1"
    assert host_config.management_credentials == "RULESET1"
Example #3
0
def test_mgmt_config_ruleset(web, protocol, cred_attribute, credentials, ruleset_credentials):
    web.set_ruleset(
        "management_board_config",
        {
            "ruleset": {
                "": [  # "" -> folder
                    {
                        'conditions': {
                            'host_specs': ['@all'],
                            'host_tags': []
                        },
                        'options': {},
                        'value': (protocol, ruleset_credentials),
                    },
                ],
            }
        })

    web.add_folder("folder1")

    web.add_host(
        "mgmt-host",
        folder="folder1",
        attributes={
            "ipaddress": "127.0.0.1",
            "management_protocol": protocol,
        })

    config_cache = reload_config()
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board
    assert host_config.management_protocol == protocol
    assert host_config.management_address == "127.0.0.1"
    assert host_config.management_credentials == ruleset_credentials
Example #4
0
def test_active_check_execution(test_cfg, site, web):
    try:
        # TODO: Remove bytestr marker once the GUI uses Python 3
        web.set_ruleset(
            api_str_type("custom_checks"),
            {
                api_str_type("ruleset"): {
                    # Main folder
                    api_str_type(""): [
                        {
                            api_str_type("value"): {
                                api_str_type('service_description'):
                                u'\xc4ctive-Check',
                                api_str_type('command_line'):
                                api_str_type('echo "123"')
                            },
                            api_str_type("condition"): {},
                            api_str_type("options"): {},
                        },
                    ],
                }
            })
        web.activate_changes()

        site.schedule_check("test-host", u'\xc4ctive-Check', 0)

        result = site.live.query_row(
            u"GET services\nColumns: host_name description state plugin_output has_been_checked\nFilter: host_name = test-host\nFilter: description = \xc4ctive-Check"
        )
        print("Result: %r" % result)
        assert result[4] == 1
        assert result[0] == "test-host"
        assert result[1] == u'\xc4ctive-Check'
        assert result[2] == 0
        assert result[3] == "123"
    finally:
        # TODO: Remove bytestr marker once the GUI uses Python 3
        web.set_ruleset(
            api_str_type("custom_checks"),
            {
                api_str_type("ruleset"): {
                    api_str_type(""): [],  # -> folder
                }
            })
        web.activate_changes()
Example #5
0
def test_active_check_execution(test_cfg, site, web):
    try:
        web.set_ruleset(
            "custom_checks",
            {
                "ruleset": {
                    # Main folder
                    "": [
                        {
                            "value": {
                                'service_description': u'\xc4ctive-Check',
                                'command_line': 'echo "123"'
                            },
                            "conditions": {
                                "host_specs": config.ALL_HOSTS,
                                "host_tags": [],
                            },
                            "options": {},
                        },
                    ],
                }
            })
        web.activate_changes()

        site.schedule_check("test-host", u'\xc4ctive-Check', 0)

        result = site.live.query_row(
            "GET services\nColumns: host_name description state plugin_output has_been_checked\nFilter: host_name = test-host\nFilter: description = Äctive-Check"
        )
        print "Result: %r" % result
        assert result[4] == 1
        assert result[0] == "test-host"
        assert result[1] == u'\xc4ctive-Check'
        assert result[2] == 0
        assert result[3] == "123"
    finally:
        web.set_ruleset(
            "custom_checks",
            {
                "ruleset": {
                    "": [],  # -> folder
                }
            })
        web.activate_changes()
Example #6
0
def configure_service_tags(site, web, default_cfg):
    web.set_ruleset(
        "service_tag_rules", {
            "ruleset": {
                "": [{
                    "value": [("criticality", "prod")],
                    "condition": {
                        "host_name": ["livestatus-test-host"],
                        "service_description": [{
                            "$regex": "CPU load$",
                        }],
                    },
                },],
            }
        })
    web.activate_changes()
    yield
    web.set_ruleset("service_tag_rules", {"ruleset": {"": [],}})
    web.activate_changes()
Example #7
0
def configure_service_tags_fixture(site, web, default_cfg):  # noqa: F811 # pylint: disable=redefined-outer-name
    web.set_ruleset(
        "service_tag_rules", {
            "ruleset": {
                "": [{
                    "value": [("criticality", "prod")],
                    "condition": {
                        "host_name": ["livestatus-test-host"],
                        "service_description": [{
                            "$regex": "CPU load$",
                        }],
                    },
                },],
            }
        })
    web.activate_changes()
    yield
    web.set_ruleset("service_tag_rules", {"ruleset": {"": [],}})
    web.activate_changes()
Example #8
0
def configure_service_tags(site, web):
    web.set_ruleset(
        "service_tag_rules", {
            "ruleset": {
                "": [
                    {
                        "value": [("criticality", "prod")],
                        "conditions": {
                            "host_tags": [],
                            "host_specs": ["livestatus-test-host"],
                            "service_specs": ["CPU load$"],
                        },
                    },
                ],
            }
        })
    web.activate_changes()
    yield
    web.set_ruleset("service_tag_rules", {"ruleset": {
        "": [],
    }})
    web.activate_changes()
Example #9
0
def test_set_ruleset(web):  # noqa: F811 # pylint: disable=redefined-outer-name
    orig_ruleset = web.get_ruleset("bulkwalk_hosts")
    assert orig_ruleset == {
        'ruleset': {
            '': [{
                'id': 'b92a5406-1d57-4f1d-953d-225b111239e5',
                'value': True,
                'condition': {
                    'host_tags': {
                        'snmp': 'snmp',
                        'snmp_ds': {
                            '$ne': 'snmp-v1'
                        }
                    }
                },
                'options': {
                    'description':
                    u'Hosts with the tag "snmp-v1" must not use bulkwalk'
                }
            }]
        },
        'configuration_hash': '9abf6316805b3daf10ac7745864f13f8',
    }

    # Now modify something
    ruleset = copy.deepcopy(orig_ruleset)
    ruleset["ruleset"][""][0]["value"] = False
    response = web.set_ruleset("bulkwalk_hosts", ruleset)
    assert response is None

    try:
        changed = web.get_ruleset("bulkwalk_hosts")
        assert changed["ruleset"][""][0]["value"] is False
    finally:
        # revert it back
        del orig_ruleset["configuration_hash"]
        response = web.set_ruleset("bulkwalk_hosts", orig_ruleset)
        assert response is None
Example #10
0
def test_set_ruleset(web):
    orig_ruleset = web.get_ruleset("bulkwalk_hosts")
    assert orig_ruleset == {
        'ruleset': {
            '': [{
                'value': True,
                'condition': {
                    'host_tags': {
                        'snmp': 'snmp',
                        'snmp_ds': {
                            '$ne': 'snmp-v1'
                        }
                    }
                },
                'options': {
                    'description':
                    u'Hosts with the tag "snmp-v1" must not use bulkwalk'
                }
            }]
        },
        'configuration_hash': '0cca93426feb558f7c9f09631340c63c',
    }

    # Now modify something
    ruleset = copy.deepcopy(orig_ruleset)
    ruleset["ruleset"][""][0]["value"] = False
    response = web.set_ruleset("bulkwalk_hosts", ruleset)
    assert response is None

    try:
        changed = web.get_ruleset("bulkwalk_hosts")
        assert changed["ruleset"][""][0]["value"] is False
    finally:
        # revert it back
        del orig_ruleset["configuration_hash"]
        response = web.set_ruleset("bulkwalk_hosts", orig_ruleset)
        assert response is None
Example #11
0
def test_cfg(web, clear_config_caches, enable_debug):
    reload_config()
    yield

    #
    # Cleanup code
    #
    print("Cleaning up test config")

    if web.host_exists("mgmt-host"):
        web.delete_host("mgmt-host")

    if web.folder_exists("folder1"):
        web.delete_folder("folder1")

    web.set_ruleset(
        "management_board_config",
        {
            "ruleset": {
                "": [],  # -> folder
            }
        })

    web.activate_changes()
Example #12
0
def scenario_fixture(request, web, site):  # noqa: F811 # pylint: disable=redefined-outer-name
    core = request.param.core
    unreachable_enabled = request.param.unreachable_enabled
    site.set_core(core)

    try:
        print("Applying test config")

        web.add_host("notify-test-parent",
                     attributes={
                         "ipaddress": "127.0.0.1",
                     })

        web.add_host("notify-test-child",
                     attributes={
                         "ipaddress": "127.0.0.1",
                         "parents": ["notify-test-parent"],
                     })

        if unreachable_enabled:
            notification_options = 'd,u,r,f,s'
        else:
            notification_options = 'd,r,f,s'

        rule_result = web.get_ruleset("extra_host_conf:notification_options")
        rule_result["ruleset"] = {
            "": [{
                'condition': {},
                'options': {},
                'value': notification_options
            }]
        }
        web.set_ruleset("extra_host_conf:notification_options", rule_result)

        web.activate_changes()

        site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-parent" %
                          time.time())
        site.live.command("[%d] DISABLE_SVC_CHECK;notify-test-parent;PING" %
                          time.time())
        site.live.command(
            "[%d] DISABLE_SVC_CHECK;notify-test-parent;Check_MK Discovery" %
            time.time())

        site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-child" %
                          time.time())
        site.live.command("[%d] DISABLE_SVC_CHECK;notify-test-child;PING" %
                          time.time())
        site.live.command(
            "[%d] DISABLE_SVC_CHECK;notify-test-child;Check_MK Discovery" %
            time.time())

        site.live.command("[%d] DISABLE_FLAP_DETECTION" % time.time())

        yield request.param
    finally:
        #
        # Cleanup code
        #
        print("Cleaning up default config")

        site.live.command("[%d] ENABLE_FLAP_DETECTION" % time.time())
        site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-child" %
                          time.time())
        site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-parent" %
                          time.time())

        web.delete_host("notify-test-child")
        web.delete_host("notify-test-parent")

        web.activate_changes()
Example #13
0
def test_active_check_macros(test_cfg, site, web):
    macros = {
        "$HOSTADDRESS$":
        "127.0.0.1",
        "$HOSTNAME$":
        "test-host",
        "$_HOSTTAGS$":
        " ".join(
            sorted([
                "/wato/", "auto-piggyback", "ip-v4", "ip-v4-only", "lan",
                "no-agent", "no-snmp", "ping", "prod",
                "site:%s" % site.id
            ])),
        "$_HOSTADDRESS_4$":
        "127.0.0.1",
        "$_HOSTADDRESS_6$":
        "",
        "$_HOSTADDRESS_FAMILY$":
        "4",
        "$USER1$":
        "/omd/sites/%s/lib/nagios/plugins" % site.id,
        "$USER2$":
        "/omd/sites/%s/local/lib/nagios/plugins" % site.id,
        "$USER3$":
        site.id,
        "$USER4$":
        site.root,
    }

    def descr(var):
        return "Macro %s" % var.strip("$")

    ruleset = []
    for var, value in macros.items():
        ruleset.append({
            "value": {
                'service_description': descr(var),
                # TODO: Remove this once the GUI uses Python 3
                'command_line': api_str_type('echo "Output: %s"' % var),
            },
            "condition": {},
        })

    try:
        web.set_ruleset(
            "custom_checks",
            {
                "ruleset": {
                    # Main folder
                    "": ruleset,
                }
            })
        web.activate_changes()

        for var, value in macros.items():
            description = descr(var)
            logger.info(description)
            site.schedule_check("test-host", description, 0)

            logger.info("Get service row")
            row = site.live.query_row(
                "GET services\n"
                "Columns: host_name description state plugin_output has_been_checked\n"
                "Filter: host_name = test-host\n"
                "Filter: description = %s\n" % description)

            logger.info(row)
            name, description, state, plugin_output, has_been_checked = row

            assert name == "test-host"
            assert has_been_checked == 1
            assert state == 0

            expected_output = "Output: %s" % value
            # TODO: Cleanup difference between nagios/cmc
            if test_cfg.core == "nagios":
                expected_output = expected_output.strip()
                if var == "$_HOSTTAGS$":
                    splitted_output = plugin_output.split(" ")
                    plugin_output = splitted_output[0] + " " + " ".join(
                        sorted(splitted_output[1:]))


            assert expected_output == plugin_output, \
                "Macro %s has wrong value (%r instead of %r)" % (var, plugin_output, expected_output)

    finally:
        web.set_ruleset(
            "custom_checks",
            {
                "ruleset": {
                    "": [],  # -> folder
                }
            })
        web.activate_changes()
def scenario(request, web, site):
    core = request.param.core
    unreachable_enabled = request.param.unreachable_enabled
    site.set_core(core)

    try:
        print("Applying test config")

        web.add_host("notify-test-parent",
                     attributes={
                         "ipaddress": "127.0.0.1",
                     })

        web.add_host("notify-test-child",
                     attributes={
                         "ipaddress": "127.0.0.1",
                         "parents": ["notify-test-parent"],
                     })

        if unreachable_enabled:
            notification_options = 'd,u,r,f,s'
        else:
            notification_options = 'd,r,f,s'

        rule_result = web.get_ruleset("extra_host_conf:notification_options")
        rule_result["ruleset"] = {
            "": [{
                'condition': {},
                'options': {},
                'value': notification_options
            }]
        }
        web.set_ruleset("extra_host_conf:notification_options", rule_result)

        # Make nagios check more often for incoming commands and add more
        # details to the log
        site.write_file(
            "etc/nagios/nagios.d/zzz_test_unreachable_notifications.cfg",
            "log_passive_checks=1\n"
            "command_check_interval=1s\n")

        web.activate_changes()

        site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-parent" %
                          time.time())
        site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-child" %
                          time.time())
        site.live.command("[%d] DISABLE_FLAP_DETECTION" % time.time())

        yield request.param
    finally:
        #
        # Cleanup code
        #
        print("Cleaning up default config")

        site.live.command("[%d] ENABLE_FLAP_DETECTION" % time.time())
        site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-child" %
                          time.time())
        site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-parent" %
                          time.time())

        site.delete_file(
            "etc/nagios/nagios.d/zzz_test_unreachable_notifications.cfg")

        web.delete_host("notify-test-child")
        web.delete_host("notify-test-parent")

        web.activate_changes()