Ejemplo n.º 1
0
def test_get_cmk_passive_service_attributes(monkeypatch, hostname, result):
    ts = Scenario()
    ts.add_host("localhost")
    ts.add_host("blub")
    ts.set_option(
        "extra_service_conf",
        {
            "contact_groups": [
                ("ding", ["localhost"], ["CPU load$"]),
            ],
            "check_interval": [
                (40.0, ["blub"], ["Check_MK$"]),
                (33.0, ["localhost"], ["CPU load$"]),
            ],
        },
    )
    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config(hostname)
    check_mk_attrs = core_config.get_service_attributes(
        hostname, "Check_MK", config_cache)

    service = ConfiguredService(
        check_plugin_name=CheckPluginName("cpu_loads"),
        item=None,
        description="CPU load",
        parameters=TimespecificParameters(),
        discovered_parameters={},
        service_labels={},
    )
    service_spec = core_config.get_cmk_passive_service_attributes(
        config_cache, host_config, service, check_mk_attrs)
    assert service_spec == result
Ejemplo n.º 2
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"
Ejemplo n.º 3
0
def test_mgmt_board_ip_addresses(
    monkeypatch,
    protocol,
    cred_attribute,
    credentials,
    tags,
    host_attributes,
    ipaddresses,
    ipv6addresses,
    ip_address_result,
):
    hostname = "mgmt-host"
    ts = Scenario()
    ts.add_host(hostname, tags=tags)
    ts.set_option("host_attributes", {hostname: host_attributes})
    ts.set_option("ipaddresses", ipaddresses)
    ts.set_option("ipv6addresses", ipv6addresses)
    ts.set_option("management_protocol", {hostname: protocol})
    ts.set_option(cred_attribute, {hostname: credentials})

    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config(hostname)

    assert host_config.has_management_board
    assert host_config.management_protocol == protocol
    assert host_config.management_address == ip_address_result
    assert host_config.management_credentials == credentials
Ejemplo n.º 4
0
def test_get_check_table__static_checks_win(monkeypatch: MonkeyPatch) -> None:
    hostname_str = "df_host"
    hostname = HostName(hostname_str)
    plugin_name = CheckPluginName("df")
    item = "/snap/core/9066"

    ts = Scenario()
    ts.add_host(hostname)
    ts.set_option(
        "static_checks",
        {
            "filesystem": [
                ((str(plugin_name), item, {
                    "source": "static"
                }), [], [hostname_str]),
            ],
        },
    )
    ts.set_autochecks(
        hostname_str,
        [AutocheckEntry(plugin_name, item, {"source": "auto"}, {})])
    ts.apply(monkeypatch)

    chk_table = check_table.get_check_table(hostname)

    # assert check table is populated as expected
    assert len(chk_table) == 1
    # assert static checks won
    effective_params = chk_table[(plugin_name,
                                  item)].parameters.evaluate(lambda _: True)
    assert effective_params[
        "source"] == "static"  # type: ignore[index,call-overload]
Ejemplo n.º 5
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
Ejemplo n.º 6
0
    def test_attribute_defaults(self, monkeypatch):
        hostname = "testhost"
        ipaddress = "1.2.3.4"

        ts = Scenario()
        ts.add_host(hostname)
        ts.set_option("management_protocol", {hostname: "snmp"})
        ts.set_option(
            "host_attributes",
            {
                hostname: {
                    "management_address": ipaddress
                },
            },
        )
        ts.apply(monkeypatch)

        source = SNMPSource.management_board(
            HostName(hostname),
            ipaddress,
            force_cache_refresh=False,
            selected_sections=NO_SELECTION,
            on_scan_error=OnError.RAISE,
        )
        assert source.description == (
            "Management board - SNMP "
            "(Community: 'public', Bulk walk: no, Port: 161, Backend: Classic)"
        )
Ejemplo n.º 7
0
def test_analyse_host(monkeypatch):
    automation = automations.AutomationAnalyseHost()

    ts = Scenario()
    ts.add_host("test-host")
    ts.set_option(
        "host_labels",
        {
            "test-host": {
                "explicit": "ding",
            },
        },
    )
    ts.apply(monkeypatch)

    assert automation.execute(["test-host"]) == AnalyseHostResult(
        label_sources={
            "cmk/site": "discovered",
            "explicit": "explicit"
        },
        labels={
            "cmk/site": "NO_SITE",
            "explicit": "ding"
        },
    )
