예제 #1
0
def test_mgmt_config_ruleset_overidden_by_explicit_setting(
        monkeypatch, protocol, cred_attribute, host_credentials,
        ruleset_credentials):
    ts = Scenario()
    ts.set_ruleset(
        "management_board_config",
        [
            {
                "condition": {},
                "options": {},
                "value": (protocol, ruleset_credentials),
            },
        ],
    )

    ts.add_host("mgmt-host", host_path="/wato/folder1/hosts.mk")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": protocol})
    ts.set_option(cred_attribute, {"mgmt-host": host_credentials})

    config_cache = ts.apply(monkeypatch)
    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 == host_credentials
예제 #2
0
def test_get_effective_service_level(monkeypatch):
    ts = Scenario()
    ts.add_host("testhost1")
    ts.add_host("testhost2")
    ts.add_host("testhost3")
    ts.set_ruleset(
        "host_service_levels",
        [
            (10, [], ["testhost2"], {}),
            (2, [], ["testhost2"], {}),
        ],
    )
    ts.set_ruleset(
        "service_service_levels",
        [
            (33, [], ["testhost1"], ["CPU load$"], {}),
        ],
    )
    ts.apply(monkeypatch)

    with plugin_contexts.current_service(CheckPluginName("cpu_loads"), "CPU load"):

        with plugin_contexts.current_host("testhost1"):
            assert check_api.get_effective_service_level() == 33

        with plugin_contexts.current_host("testhost2"):
            assert check_api.get_effective_service_level() == 10

        with plugin_contexts.current_host("testhost3"):
            assert check_api.get_effective_service_level() == 0
예제 #3
0
def test_get_labels_of_service(monkeypatch):
    automation = automations.AutomationGetLabelsOf()

    ts = Scenario().add_host("test-host")
    ts.set_ruleset(
        "service_label_rules",
        [
            ({
                "label1": "val1"
            }, [], config.ALL_HOSTS, ["CPU load$"], {}),
            ({
                "label2": "val2"
            }, [], config.ALL_HOSTS, ["CPU load$"], {}),
        ],
    )
    ts.apply(monkeypatch)

    assert automation.execute(["service", "test-host",
                               "CPU load"]) == GetLabelsOfResult({
                                   "labels": {
                                       "label1": "val1",
                                       "label2": "val2"
                                   },
                                   "label_sources": {
                                       "label1": "ruleset",
                                       "label2": "ruleset"
                                   },
                               })
예제 #4
0
def test_cluster_ignores_nodes_parameters(monkeypatch: MonkeyPatch) -> None:

    node = HostName("node")
    cluster = HostName("cluster")

    service_id = CheckPluginName("smart_temp"), "auto-clustered"

    ts = Scenario()
    ts.add_host("node")
    ts.add_cluster("cluster", nodes=["node"])
    ts.set_ruleset(
        "clustered_services",
        [([], ["node"], ["Temperature SMART auto-clustered$"])],
    )
    ts.set_autochecks("node", [Service(*service_id, "Temperature SMART auto-clustered", {})])
    ts.apply(monkeypatch)

    # a rule for the node:
    monkeypatch.setattr(
        config,
        "_update_with_configured_check_parameters",
        lambda host, plugin, item, params, configured_params: {"levels_for_node": (1, 2), **params}
        if host == node
        else params,
    )

    clustered_service = check_table.get_check_table(cluster)[service_id]
    assert clustered_service.parameters == {"levels": (35, 40)}
예제 #5
0
def test_cluster_ignores_nodes_parameters(monkeypatch: MonkeyPatch) -> None:

    node = HostName("node")
    cluster = HostName("cluster")

    service_id = CheckPluginName("smart_temp"), "auto-clustered"

    ts = Scenario()
    ts.add_host("node")
    ts.add_cluster("cluster", nodes=["node"])
    ts.set_ruleset(
        "clustered_services",
        [([], ["node"], ["Temperature SMART auto-clustered$"])],
    )
    ts.set_autochecks("node", [AutocheckEntry(*service_id, {}, {})])
    ts.apply(monkeypatch)

    # a rule for the node:
    monkeypatch.setattr(
        config,
        "_get_configured_parameters",
        lambda host, plugin, item:
        (TimespecificParameters((TimespecificParameterSet.from_parameters(
            {"levels_for_node": (1, 2)}), ))
         if host == node else TimespecificParameters()),
    )

    clustered_service = check_table.get_check_table(cluster)[service_id]
    assert clustered_service.parameters.entries == (
        TimespecificParameterSet.from_parameters({"levels": (35, 40)}), )
