示例#1
0
def test_command_line_and_stdin(monkeypatch, info_func_result, expected):
    Scenario().add_host("testhost").apply(monkeypatch)
    special_agent_id = "bi"
    agent_prefix = "%s/special/agent_%s " % (cmk.utils.paths.agents_dir,
                                             special_agent_id)
    ds = SpecialAgentDataSource("testhost", "127.0.0.1", special_agent_id, {})
    monkeypatch.setattr(config, "special_agent_info", {
        special_agent_id: lambda a, b, c: info_func_result
    })

    assert ds.source_cmdline == agent_prefix + expected[0]
    assert ds.source_stdin == expected[1]
def test_snmp_ipaddress_from_mgmt_board_unresolvable(monkeypatch):
    def failed_ip_lookup(h):
        raise MKIPAddressLookupError("Failed to ...")

    Scenario().add_host("hostname").apply(monkeypatch)
    monkeypatch.setattr(ip_lookup, "lookup_ip_address", failed_ip_lookup)
    monkeypatch.setattr(config, "host_attributes", {
        "hostname": {
            "management_address": "lolo"
        },
    })
    assert management_board_ipaddress("hostname") is None
示例#3
0
def test_compile_delayed_host_check(monkeypatch, serial):
    hostname = "localhost"
    ts = Scenario().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:
                        (["uptime"], [], []))

    source_file = core_nagios.HostCheckStore.host_check_source_file_path(
        serial, hostname)
    compiled_file = core_nagios.HostCheckStore.host_check_file_path(
        serial, 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, serial, hostname, verify_site_python=False)
    assert host_check is not None
    core_nagios.HostCheckStore().write(serial, 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.Popen(["python3", str(compiled_file)],
                            shell=False,
                            close_fds=True).wait() == 1

    assert compiled_file.resolve() != source_file
    with compiled_file.open("rb") as f:
        assert f.read().startswith(importlib.util.MAGIC_NUMBER)
示例#4
0
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
示例#5
0
def test_update_dns_cache(monkeypatch, _cache_file):
    def _getaddrinfo(host,
                     port,
                     family=None,
                     socktype=None,
                     proto=None,
                     flags=None):
        # Needs to return [(family, type, proto, canonname, sockaddr)] but only
        # caring about the address
        return {
            ("blub", socket.AF_INET):
            [(family, None, None, None, ("127.0.0.13", 1337))],
            ("bla", socket.AF_INET):
            [(family, None, None, None, ("127.0.0.37", 1337))],
            ("dual", socket.AF_INET):
            [(family, None, None, None, ("127.0.0.42", 1337))],
        }[(host, family)]

    monkeypatch.setattr(socket, "getaddrinfo", _getaddrinfo)

    ts = Scenario()
    ts.add_host("blub", tags={"criticality": "offline"})
    ts.add_host("bla")
    ts.add_host("dual", tags={"address_family": "ip-v4v6"})
    ts.apply(monkeypatch)

    config_cache = config.get_config_cache()
    assert ip_lookup.update_dns_cache(
        host_configs=(config_cache.get_host_config(hn)
                      for hn in config_cache.all_active_hosts()),
        configured_ipv4_addresses={},
        configured_ipv6_addresses={},
        simulation_mode=False,
        override_dns=None,
    ) == (3, ["dual"])

    # Check persisted data
    cache = ip_lookup._load_ip_lookup_cache(lock=False)
    assert cache[("blub", socket.AF_INET)] == "127.0.0.13"
    assert ("dual", socket.AF_INET6) not in cache
示例#6
0
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
示例#7
0
def test_filter_by_management_board_dual_host_with_SNMP_mgmt_board(
        monkeypatch, for_discovery, host_result, mgmt_board_result):
    ts = Scenario()
    ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "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(_check_plugins())
    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_attribute_defaults(self, mode, monkeypatch):
        hostname = "testhost"
        ipaddress = "1.2.3.4"

        Scenario().add_host(hostname).apply(monkeypatch)

        source = SNMPSource.snmp(hostname,
                                 ipaddress,
                                 mode=mode,
                                 selected_sections=NO_SELECTION)
        assert source.description == (
            "SNMP (Community: 'public', Bulk walk: no, Port: 161, Backend: Classic)"
        )
def test_snmp_ipaddress_from_mgmt_board_unresolvable(hostname, monkeypatch):
    def fake_lookup_ip_address(host_config, family=None, for_mgmt_board=True):
        raise MKIPAddressLookupError("Failed to ...")

    Scenario().add_host(hostname).apply(monkeypatch)
    monkeypatch.setattr(ip_lookup, "lookup_ip_address", fake_lookup_ip_address)
    monkeypatch.setattr(config, "host_attributes", {
        "hostname": {
            "management_address": "lolo"
        },
    })
    host_config = config.get_config_cache().get_host_config(hostname)
    assert ip_lookup.lookup_mgmt_board_ip_address(host_config) is None
    def test_attribute_defaults(self, monkeypatch, ipaddress):
        template = ""
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        source = DSProgramDataSource(hostname, ipaddress, template)

        assert source.id() == "agent"
        assert source.name() == ""
        # ProgramDataSource
        assert source._cpu_tracking_id() == "ds"
        assert source.source_cmdline == ""
        assert source.source_stdin is None
        assert source.describe() == "Program: "
示例#11
0
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
示例#12
0
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
示例#13
0
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)
示例#14
0
def test_config_cache_tag_to_group_map(monkeypatch):
    ts = Scenario()
    ts.set_option(
        "tag_config", {
            "aux_tags": [],
            "tag_groups": [{
                'id': 'dingeling',
                'title': u'Dung',
                'tags': [{
                    'aux_tags': [],
                    'id': 'dong',
                    'title': u'ABC'
                },],
            }],
        })
    config_cache = ts.apply(monkeypatch)
    assert config_cache.get_tag_to_group_map() == {
        'all-agents': 'agent',
        'auto-piggyback': 'piggyback',
        'cmk-agent': 'agent',
        'dong': 'dingeling',
        'ip-v4': 'ip-v4',
        'ip-v4-only': 'address_family',
        'ip-v4v6': 'address_family',
        'ip-v6': 'ip-v6',
        'ip-v6-only': 'address_family',
        'no-agent': 'agent',
        'no-ip': 'address_family',
        'no-piggyback': 'piggyback',
        'no-snmp': 'snmp_ds',
        'piggyback': 'piggyback',
        'ping': 'ping',
        'snmp': 'snmp',
        'snmp-v1': 'snmp_ds',
        'snmp-v2': 'snmp_ds',
        'special-agents': 'agent',
        'tcp': 'tcp',
    }