Ejemplo n.º 8
0
def test_compile_delayed_host_check(monkeypatch: MonkeyPatch,
                                    config_path: VersionedConfigPath) -> None:
    hostname = HostName("localhost")
    ts = Scenario()
    ts.add_host(hostname)
    ts.set_option("delay_precompile", True)
    config_cache = ts.apply(monkeypatch)

    # Ensure a host check is created
    monkeypatch.setattr(
        core_nagios,
        "_get_needed_plugin_names",
        lambda c: (set(), {CheckPluginName("uptime")}, set()),
    )

    source_file = core_nagios.HostCheckStore.host_check_source_file_path(
        config_path,
        hostname,
    )
    compiled_file = core_nagios.HostCheckStore.host_check_file_path(
        config_path, hostname)

    assert config.delay_precompile is True
    assert not source_file.exists()
    assert not compiled_file.exists()

    # Write the host check source file
    host_check = core_nagios._dump_precompiled_hostcheck(
        config_cache,
        config_path,
        hostname,
        verify_site_python=False,
    )
    assert host_check is not None
    core_nagios.HostCheckStore().write(config_path, hostname, host_check)

    # The compiled file path links to the source file until it has been executed for the first
    # time. Then the symlink is replaced with the compiled file
    assert source_file.exists()
    assert compiled_file.exists()
    assert compiled_file.resolve() == source_file

    # Expect the command to fail: We don't have the correct environment to execute it.
    # But this is no problem for our test, we only want to see the result of the compilation.
    assert (subprocess.run(
        ["python3", str(compiled_file)],
        shell=False,
        close_fds=True,
        check=False,
    ).returncode == 1)
    assert compiled_file.resolve() != source_file
    with compiled_file.open("rb") as f:
        assert f.read().startswith(importlib.util.MAGIC_NUMBER)
Ejemplo n.º 9
0
def test_get_check_table_of_mgmt_boards(
        monkeypatch: MonkeyPatch, hostname_str: str,
        expected_result: List[ServiceID]) -> None:
    hostname = HostName(hostname_str)
    autochecks: Mapping[str, Sequence[Service[LegacyCheckParameters]]] = {
        "mgmt-board-ipmi": [
            Service(
                CheckPluginName("mgmt_ipmi_sensors"),
                "TEMP X",
                "Management Interface: IPMI Sensor TEMP X",
                {},
            ),
        ],
        "ipmi-host": [
            Service(CheckPluginName("ipmi_sensors"), "TEMP Y",
                    "IPMI Sensor TEMP Y", {}),
        ],
    }

    ts = Scenario().add_host(
        "mgmt-board-ipmi",
        tags={
            "piggyback": "auto-piggyback",
            "networking": "lan",
            "address_family": "no-ip",
            "criticality": "prod",
            "snmp_ds": "no-snmp",
            "site": "heute",
            "agent": "no-agent",
        },
    )
    ts.add_host(
        "ipmi-host",
        tags={
            "piggyback": "auto-piggyback",
            "networking": "lan",
            "agent": "cmk-agent",
            "criticality": "prod",
            "snmp_ds": "no-snmp",
            "site": "heute",
            "address_family": "ip-v4-only",
        },
    )
    ts.set_option("management_protocol", {"mgmt-board-ipmi": "ipmi"})

    config_cache = ts.apply(monkeypatch)
    monkeypatch.setattr(config_cache, "get_autochecks_of",
                        lambda h: autochecks.get(h, []))

    assert list(
        check_table.get_check_table(hostname).keys()) == expected_result
