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)}
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]
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)}), )
def test_ruleset_matcher_get_service_ruleset_values_labels( monkeypatch: MonkeyPatch, hostname: HostName, service_description: str, expected_result: Sequence[str], ) -> None: ts = Scenario() ts.add_host(HostName("host1")) ts.set_autochecks( HostName("host1"), [ Service( CheckPluginName("cpu_load"), None, "CPU load", "{}", service_labels={ "os": ServiceLabel("os", "linux"), "abc": ServiceLabel("abc", "xä"), "hu": ServiceLabel("hu", "ha"), }, ) ], ) ts.add_host(HostName("host2")) ts.set_autochecks( HostName("host2"), [ Service( CheckPluginName("cpu_load"), None, "CPU load", "{}", service_labels={}, ), ], ) config_cache = ts.apply(monkeypatch) matcher = config_cache.ruleset_matcher assert ( list( matcher.get_service_ruleset_values( config_cache.ruleset_match_object_of_service( hostname, ServiceName(service_description) ), ruleset=service_label_ruleset, is_binary=False, ) ) == expected_result )
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
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