예제 #6
0
def test_mgmt_config_ruleset_order(monkeypatch, protocol, cred_attribute,
                                   folder_credentials, ruleset_credentials):
    ts = Scenario()
    ts.set_ruleset(
        "management_board_config",
        [
            {
                "condition": {},
                "options": {},
                "value": ("snmp", "RULESET1"),
            },
            {
                "condition": {},
                "options": {},
                "value": ("snmp", "RULESET2"),
            },
        ],
    )

    ts.add_host("mgmt-host", host_path="/wato/folder1/hosts.mk")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": "snmp"})

    config_cache = ts.apply(monkeypatch)
    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"
예제 #7
0
def make_scenario(hostname, tags):
    ts = Scenario().add_host(hostname, tags=tags)
    ts.set_ruleset(
        "datasource_programs",
        [
            ("echo 1", [], ["ds-host-14", "all-agents-host", "all-special-host"], {}),
        ],
    )
    ts.set_option(
        "special_agents",
        {
            "jolokia": [
                (
                    {},
                    [],
                    [
                        "special-host-14",
                        "all-agents-host",
                        "all-special-host",
                    ],
                    {},
                ),
            ]
        },
    )
    return ts
예제 #8
0
def test_get_effective_service_level(monkeypatch):
    ts = Scenario().add_host("testhost1")
    ts.add_host("testhost2")
    ts.add_host("testhost3")
    ts.set_ruleset(
        "host_service_levels",
        [
            (10, [], ["testhost2"], {}),
            (2, [], ["testhost2"], {}),
        ],
    )
    ts.set_ruleset(
        "service_service_levels",
        [
            (33, [], ["testhost1"], ["CPU load$"], {}),
        ],
    )
    ts.apply(monkeypatch)

    with plugin_contexts.current_service(
            Service(
                item=None,
                check_plugin_name=CheckPluginName("cpu_loads"),
                description="CPU load",
                parameters={},
            )):

        with plugin_contexts.current_host("testhost1"):
            assert check_api.get_effective_service_level() == 33

        with plugin_contexts.current_host("testhost2"):
            assert check_api.get_effective_service_level() == 10

        with plugin_contexts.current_host("testhost3"):
            assert check_api.get_effective_service_level() == 0
예제 #9
0
def test_sanitize_snmp_encoding(monkeypatch, encoding, columns, expected):
    ts = Scenario().add_host("localhost")
    ts.set_ruleset(
        "snmp_character_encodings",
        [
            (encoding, [], config.ALL_HOSTS, {}),
        ],
    )
    config_cache = ts.apply(monkeypatch)

    snmp_config = config_cache.get_host_config("localhost").snmp_config("")
    assert snmp_table._sanitize_snmp_encoding(columns, snmp_config) == expected
예제 #10
0
def test_is_bulkwalk_host(monkeypatch):
    ts = Scenario()
    ts.set_ruleset(
        "bulkwalk_hosts",
        [
            ([], ["localhost"], {}),
        ],
    )
    ts.add_host("abc")
    ts.add_host("localhost")
    config_cache = ts.apply(monkeypatch)
    assert config_cache.get_host_config("abc").snmp_config("").is_bulkwalk_host is False
    assert config_cache.get_host_config("localhost").snmp_config("").is_bulkwalk_host is True
예제 #11
0
def test_is_classic_at_snmp_v1_host(monkeypatch):
    ts = Scenario()
    ts.set_ruleset(
        "bulkwalk_hosts",
        [
            ([], ["bulkwalk_h"], {}),
        ],
    )
    ts.set_ruleset(
        "snmpv2c_hosts",
        [
            ([], ["v2c_h"], {}),
        ],
    )
    ts.add_host("bulkwalk_h")
    ts.add_host("v2c_h")
    ts.add_host("not_included")
    monkeypatch.setattr(config.HostConfig, "_is_inline_backend_supported", lambda _: True)

    config_cache = ts.apply(monkeypatch)

    # not bulkwalk and not v2c
    assert (
        config_cache.get_host_config("not_included").snmp_config("").snmp_backend
        == SNMPBackendEnum.CLASSIC
    )

    assert (
        config_cache.get_host_config("bulkwalk_h").snmp_config("").snmp_backend
        == SNMPBackendEnum.INLINE
    )

    assert (
        config_cache.get_host_config("v2c_h").snmp_config("").snmp_backend == SNMPBackendEnum.INLINE
    )

    # credentials is v3 -> INLINE
    monkeypatch.setattr(
        config.HostConfig,
        "_snmp_credentials",
        lambda _: (
            "a",
            "p",
        ),
    )
    assert (
        config_cache.get_host_config("not_included").snmp_config("").snmp_backend
        == SNMPBackendEnum.INLINE
    )