Ejemplo n.º 10
0
def test_get_check_table_of_mgmt_boards(
        monkeypatch: MonkeyPatch, hostname_str: str,
        expected_result: List[ServiceID]) -> None:
    hostname = HostName(hostname_str)

    ts = Scenario()
    ts.add_host(
        "mgmt-board-ipmi",
        tags={
            "piggyback": "auto-piggyback",
            "networking": "lan",
            "address_family": "no-ip",
            "criticality": "prod",
            "snmp_ds": "no-snmp",
            "site": "heute",
            "agent": "no-agent",
        },
    )
    ts.add_host(
        "ipmi-host",
        tags={
            "piggyback": "auto-piggyback",
            "networking": "lan",
            "agent": "cmk-agent",
            "criticality": "prod",
            "snmp_ds": "no-snmp",
            "site": "heute",
            "address_family": "ip-v4-only",
        },
    )
    ts.set_option("management_protocol", {"mgmt-board-ipmi": "ipmi"})

    ts.set_autochecks(
        "mgmt-board-ipmi",
        [
            AutocheckEntry(CheckPluginName("mgmt_ipmi_sensors"), "TEMP X", {},
                           {})
        ],
    )
    ts.set_autochecks(
        "ipmi-host",
        [AutocheckEntry(CheckPluginName("ipmi_sensors"), "TEMP Y", {}, {})],
    )

    ts.apply(monkeypatch)

    assert list(
        check_table.get_check_table(hostname).keys()) == expected_result
Ejemplo n.º 11
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
Ejemplo n.º 12
0
def test_get_host_attributes(fixup_ip_lookup, monkeypatch):
    ts = Scenario()
    ts.add_host("test-host", tags={"agent": "no-agent"})
    ts.set_option(
        "host_labels",
        {
            "test-host": {
                "ding": "dong",
            },
        },
    )
    config_cache = ts.apply(monkeypatch)

    expected_attrs = {
        "_ADDRESSES_4": "",
        "_ADDRESSES_6": "",
        "_ADDRESS_4": "0.0.0.0",
        "_ADDRESS_6": "",
        "_ADDRESS_FAMILY": "4",
        "_FILENAME": "/wato/hosts.mk",
        "_TAGS":
        "/wato/ auto-piggyback ip-v4 ip-v4-only lan no-agent no-snmp prod site:unit",
        "__TAG_address_family": "ip-v4-only",
        "__TAG_agent": "no-agent",
        "__TAG_criticality": "prod",
        "__TAG_ip-v4": "ip-v4",
        "__TAG_networking": "lan",
        "__TAG_piggyback": "auto-piggyback",
        "__TAG_site": "unit",
        "__TAG_snmp_ds": "no-snmp",
        "__LABEL_ding": "dong",
        "__LABEL_cmk/site": "NO_SITE",
        "__LABELSOURCE_cmk/site": "discovered",
        "__LABELSOURCE_ding": "explicit",
        "address": "0.0.0.0",
        "alias": "test-host",
    }

    if cmk_version.is_managed_edition():
        expected_attrs["_CUSTOMER"] = "provider"

    attrs = core_config.get_host_attributes("test-host", config_cache)
    assert attrs == expected_attrs
Ejemplo n.º 13
0
def test_tcpdatasource_only_from(monkeypatch, res, reported, rule):
    # 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.
    ts = Scenario().add_host("hostname")
    ts.set_option("agent_config", {"only_from": [rule]} if rule else {})
    config_cache = ts.apply(monkeypatch)

    source = TCPSource(HostName("hostname"), "ipaddress")
    monkeypatch.setattr(config_cache, "host_extra_conf",
                        lambda host, ruleset: ruleset)

    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(reported) == res
Ejemplo n.º 14
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
Ejemplo n.º 15
0
def test_get_cmk_passive_service_attributes(monkeypatch, hostname, result):
    ts = Scenario().add_host("localhost")
    ts.add_host("blub")
    ts.set_option(
        "extra_service_conf",
        {
            "contact_groups": [
                ("ding", ["localhost"], ["CPU load$"]),
            ],
            "check_interval": [
                (40.0, ["blub"], ["Check_MK$"]),
                (33.0, ["localhost"], ["CPU load$"]),
            ],
        },
    )
    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config(hostname)
    check_mk_attrs = core_config.get_service_attributes(
        hostname, "Check_MK", config_cache)

    service = Service(CheckPluginName("cpu_loads"), None, "CPU load", {})
    service_spec = core_config.get_cmk_passive_service_attributes(
        config_cache, host_config, service, check_mk_attrs)
    assert service_spec == result
