Esempio n. 1
0
    def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, mode, config_cache, host_config):
        sources = [
            ProgramDataSource(
                configurator=ProgramConfigurator.ds(hostname + "0", ipaddress,
                                                    mode=mode,
                                                   template="",),),
            TCPDataSource(configurator=TCPConfigurator(hostname + "1", ipaddress, mode=mode,),),
            TCPDataSource(
                configurator=TCPConfigurator(hostname + "2", ipaddress, mode=mode),
            ),
        ]

        mhs = make_host_sections(
            config_cache,
            host_config,
            ipaddress,
            mode=mode,
            sources=sources,
            max_cachefile_age=0,
            selected_raw_sections=None,
        )
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, AgentHostSections)

        assert len(section.sections) == len(sources)
        for source in sources:
            # yapf: disable
            assert (
                section.sections[SectionName("section_name_%s" % source.hostname)]
                == [["section_content"]])
Esempio n. 2
0
def test_attribute_defaults(mode, monkeypatch):
    ipaddress = "1.2.3.4"
    hostname = "testhost"
    Scenario().add_host(hostname).apply(monkeypatch)

    configurator = TCPConfigurator(hostname, ipaddress, mode=mode)
    configurator.file_cache.path = Path("/my/path/")
    assert configurator.configure_fetcher() == {
        "file_cache": {
            "disabled": False,
            "max_age": None,
            "path": "/my/path",
            "simulation": False,
            "use_outdated": False,
        },
        "family": socket.AF_INET,
        "address": (ipaddress, 6556),
        "timeout": 5.0,
        "encryption_settings": {
            "use_realtime": "enforce",
            "use_regular": "disable",
        },
        "use_only_cache": False,
    }
    assert configurator.description == "TCP: %s:%s" % (ipaddress, 6556)

    checker = configurator.make_checker()

    assert checker.id == "agent"
    assert configurator.file_cache.maybe is False
    assert checker.exception is None
Esempio n. 3
0
    def test_defaults(self, ipaddress, mode, monkeypatch):
        hostname = "testhost"
        Scenario().add_host(hostname).apply(monkeypatch)
        summarizer = TCPConfigurator(
            hostname,
            ipaddress,
            mode=mode,
        ).make_summarizer()

        assert summarizer.summarize(
            AgentHostSections()) == (0, "Version: unknown, OS: unknown", [])
Esempio n. 4
0
def test_tcpdatasource_only_from(mode, monkeypatch, result, reported, rule):
    ts = Scenario().add_host("hostname")
    ts.set_option("agent_config", {"only_from": [rule]} if rule else {})
    config_cache = ts.apply(monkeypatch)

    configurator = TCPConfigurator("hostname", "ipaddress", mode=mode)
    monkeypatch.setattr(config_cache, "host_extra_conf", lambda host, ruleset: ruleset)

    summarizer = AgentSummarizerDefault(configurator)
    assert summarizer._sub_result_only_from({"onlyfrom": reported}) == result
Esempio n. 5
0
    def test_multiple_sources_from_the_same_host(
        self,
        hostname,
        ipaddress,
        mode,
        config_cache,
        host_config,
    ):
        sources = [
            ProgramConfigurator.ds(
                hostname,
                ipaddress,
                mode=mode,
                template="",
            ).make_checker(),
            TCPConfigurator(
                hostname,
                ipaddress,
                mode=mode,
            ).make_checker(),
        ]

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=sources,
            ),
            max_cachefile_age=0,
            selected_raw_sections=None,
            host_config=host_config,
        )
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, AgentHostSections)

        assert len(section.sections) == 1
        # yapf: disable
        assert (section.sections[SectionName("section_name_%s" % hostname)]
                == len(sources) * [["section_content"]])
Esempio n. 6
0
def test_piggyback_storage(mode, monkeypatch, mocker):
    hostname = "testhost"
    ipaddress = "1.2.3.4"
    raw_data = b"\n".join((
        b"<<<<piggyback header>>>>",
        b"<<<section>>>",
        b"first line",
        b"second line",
        b"<<<<>>>>",
    ))

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

    source = TCPDataSource(configurator=TCPConfigurator(
        hostname,
        ipaddress,
        mode=mode,
    ), )
    monkeypatch.setattr(time, "time", lambda: 0)
    mhs = agent.AgentParser(hostname,
                            logging.getLogger("test")).parse(raw_data)
    monkeypatch.setattr(
        type(source),
        "run",
        lambda self, *args, selected_raw_sections, **kwargs: mhs,
    )

    mocker.patch.object(
        cmk.utils.piggyback,
        "store_piggyback_raw_data",
        autospec=True,
    )

    # End of setup

    _make_host_sections(
        [(hostname, ipaddress, [source])],
        max_cachefile_age=0,
        selected_raw_sections=None,
    )

    args = cmk.utils.piggyback.store_piggyback_raw_data.call_args.args  # type: ignore[attr-defined]

    assert mhs.piggybacked_raw_data
    assert args == (hostname, mhs.piggybacked_raw_data)
Esempio n. 7
0
def test_tcpdatasource_restricted_address_mismatch(mode, monkeypatch,
                                                   restricted_address_mismatch_state, only_from,
                                                   rule, result):
    hostname = "hostname"
    ts = Scenario().add_host(hostname)
    ts.set_option("agent_config", {"only_from": [(rule, [], [hostname], {})]})

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

    ts.apply(monkeypatch)
    configurator = TCPConfigurator(hostname, "ipaddress", mode=mode)
    summarizer = AgentSummarizerDefault(configurator)

    assert summarizer._sub_result_only_from({"onlyfrom": only_from}) == result