예제 #12
0
def test_tcpdatasource_restricted_address_mismatch(
    monkeypatch,
    restricted_address_mismatch_state,
    only_from,
    rule,
    res,
):
    # TODO(ml): Not only is this white box testing but all these instantiations
    #           before the summarizer obscure the purpose of the test.  This is
    #           way too complicated.  Test the `AgentSummarizerDefault` directly
    #           in `tests.unit.cmk.core_helpers.test_summarizers` instead.
    hostname = HostName("hostname")

    ts = Scenario().add_host(hostname)
    ts.set_option("agent_config",
                  {"only_from": [(rule, [], [str(hostname)], {})]})

    if restricted_address_mismatch_state is not None:
        ts.set_ruleset(
            "check_mk_exit_status",
            [
                (
                    {
                        "restricted_address_mismatch":
                        restricted_address_mismatch_state,
                    },
                    [],
                    [str(hostname)],
                    {},
                ),
            ],
        )

    ts.apply(monkeypatch)
    source = TCPSource(hostname, "ipaddress")

    summarizer = AgentSummarizerDefault(
        source.exit_spec,
        is_cluster=source.host_config.is_cluster,
        agent_min_version=0,
        agent_target_version=source.host_config.agent_target_version,
        only_from=source.host_config.only_from,
    )

    assert summarizer._check_only_from(only_from) == res
예제 #13
0
def test_check_table__get_static_check_entries(
    monkeypatch: MonkeyPatch, check_group_parameters: LegacyCheckParameters
) -> None:
    hostname = HostName("hostname")
    static_parameters = {
        "levels": (1, 2, 3, 4),
    }
    static_checks: Dict[str, List] = {
        "ps": [(("ps", "item", static_parameters), [], [hostname], {})],
    }

    ts = Scenario().add_host(hostname)
    ts.set_option("static_checks", static_checks)

    ts.set_ruleset(
        "checkgroup_parameters",
        {
            "ps": [(check_group_parameters, [hostname], [], {})],
        },
    )

    config_cache = ts.apply(monkeypatch)

    monkeypatch.setattr(
        agent_based_register,
        "get_check_plugin",
        lambda cpn: CheckPlugin(
            CheckPluginName("ps"),
            [],
            "Process item",
            None,  # type: ignore
            None,  # type: ignore
            None,  # type: ignore
            None,  # type: ignore
            None,  # type: ignore
            {},
            "ps",  # type: ignore
            None,  # type: ignore
            None,  # type: ignore
        ),
    )

    host_config = config_cache.get_host_config(hostname)
    static_check_parameters = [
        service.parameters
        for service in check_table._get_static_check_entries(config_cache, host_config)
    ]

    entries = config._get_checkgroup_parameters(
        config_cache,
        hostname,
        "ps",
        "item",
        "Process item",
    )

    assert len(entries) == 1
    assert entries[0] == check_group_parameters

    assert len(static_check_parameters) == 1
    static_check_parameter = static_check_parameters[0]
    assert static_check_parameter == static_parameters