Ejemplo n.º 16
0
def test_mgmt_disabled(monkeypatch):
    ts = Scenario()
    ts.add_host("mgmt-host")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": None})
    ts.set_option("host_attributes",
                  {"mgmt-host": {
                      "management_address": "127.0.0.1"
                  }})
    ts.set_option("management_snmp_credentials", {"mgmt-host": "HOST"})

    config_cache = ts.apply(monkeypatch)
    host_config = config_cache.get_host_config("mgmt-host")
    assert host_config.has_management_board is False
    assert host_config.management_protocol is None
    assert host_config.management_address == "127.0.0.1"
    assert host_config.management_credentials is None
Ejemplo n.º 17
0
def test_get_labels_of_host(monkeypatch):
    automation = automations.AutomationGetLabelsOf()

    ts = Scenario().add_host("test-host")
    ts.set_option(
        "host_labels",
        {
            "test-host": {
                "explicit": "ding",
            },
        },
    )
    ts.apply(monkeypatch)

    assert automation.execute(["host", "test-host"]) == GetLabelsOfResult({
        "label_sources": {
            "cmk/site": "discovered",
            "explicit": "explicit"
        },
        "labels": {
            "cmk/site": "NO_SITE",
            "explicit": "ding"
        },
    })
Ejemplo n.º 18
0
def test_create_nagios_host_spec(hostname_str: str, result: Dict[str, str],
                                 monkeypatch: MonkeyPatch) -> None:
    if cmk_version.is_managed_edition():
        result = result.copy()
        result["_CUSTOMER"] = "provider"

    ts = Scenario()
    ts.add_host(HostName("localhost"))
    ts.add_host(HostName("host2"))
    ts.add_cluster(HostName("cluster1"))

    ts.add_cluster(HostName("cluster2"), nodes=["node1", "node2"])
    ts.add_host(HostName("node1"))
    ts.add_host(HostName("node2"))
    ts.add_host(HostName("switch"))
    ts.set_option(
        "ipaddresses",
        {
            HostName("node1"): "127.0.0.1",
            HostName("node2"): "127.0.0.2",
        },
    )

    ts.set_option(
        "extra_host_conf",
        {
            "alias": [
                ("lOCALhost", ["localhost"]),
            ],
        },
    )

    ts.set_option(
        "extra_host_conf",
        {
            "alias": [
                ("lOCALhost", ["host2"]),
                ("CLUSTer", ["cluster2"]),
            ],
            "parents": [
                ("switch", ["node1", "node2"]),
            ],
        },
    )

    hostname = HostName(hostname_str)
    outfile = io.StringIO()
    cfg = core_nagios.NagiosConfig(outfile, [hostname])

    config_cache = ts.apply(monkeypatch)
    host_attrs = core_config.get_host_attributes(hostname, config_cache)

    host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache,
                                                     hostname, host_attrs)
    assert host_spec == result
Ejemplo n.º 19
0
def test_mgmt_explicit_settings(monkeypatch, protocol, cred_attribute,
                                credentials):
    ts = Scenario()
    ts.add_host("mgmt-host")
    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": 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 == credentials
Ejemplo n.º 20
0
def test_mgmt_explicit_address(monkeypatch):
    ts = Scenario()
    ts.add_host("mgmt-host")
    ts.set_option("ipaddresses", {"mgmt-host": "127.0.0.1"})
    ts.set_option("management_protocol", {"mgmt-host": "snmp"})
    ts.set_option("host_attributes",
                  {"mgmt-host": {
                      "management_address": "127.0.0.2"
                  }})

    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.2"
    assert host_config.management_credentials == "public"
Ejemplo n.º 21
0
def scenario_fixture(monkeypatch):
    test_hosts = ["ds-test-host1", "ds-test-host2", "ds-test-node1", "ds-test-node2"]

    ts = Scenario()

    if is_enterprise_repo():
        ts.set_option("monitoring_core", "cmc")
    else:
        ts.set_option("monitoring_core", "nagios")

    for h in test_hosts:
        ts.add_host(h)

    ts.set_option("ipaddresses", dict((h, "127.0.0.1") for h in test_hosts))
    ts.add_cluster("ds-test-cluster1", nodes=["ds-test-node1", "ds-test-node2"])
    ts.fake_standard_linux_agent_output(*test_hosts)

    return ts.apply(monkeypatch)