示例#15
0
def test_config_cache_tag_list_of_host(monkeypatch):
    ts = Scenario()
    ts.add_host("test-host", tags={"agent": "no-agent"})
    ts.add_host("xyz")
    config_cache = ts.apply(monkeypatch)

    print config_cache._hosttags["test-host"]
    print config_cache._hosttags["xyz"]
    assert config_cache.tag_list_of_host("xyz") == {
        '/wato/', 'lan', 'ip-v4', 'cmk-agent', 'no-snmp', 'tcp', 'auto-piggyback', 'ip-v4-only',
        'site:unit', 'prod'
    }
示例#16
0
def test_ruleset_matcher_get_host_ruleset_values_labels(monkeypatch, hostname, expected_result):
    ts = Scenario()
    ts.add_host("host1", labels={"os": "linux", "abc": "xä", "hu": "ha"})
    ts.add_host("host2")
    config_cache = ts.apply(monkeypatch)
    matcher = config_cache.ruleset_matcher

    assert list(
        matcher.get_host_ruleset_values(RulesetMatchObject(host_name=hostname,
                                                           service_description=None),
                                        ruleset=host_label_ruleset,
                                        is_binary=False)) == expected_result
def test_attribute_defaults(monkeypatch, ipaddress):
    hostname = "testhost"
    Scenario().add_host(hostname).apply(monkeypatch)
    # NOTE: pylint is quite buggy when it comes to class hierarchies and abstract methods!
    source = IPMIManagementBoardDataSource(hostname, ipaddress)  # pylint: disable=abstract-class-instantiated

    assert source.id() == "mgmt_ipmi"
    assert source.title() == "Management board - IPMI"
    assert source._cpu_tracking_id() == source.id()
    assert source._gather_check_plugin_names() == {"mgmt_ipmi_sensors"}
    assert source._summary_result("anything will do") == (0,
                                                          "Version: unknown",
                                                          [])
    assert source._get_ipmi_version() == "unknown"