예제 #14
0
def test_get_check_table(
    monkeypatch: MonkeyPatch, hostname_str: str, expected_result: HostCheckTable
) -> None:
    hostname = HostName(hostname_str)
    autochecks = {
        "ping-host": [
            Service(
                CheckPluginName("smart_temp"),
                "bla",
                "Temperature SMART bla",
                {},
            )
        ],
        "autocheck-overwrite": [
            Service(
                CheckPluginName("smart_temp"),
                "/dev/sda",
                "Temperature SMART /dev/sda",
                {"is_autocheck": True},
            ),
            Service(
                CheckPluginName("smart_temp"),
                "/dev/sdb",
                "Temperature SMART /dev/sdb",
                {"is_autocheck": True},
            ),
        ],
        "ignore-not-existing-checks": [
            Service(
                CheckPluginName("bla_blub"),
                "ITEM",
                "Blub ITEM",
                {},
            ),
        ],
        "node1": [
            Service(
                CheckPluginName("smart_temp"),
                "auto-clustered",
                "Temperature SMART auto-clustered",
                {},
            ),
            Service(
                CheckPluginName("smart_temp"),
                "auto-not-clustered",
                "Temperature SMART auto-not-clustered",
                {},
            ),
        ],
    }

    ts = Scenario().add_host(hostname, tags={"criticality": "test"})
    ts.add_host("ping-host", tags={"agent": "no-agent"})
    ts.add_host("node1")
    ts.add_cluster("cluster1", nodes=["node1"])
    ts.set_option(
        "static_checks",
        {
            "temperature": [
                (("smart.temp", "/dev/sda", {}), [], ["no-autochecks", "autocheck-overwrite"]),
                (("blub.bla", "ITEM", {}), [], ["ignore-not-existing-checks"]),
                (("smart.temp", "ITEM1", {}), [], ["ignore-disabled-rules"], {"disabled": True}),
                (("smart.temp", "ITEM2", {}), [], ["ignore-disabled-rules"]),
                (("smart.temp", "/dev/sda", {"rule": 1}), [], ["static-check-overwrite"]),
                (("smart.temp", "/dev/sda", {"rule": 2}), [], ["static-check-overwrite"]),
                (("smart.temp", "static-node1", {}), [], ["node1"]),
                (("smart.temp", "static-cluster", {}), [], ["cluster1"]),
            ]
        },
    )
    ts.set_ruleset(
        "clustered_services",
        [
            ([], ["node1"], ["Temperature SMART auto-clustered$"]),
        ],
    )
    config_cache = ts.apply(monkeypatch)
    monkeypatch.setattr(config_cache, "get_autochecks_of", lambda h: autochecks.get(h, []))

    assert check_table.get_check_table(hostname) == expected_result
예제 #15
0
def test_get_check_table(monkeypatch: MonkeyPatch, hostname_str: str,
                         expected_result: HostCheckTable) -> None:
    hostname = HostName(hostname_str)

    ts = Scenario()
    ts.add_host(hostname, tags={"criticality": "test"})
    ts.add_host("ping-host", tags={"agent": "no-agent"})
    ts.add_host("node1")
    ts.add_cluster("cluster1", nodes=["node1"])
    ts.set_option(
        "static_checks",
        {
            "temperature": [
                (("smart.temp", "/dev/sda", {}), [],
                 ["no-autochecks", "autocheck-overwrite"]),
                (("blub.bla", "ITEM", {}), [], ["ignore-not-existing-checks"]),
                (("smart.temp", "ITEM1", {}), [], ["ignore-disabled-rules"], {
                    "disabled": True
                }),
                (("smart.temp", "ITEM2", {}), [], ["ignore-disabled-rules"]),
                (("smart.temp", "/dev/sda", {
                    "rule": 1
                }), [], ["static-check-overwrite"]),
                (("smart.temp", "/dev/sda", {
                    "rule": 2
                }), [], ["static-check-overwrite"]),
                (("smart.temp", "static-node1", {}), [], ["node1"]),
                (("smart.temp", "static-cluster", {}), [], ["cluster1"]),
            ]
        },
    )
    ts.set_ruleset(
        "clustered_services",
        [
            ([], ["node1"], ["Temperature SMART auto-clustered$"]),
        ],
    )
    ts.set_autochecks(
        "ping-host",
        [
            AutocheckEntry(CheckPluginName("smart_temp"), "bla", {}, {}),
        ],
    )
    ts.set_autochecks(
        "autocheck-overwrite",
        [
            AutocheckEntry(CheckPluginName("smart_temp"), "/dev/sda",
                           {"is_autocheck": True}, {}),
            AutocheckEntry(CheckPluginName("smart_temp"), "/dev/sdb",
                           {"is_autocheck": True}, {}),
        ],
    )
    ts.set_autochecks(
        "ignore-not-existing-checks",
        [
            AutocheckEntry(CheckPluginName("bla_blub"), "ITEM", {}, {}),
        ],
    )
    ts.set_autochecks(
        "node1",
        [
            AutocheckEntry(CheckPluginName("smart_temp"), "auto-clustered", {},
                           {}),
            AutocheckEntry(CheckPluginName("smart_temp"), "auto-not-clustered",
                           {}, {}),
        ],
    )

    ts.apply(monkeypatch)

    assert set(check_table.get_check_table(hostname)) == set(expected_result)
    for key, value in check_table.get_check_table(hostname).items():
        assert key in expected_result
        assert expected_result[key] == value