Esempio n. 8
0
class TestMakeHostSectionsHosts:
    @pytest.fixture(autouse=False)
    def patch_fs(self, fs):
        # piggyback.store_piggyback_raw_data() writes to disk.
        pass

    @pytest.fixture(autouse=True)
    def patch_io(self, monkeypatch):
        class DummyHostSection(ABCHostSections):
            def _extend_section(self, section_name, section_content):
                pass

        for fetcher in (IPMIFetcher, PiggybackFetcher, ProgramFetcher,
                        SNMPFetcher, TCPFetcher):
            monkeypatch.setattr(fetcher, "__enter__", lambda self: self)
            monkeypatch.setattr(
                fetcher,
                "fetch",
                lambda self, mode, fetcher=fetcher: {}
                if fetcher is SNMPFetcher else b"",
            )

        monkeypatch.setattr(CachedSNMPDetector, "__call__",
                            lambda *args, **kwargs: frozenset())
        monkeypatch.setattr(
            ABCChecker,
            "check",
            lambda self, raw_data: DummyHostSection(
                sections={
                    SectionName("section_name_%s" % self.configurator.hostname):
                    [["section_content"]]
                },
                cache_info={},
                piggybacked_raw_data={},
                persisted_sections="",
            ),
        )

    @pytest.fixture
    def hostname(self):
        return "testhost"

    @pytest.fixture
    def ipaddress(self):
        return "1.2.3.4"

    @pytest.fixture
    def host_config(self, hostname):
        return config.HostConfig.make_host_config(hostname)

    @pytest.fixture
    def config_cache(self, hostname, ipaddress, monkeypatch):
        ts = Scenario().add_host(hostname)
        return ts.apply(monkeypatch)

    def test_no_sources(self, hostname, ipaddress, mode, config_cache,
                        host_config):
        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=[],
            ),
            max_cachefile_age=0,
            selected_raw_sections=None,
            host_config=host_config,
        )
        # The length is not zero because the function always sets,
        # at least, a piggy back section.
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, AgentHostSections)

        # Public attributes from ABCHostSections:
        assert not section.sections
        assert not section.cache_info
        assert not section.piggybacked_raw_data
        assert not section.persisted_sections

    def test_one_snmp_source(self, hostname, ipaddress, mode, config_cache,
                             host_config):
        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=[
                    SNMPConfigurator.snmp(
                        hostname,
                        ipaddress,
                        mode=mode,
                    ).make_checker(),
                ],
            ),
            max_cachefile_age=0,
            selected_raw_sections=None,
            host_config=host_config,
        )
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, SNMPHostSections)

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" %
                                            hostname)] == [["section_content"]]

    @pytest.mark.parametrize(
        "source",
        [
            lambda hostname, ipaddress, *, mode: PiggybackConfigurator(
                hostname,
                ipaddress,
                mode=mode,
            ).make_checker(),
            lambda hostname, ipaddress, *, mode: ProgramConfigurator.ds(
                hostname,
                ipaddress,
                mode=mode,
                template="",
            ).make_checker(),
            lambda hostname, ipaddress, *, mode: TCPConfigurator(
                hostname,
                ipaddress,
                mode=mode,
            ).make_checker(),
        ],
    )
    def test_one_nonsnmp_source(self, hostname, ipaddress, mode, config_cache,
                                host_config, source):
        source = source(hostname, ipaddress, mode=mode)
        assert source.configurator.source_type is SourceType.HOST

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=[source],
            ),
            max_cachefile_age=0,
            selected_raw_sections=None,
            host_config=host_config,
        )
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, source.configurator.source_type)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, AgentHostSections)

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" %
                                            hostname)] == [["section_content"]]

    def test_multiple_sources_from_the_same_host(
        self,
        hostname,
        ipaddress,
        mode,
        config_cache,
        host_config,
    ):
        sources = [
            ProgramConfigurator.ds(
                hostname,
                ipaddress,
                mode=mode,
                template="",
            ).make_checker(),
            TCPConfigurator(
                hostname,
                ipaddress,
                mode=mode,
            ).make_checker(),
        ]

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=sources,
            ),
            max_cachefile_age=0,
            selected_raw_sections=None,
            host_config=host_config,
        )
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, AgentHostSections)

        assert len(section.sections) == 1
        # yapf: disable
        assert (section.sections[SectionName("section_name_%s" % hostname)]
                == len(sources) * [["section_content"]])

    def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, mode, config_cache, host_config):
        sources = [
            ProgramConfigurator.ds(hostname + "0", ipaddress,
                                                    mode=mode,
                                                   template="",).make_checker(),
            TCPConfigurator(hostname + "1", ipaddress, mode=mode,).make_checker(),
            TCPConfigurator(hostname + "2", ipaddress, mode=mode,).make_checker(),
        ]

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=sources,
            ),
            max_cachefile_age=0,
            selected_raw_sections=None,
            host_config=host_config,
        )
        assert len(mhs) == 1

        key = HostKey(hostname, ipaddress, SourceType.HOST)
        assert key in mhs

        section = mhs[key]
        assert isinstance(section, AgentHostSections)

        assert len(section.sections) == len(sources)
        for configurator in (s.configurator for s in sources):
            # yapf: disable
            assert (
                section.sections[SectionName("section_name_%s" % configurator.hostname)]
                == [["section_content"]])