Ejemplo n.º 22
0
 def scenario(self, hostname, ipaddress, monkeypatch):
     ts = Scenario()
     ts.add_host(hostname)
     ts.set_option("ipaddresses", {hostname: ipaddress})
     ts.apply(monkeypatch)
     return ts
Ejemplo n.º 23
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
Ejemplo n.º 24
0
def test_get_check_table_of_static_check(
    monkeypatch: MonkeyPatch, hostname_str: str, expected_result: List[ServiceID]
) -> None:
    hostname = HostName(hostname_str)
    static_checks = {
        "df_host": [
            Service(
                CheckPluginName("df"),
                "/snap/core/9066",
                "Filesystem /snap/core/9066",
                [
                    {"tp_values": [("24X7", {"inodes_levels": None})], "tp_default_value": {}},
                    {
                        "trend_range": 24,
                        "show_levels": "onmagic",
                        "inodes_levels": (10.0, 5.0),
                        "magic_normsize": 20,
                        "show_inodes": "onlow",
                        "levels": (80.0, 90.0),
                        "show_reserved": False,
                        "levels_low": (50.0, 60.0),
                        "trend_perfdata": True,
                    },
                ],
            ),
        ],
        "df_host_1": [
            Service(
                CheckPluginName("df"),
                "/snap/core/9067",
                "Filesystem /snap/core/9067",
                {
                    "trend_range": 24,
                    "show_levels": "onmagic",
                    "inodes_levels": (10.0, 5.0),
                    "magic_normsize": 20,
                    "show_inodes": "onlow",
                    "levels": (80.0, 90.0),
                    "tp_default_value": {"levels": (87.0, 90.0)},
                    "show_reserved": False,
                    "tp_values": [("24X7", {"inodes_levels": None})],
                    "levels_low": (50.0, 60.0),
                    "trend_perfdata": True,
                },
            )
        ],
        "df_host_2": [
            Service(CheckPluginName("df"), "/snap/core/9068", "Filesystem /snap/core/9068", None)
        ],
    }

    ts = Scenario().add_host(hostname, tags={"criticality": "test"})
    ts.add_host("df_host")
    ts.add_host("df_host_1")
    ts.add_host("df_host_2")
    ts.set_option(
        "static_checks",
        {
            "filesystem": [
                (
                    (
                        "df",
                        "/snap/core/9066",
                        [
                            {
                                "tp_values": [("24X7", {"inodes_levels": None})],
                                "tp_default_value": {},
                            },
                            {
                                "trend_range": 24,
                                "show_levels": "onmagic",
                                "inodes_levels": (10.0, 5.0),
                                "magic_normsize": 20,
                                "show_inodes": "onlow",
                                "levels": (80.0, 90.0),
                                "show_reserved": False,
                                "levels_low": (50.0, 60.0),
                                "trend_perfdata": True,
                            },
                        ],
                    ),
                    [],
                    ["df_host"],
                ),
                (
                    (
                        "df",
                        "/snap/core/9067",
                        [
                            {
                                "tp_values": [("24X7", {"inodes_levels": None})],
                                "tp_default_value": {},
                            },
                            {
                                "trend_range": 24,
                                "show_levels": "onmagic",
                                "inodes_levels": (10.0, 5.0),
                                "magic_normsize": 20,
                                "show_inodes": "onlow",
                                "levels": (80.0, 90.0),
                                "show_reserved": False,
                                "levels_low": (50.0, 60.0),
                                "trend_perfdata": True,
                            },
                        ],
                    ),
                    [],
                    ["df_host_1"],
                ),
                (("df", "/snap/core/9068", None), [], ["df_host_2"]),
            ],
        },
    )

    config_cache = ts.apply(monkeypatch)
    monkeypatch.setattr(config_cache, "get_autochecks_of", lambda h: static_checks.get(h, []))

    assert list(check_table.get_check_table(hostname).keys()) == expected_result
Ejemplo n.º 25
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
Ejemplo n.º 26
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
Ejemplo n.º 27
0
def fixture_core_scenario(monkeypatch):
    ts = Scenario().add_host("test-host")
    ts.set_option("ipaddresses", {"test-host": "127.0.0.1"})
    return ts.apply(monkeypatch)