示例#18
0
def test_attribute_defaults(monkeypatch, ipaddress, mode):
    hostname = "testhost"
    Scenario().add_host(hostname).apply(monkeypatch)

    source = PiggybackSource(hostname,
                             ipaddress,
                             mode=mode,
                             preselected_sections=AUTO_DETECT)
    assert source.hostname == hostname
    assert source.ipaddress == ipaddress
    assert source.mode is mode
    assert source.description.startswith("Process piggyback data from")
    assert source.summarize(result.OK(AgentHostSections())) == (0, "", [])
    assert source.id == "piggyback"
示例#19
0
def test_labels_of_service_discovered_labels(monkeypatch, tmp_path):
    CheckManager().load(["cpu"])
    ts = Scenario().add_host("test-host")

    monkeypatch.setattr(cmk.utils.paths, "autochecks_dir", str(tmp_path))
    autochecks_file = Path(cmk.utils.paths.autochecks_dir).joinpath("test-host.mk")
    with autochecks_file.open("w", encoding="utf-8") as f:  # pylint: disable=no-member
        f.write(u"""[
    {'check_plugin_name': 'cpu.loads', 'item': None, 'parameters': cpuload_default_levels, 'service_labels': {u'äzzzz': u'eeeeez'}},
]""")

    config_cache = ts.apply(monkeypatch)

    service = config_cache.get_autochecks_of("test-host")[0]
    assert service.description == u"CPU load"

    assert config_cache.labels_of_service("xyz", u"CPU load") == {}
    assert config_cache.label_sources_of_service("xyz", u"CPU load") == {}

    assert config_cache.labels_of_service("test-host", service.description) == {u"äzzzz": u"eeeeez"}
    assert config_cache.label_sources_of_service("test-host", service.description) == {
        u"äzzzz": u"discovered"
    }
示例#20
0
def test_ipmi_ipaddress_from_mgmt_board(monkeypatch):
    hostname = "testhost"
    ipaddress = "127.0.0.1"
    Scenario().add_host(hostname).apply(monkeypatch)
    monkeypatch.setattr(ip_lookup, "lookup_ip_address", lambda h: ipaddress)
    monkeypatch.setattr(config, "host_attributes", {
        hostname: {
            "management_address": ipaddress
        },
    })
    source = IPMIManagementBoardDataSource(hostname, None)

    assert source._host_config.management_address == ipaddress
    assert source._ipaddress == ipaddress
示例#21
0
def test_host_config_extra_host_attributes(monkeypatch, hostname, result):
    ts = Scenario().add_host(hostname)
    ts.set_option(
        "extra_host_conf", {
            "dingdong": [
                ([
                    "value1",
                ], [], ["testhost2"], {}),
                ([
                    "value2",
                ], [], ["testhost2"], {}),
            ],
            "_custom": [
                ([
                    "value1",
                ], [], ["testhost2"], {}),
                ([
                    "value2",
                ], [], ["testhost2"], {}),
            ],
        })
    config_cache = ts.apply(monkeypatch)
    assert config_cache.get_host_config(hostname).extra_host_attributes == result
示例#22
0
def test_mgmt_board_data_source_management_board_ipaddress(
        monkeypatch, address, result):
    Scenario().add_host("hostname").apply(monkeypatch)
    # TODO: Extremely obscure code below: The class seems to be abstract??!!
    source = SNMPManagementBoardDataSource("hostname", "ipaddress")  # pylint: disable=abstract-class-instantiated

    monkeypatch.setattr(ip_lookup, "lookup_ip_address", lambda h: "127.0.1.1")
    monkeypatch.setattr(config, "host_attributes", {
        "hostname": {
            "management_address": address
        },
    })

    assert source._management_board_ipaddress("hostname") == result
示例#23
0
    def test_defaults(self, ipaddress, mode, monkeypatch):
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        source = TCPSource(
            hostname,
            ipaddress,
            mode=mode,
        )

        assert source.summarize(Result.OK(AgentHostSections())) == (
            0,
            "Version: unknown, OS: unknown",
            [],
        )
