def test_basic_host_ruleset_get_merged_dict_values(monkeypatch): ts = Scenario().add_host("abc") ts.add_host("abc") ts.add_host("xyz") ts.add_host("host1") ts.add_host("host2") config_cache = ts.apply(monkeypatch) matcher = config_cache.ruleset_matcher assert matcher.get_host_ruleset_merged_dict(RulesetMatchObject( host_name="abc", service_description=None), ruleset=dict_ruleset) == {} assert matcher.get_host_ruleset_merged_dict(RulesetMatchObject( host_name="xyz", service_description=None), ruleset=dict_ruleset) == {} assert matcher.get_host_ruleset_merged_dict(RulesetMatchObject( host_name="host1", service_description=None), ruleset=dict_ruleset) == { "hu": "BLA", "ho": "BLA", "he": "BLUB", } assert matcher.get_host_ruleset_merged_dict(RulesetMatchObject( host_name="host2", service_description=None), ruleset=dict_ruleset) == { "hu": "BLUB", "ho": "BLA", "he": "BLUB", }
def test_snmp_ipaddress_from_mgmt_board(monkeypatch): hostname = "testhost" ipaddress = "1.2.3.4" def fake_lookup_ip_address(host_config, family=None, for_mgmt_board=True): return ipaddress ts = Scenario() ts.add_host(hostname) ts.set_option("management_protocol", {hostname: "snmp"}) ts.apply(monkeypatch) monkeypatch.setattr(ip_lookup, "lookup_ip_address", fake_lookup_ip_address) monkeypatch.setattr(config, "host_attributes", { hostname: { "management_address": ipaddress, }, }) host_config = config.get_config_cache().get_host_config(hostname) source = SNMPManagementBoardDataSource( hostname, ip_lookup.lookup_mgmt_board_ip_address(host_config), ) assert source._host_config.management_address == ipaddress assert source.ipaddress == ipaddress
def test_get_check_table_of_mgmt_boards(monkeypatch, hostname, expected_result): autochecks = { "mgmt-board-ipmi": [ Service("mgmt_ipmi_sensors", "TEMP X", "Management Interface: IPMI Sensor TEMP X", {}), ], "ipmi-host": [Service("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, [])) CheckManager().load(["mgmt_ipmi_sensors", "ipmi_sensors"]) assert check_table.get_check_table(hostname).keys() == expected_result
def test_get_section_content(monkeypatch, hostname, nodes, host_entries, cluster_mapping, service_descr, expected_result): ts = Scenario() if nodes is None: ts.add_host(hostname) else: ts.add_cluster(hostname, nodes=nodes) for node in nodes or []: ts.add_host(node) config_cache = ts.apply(monkeypatch) def host_of_clustered_service(hostname, service_description): return cluster_mapping[hostname] multi_host_sections = MultiHostSections() for nodename, node_section_content in host_entries: multi_host_sections.add_or_get_host_sections( nodename, "127.0.0.1", AgentHostSections(sections={"check_plugin_name": node_section_content})) monkeypatch.setattr(ip_lookup, "lookup_ip_address", lambda h: "127.0.0.1") monkeypatch.setattr(config_cache, "host_of_clustered_service", host_of_clustered_service) section_content = multi_host_sections.get_section_content(hostname, "127.0.0.1", "check_plugin_name", False, service_description=service_descr) assert expected_result == section_content,\ "Section content: Expected '%s' but got '%s'" % (expected_result, section_content)
def test_host_ruleset_match_object_of_service(monkeypatch): ts = Scenario() ts.add_host("xyz") ts.add_host("test-host", tags={"agent": "no-agent"}) ts.set_autochecks("test-host", [ Service("cpu.load", None, "CPU load", "{}", service_labels=DiscoveredServiceLabels(ServiceLabel(u"abc", u"xä"),)) ]) config_cache = ts.apply(monkeypatch) obj = config_cache.ruleset_match_object_of_service("xyz", "bla blä") assert isinstance(obj, RulesetMatchObject) assert obj.to_dict() == { "host_name": "xyz", "service_description": "bla blä", "service_labels": {}, "service_cache_id": ('bla bl\xc3\xa4', None), } obj = config_cache.ruleset_match_object_of_service("test-host", "CPU load") service_labels = {u"abc": u"xä"} assert isinstance(obj, RulesetMatchObject) assert obj.to_dict() == { "host_name": "test-host", "service_description": "CPU load", "service_labels": service_labels, "service_cache_id": ('CPU load', obj._generate_hash(service_labels)), }
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"
def test_host_config_labels(monkeypatch): ts = Scenario() ts.set_ruleset("host_label_rules", [ ({ "from-rule": "rule1" }, ["no-agent"], config.ALL_HOSTS, {}), ({ "from-rule2": "rule2" }, ["no-agent"], config.ALL_HOSTS, {}), ]) ts.add_host("test-host", tags={"agent": "no-agent"}, labels={"explicit": "ding"}) ts.add_host("xyz") config_cache = ts.apply(monkeypatch) cfg = config_cache.get_host_config("xyz") assert cfg.labels == {} cfg = config_cache.get_host_config("test-host") assert cfg.labels == { "explicit": "ding", "from-rule": "rule1", "from-rule2": "rule2", } assert cfg.label_sources == { "explicit": "explicit", "from-rule": "ruleset", "from-rule2": "ruleset", }
def test_labels_of_service(monkeypatch): ts = Scenario() ts.set_ruleset("service_label_rules", [ ({ "label1": "val1" }, ["no-agent"], config.ALL_HOSTS, ["CPU load$"], {}), ({ "label2": "val2" }, ["no-agent"], config.ALL_HOSTS, ["CPU load$"], {}), ]) ts.add_host("test-host", tags={"agent": "no-agent"}) config_cache = ts.apply(monkeypatch) assert config_cache.labels_of_service("xyz", "CPU load") == {} assert config_cache.label_sources_of_service("xyz", "CPU load") == {} assert config_cache.labels_of_service("test-host", "CPU load") == { "label1": "val1", "label2": "val2", } assert config_cache.label_sources_of_service("test-host", "CPU load") == { "label1": "ruleset", "label2": "ruleset", }
def test_host_tags_of_host(monkeypatch): ts = Scenario() ts.add_host("test-host", tags={"agent": "no-agent"}) ts.add_host("xyz") config_cache = ts.apply(monkeypatch) cfg = config_cache.get_host_config("xyz") assert cfg.tag_groups == { 'address_family': 'ip-v4-only', 'agent': 'cmk-agent', 'criticality': 'prod', 'ip-v4': 'ip-v4', 'networking': 'lan', 'piggyback': 'auto-piggyback', 'site': 'unit', 'snmp_ds': 'no-snmp', 'tcp': 'tcp', } assert config_cache.tags_of_host("xyz") == cfg.tag_groups cfg = config_cache.get_host_config("test-host") assert cfg.tag_groups == { 'address_family': 'ip-v4-only', 'agent': 'no-agent', 'criticality': 'prod', 'ip-v4': 'ip-v4', 'networking': 'lan', 'piggyback': 'auto-piggyback', 'site': 'unit', 'snmp_ds': 'no-snmp', } assert config_cache.tags_of_host("test-host") == cfg.tag_groups
def test_lookup_mgmt_board_ip_address_ipv4_host(monkeypatch, hostname, tags, result_address): ts = Scenario() ts.add_host(hostname, tags=tags) ts.apply(monkeypatch) host_config = config.get_config_cache().get_host_config(hostname) assert config.lookup_mgmt_board_ip_address(host_config) == result_address
def test_lookup_mgmt_board_ip_address_unresolveable(monkeypatch, tags, family): hostname = "unresolveable-hostname" ts = Scenario() ts.add_host(hostname, tags=tags) ts.apply(monkeypatch) host_config = config.get_config_cache().get_host_config(hostname) assert ip_lookup.lookup_mgmt_board_ip_address(host_config) is None
def test_basic_get_host_ruleset_values(monkeypatch): ts = Scenario().add_host("abc") ts.add_host("xyz") ts.add_host("host1") ts.add_host("host2") config_cache = ts.apply(monkeypatch) matcher = config_cache.ruleset_matcher assert list( matcher.get_host_ruleset_values(RulesetMatchObject( host_name="abc", service_description=None), ruleset=ruleset, is_binary=False)) == [] assert list( matcher.get_host_ruleset_values(RulesetMatchObject( host_name="xyz", service_description=None), ruleset=ruleset, is_binary=False)) == [] assert list( matcher.get_host_ruleset_values(RulesetMatchObject( host_name="host1", service_description=None), ruleset=ruleset, is_binary=False)) == ["BLA", "BLUB"] assert list( matcher.get_host_ruleset_values(RulesetMatchObject( host_name="host2", service_description=None), ruleset=ruleset, is_binary=False)) == ["BLUB"]
def test_ruleset_matcher_get_service_ruleset_values_labels(monkeypatch, hostname, service_description, expected_result): ts = Scenario() ts.add_host("host1") ts.set_autochecks("host1", [ Service("cpu.load", None, "CPU load", "{}", service_labels=DiscoveredServiceLabels( ServiceLabel(u"os", u"linux"), ServiceLabel(u"abc", u"xä"), ServiceLabel(u"hu", u"ha"), )) ]) ts.add_host("host2") ts.set_autochecks("host2", [ Service("cpu.load", None, "CPU load", "{}", service_labels=DiscoveredServiceLabels()), ]) 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, service_description), ruleset=service_label_ruleset, is_binary=False)) == expected_result
def test_cfg(monkeypatch): test_hosts = [ "ds-test-host1", "ds-test-host2", "ds-test-node1", "ds-test-node2" ] ts = Scenario() 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.set_ruleset("datasource_programs", [ ('cat %s/<HOST>' % cmk.utils.paths.tcp_cache_dir, [], test_hosts, {}), ]) with open("%s/tests/integration/cmk/base/test-files/linux-agent-output" % repo_path()) as f: linux_agent_output = f.read().decode("utf-8") for h in test_hosts: cache_path = Path(cmk.utils.paths.tcp_cache_dir, h) cache_path.parent.mkdir(parents=True, exist_ok=True) # pylint: disable=no-member with cache_path.open("w", encoding="utf-8") as f: f.write(linux_agent_output) return ts.apply(monkeypatch)
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) check_api_utils.set_service("cpu.loads", "CPU load") check_api_utils.set_hostname("testhost1") assert check_api.get_effective_service_level() == 33 check_api_utils.set_hostname("testhost2") assert check_api.get_effective_service_level() == 10 check_api_utils.set_hostname("testhost3") assert check_api.get_effective_service_level() == 0
def test_attribute_defaults(self, mode, 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, ipaddress, mode=mode, ) assert source.description == ( "Management board - SNMP " "(Community: 'public', Bulk walk: no, Port: 161, Backend: Classic)" )
def test_filter_by_management_board_TCP_host_with_SNMP_mgmt_board( monkeypatch, for_discovery, host_result, mgmt_board_result): ts = Scenario() ts.add_host("this_host", tags={ "agent": "cmk-agent", }) config_cache = ts.apply(monkeypatch) h = config_cache.get_host_config("this_host") h.has_management_board = True found_check_plugins = set(c for c in _check_plugins() if c.startswith("tcp_")) monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(host_result) found_check_plugins = set(c for c in _check_plugins() if c.startswith("snmp_")) monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, True, for_discovery=for_discovery) == set(mgmt_board_result)
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
def scenario_fixture(monkeypatch): test_hosts = [ "ds-test-host1", "ds-test-host2", "ds-test-node1", "ds-test-node2" ] ts = Scenario() 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.set_ruleset( "datasource_programs", [ ("cat %s/<HOST>" % cmk.utils.paths.tcp_cache_dir, [], test_hosts, {}), ], ) linux_agent_output = get_standard_linux_agent_output() for h in test_hosts: cache_path = Path(cmk.utils.paths.tcp_cache_dir, h) cache_path.parent.mkdir(parents=True, exist_ok=True) with cache_path.open("w", encoding="utf-8") as f: f.write(linux_agent_output) return ts.apply(monkeypatch)
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
def test_attribute_defaults(self, mode, 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, ipaddress, mode=mode, force_cache_refresh=False, selected_sections=NO_SELECTION, on_scan_error="raise", ) assert source.description == ( "Management board - SNMP " "(Community: 'public', Bulk walk: no, Port: 161, Backend: Classic)")
def test_lookup_mgmt_board_ip_address_dual_host(monkeypatch, hostname, result_address): ts = Scenario() ts.add_host(hostname, tags={ "address_family": "ip-v4v6", }) ts.apply(monkeypatch) host_config = config.get_config_cache().get_host_config(hostname) assert ip_lookup.lookup_mgmt_board_ip_address(host_config) == result_address
def clear_config_caches(monkeypatch): from cmk.base.caching import config_cache as _config_cache, runtime_cache as _runtime_cache _config_cache.reset() _runtime_cache.reset() ts = Scenario() ts.add_host("non-existent-testhost") ts.apply(monkeypatch)
def test_all_configured_offline_hosts(monkeypatch): ts = Scenario(site_id="site1") ts.set_ruleset("only_hosts", [ (["!offline"], config.ALL_HOSTS), ]) ts.add_host("blub1", tags={"criticality": "offline", "site": "site1"}) ts.add_host("blub2", tags={"criticality": "offline", "site": "site2"}) ts.apply(monkeypatch) assert config.all_offline_hosts() == {"blub1"}
def test_is_usewalk_host(monkeypatch): ts = Scenario() ts.add_host("xyz") ts.set_ruleset("usewalk_hosts", [ (["xyz"], config.ALL_HOSTS, {}), ]) config_cache = ts.apply(monkeypatch) assert config_cache.get_host_config("xyz").is_usewalk_host is False
def clear_config_caches(monkeypatch): import cmk_base import cmk_base.caching monkeypatch.setattr(cmk_base, "config_cache", cmk_base.caching.CacheManager()) monkeypatch.setattr(cmk_base, "runtime_cache", cmk_base.caching.CacheManager()) ts = Scenario() ts.add_host("non-existent-testhost") ts.apply(monkeypatch)
def test_is_bulkwalk_host(monkeypatch): ts = Scenario().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
def test_get_check_table(monkeypatch, hostname, expected_result): autochecks = { "ping-host": [Service("smart.temp", "bla", u'Temperature SMART bla', {})], "autocheck-overwrite": [ Service('smart.temp', '/dev/sda', u'Temperature SMART /dev/sda', {"is_autocheck": True}), Service('smart.temp', '/dev/sdb', u'Temperature SMART /dev/sdb', {"is_autocheck": True}), ], "ignore-not-existing-checks": [ Service("bla.blub", "ITEM", u'Blub ITEM', {}), ], "node1": [ Service("smart.temp", "auto-clustered", u"Temperature SMART auto-clustered", {}), Service("smart.temp", "auto-not-clustered", u'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'], [u'Temperature SMART auto-clustered$']), ]) config_cache = ts.apply(monkeypatch) monkeypatch.setattr(config_cache, "get_autochecks_of", lambda h: autochecks.get(h, [])) CheckManager().load(["smart"]) assert check_table.get_check_table(hostname) == expected_result
def test_filter_by_management_board_TCP_host_without_mgmt_board(monkeypatch, for_discovery, result): ts = Scenario() ts.add_host("this_host") ts.apply(monkeypatch) found_check_plugins = [c for c in _check_plugins() if c.startswith("tcp_")] monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(result)
def test_filter_by_management_board_unknown_check_plugins(monkeypatch, for_discovery, result): ts = Scenario() ts.add_host("this_host") ts.apply(monkeypatch) found_check_plugins = [c for c in _check_plugins()] monkeypatch.setattr(config, "check_info", []) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(result)