示例#24
0
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)
示例#25
0
def test_host_config_explicit_check_command(monkeypatch, hostname, core_name, result):
    ts = Scenario().add_host(hostname)
    ts.set_option("monitoring_core", core_name)
    ts.set_option(
        "host_check_commands",
        [
            ("command1", [], ["testhost2"], {}),
            ("command2", [], ["testhost2"], {}),
            ("smart", [], ["testhost3"], {}),
        ],
    )
    config_cache = ts.apply(monkeypatch)
    assert config_cache.get_host_config(hostname).explicit_check_command == result
示例#26
0
def test_attribute_defaults(monkeypatch, ipaddress):
    hostname = "testhost"
    Scenario().add_host(hostname).apply(monkeypatch)
    source = IPMIManagementBoardDataSource(hostname, ipaddress)

    assert source._for_mgmt_board is True
    assert source._hostname == hostname
    # Address comes from management board.
    assert source._ipaddress is None
    assert source.id() == "mgmt_ipmi"
    assert source.title() == "Management board - IPMI"
    assert source._cpu_tracking_id() == source.id()
    assert source._gather_check_plugin_names() == {"mgmt_ipmi_sensors"}
    assert source._summary_result(True) == (0, "Version: unknown", [])
    assert source._get_ipmi_version() == "unknown"
示例#27
0
def test_data_sources_of_hosts(monkeypatch, hostname, settings):
    ts = Scenario().add_host(hostname, tags=settings["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',
        ], {}),]})
    ts.apply(monkeypatch)

    sources = cmk.base.data_sources.DataSources(hostname, "127.0.0.1")
    source_names = [s.__class__.__name__ for s in sources.get_data_sources()]
    assert settings["sources"] == source_names, "Wrong sources for %s" % hostname
示例#28
0
def test_update_dns_cache(monkeypatch, _cache_file):
    def _getaddrinfo(host, port, family=None, socktype=None, proto=None, flags=None):
        # Needs to return [(family, type, proto, canonname, sockaddr)] but only
        # caring about the address
        return {
            ("blub", socket.AF_INET): [(family, None, None, None, ("127.0.0.13", 1337))],
            ("bla", socket.AF_INET): [(family, None, None, None, ("127.0.0.37", 1337))],
            ("dual", socket.AF_INET): [(family, None, None, None, ("127.0.0.42", 1337))],
        }[(host, family)]

    monkeypatch.setattr(socket, "getaddrinfo", _getaddrinfo)

    ts = Scenario()
    ts.add_host("blub", tags={"criticality": "offline"})
    ts.add_host("bla")
    ts.add_host("dual", tags={"address_family": "ip-v4v6"})
    ts.apply(monkeypatch)

    assert ip_lookup.update_dns_cache() == (3, ["dual"])

    # Check persisted data
    cache = ip_lookup._load_ip_lookup_cache(lock=False)
    assert cache[("blub", 4)] == "127.0.0.13"
    assert ("dual", 6) not in cache
示例#29
0
    def test_template_translation(self, ipaddress, mode, monkeypatch):
        template = "<NOTHING>x<IP>x<HOST>x<host>x<ip>x"
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        configurator = DSProgramConfigurator(
            hostname,
            ipaddress,
            mode=mode,
            template=template,
        )

        assert configurator.cmdline == "<NOTHING>x%sx%sx<host>x<ip>x" % (
            ipaddress if ipaddress is not None else "",
            hostname,
        )
示例#30
0
def test_snmp_ipaddress_from_mgmt_board(monkeypatch):
    hostname = "testhost"
    ipaddress = "1.2.3.4"
    Scenario().add_host(hostname).apply(monkeypatch)
    monkeypatch.setattr(ip_lookup, "lookup_ip_address", lambda h: ipaddress)
    monkeypatch.setattr(config, "host_attributes", {
        hostname: {
            "management_address": ipaddress
        },
    })

    source = SNMPManagementBoardDataSource(hostname, "unused")

    assert source._host_config.management_address == ipaddress
    assert source._ipaddress == ipaddress