예제 #1
0
 def test_raw_data_exception(self, duration):
     raw_data: result.Result[AgentRawData,
                             Exception] = result.Error(Exception("zomg!"))
     message = FetcherMessage.from_raw_data(raw_data, duration,
                                            FetcherType.TCP)
     assert isinstance(message.raw_data.error, Exception)
     assert str(message.raw_data.error) == "zomg!"
예제 #2
0
 def test_raw_data_tcp_standard(self, agent_raw_data, duration,
                                fetcher_type):
     raw_data: result.Result[AgentRawData,
                             Exception] = result.OK(agent_raw_data)
     message = FetcherMessage.from_raw_data(raw_data, duration,
                                            fetcher_type)
     assert message.raw_data == raw_data
예제 #3
0
def fetch_all(
    nodes: Iterable[Tuple[HostName, Optional[HostAddress], Sequence[Source]]],
    *,
    max_cachefile_age: int,
    host_config: HostConfig,
) -> Iterator[FetcherMessage]:
    console.verbose("%s+%s %s\n", tty.yellow, tty.normal,
                    "Fetching data".upper())
    # TODO(ml): It is not clear to me in which case it is possible for the following to hold true
    #           for any source in nodes:
    #             - hostname != source.hostname
    #             - ipaddress != source.ipaddress
    #           If this is impossible, then we do not need the Tuple[HostName, HostAddress, ...].
    for _hostname, _ipaddress, sources in nodes:
        for source in sources:
            console.vverbose("  Source: %s/%s\n" %
                             (source.source_type, source.fetcher_type))

            source.file_cache_max_age = max_cachefile_age

            with CPUTracker() as tracker:
                raw_data = source.fetch()
            yield FetcherMessage.from_raw_data(
                raw_data,
                tracker.duration,
                source.fetcher_type,
            )
예제 #4
0
 def test_from_raw_data_exception(self, duration):
     error: result.Result[AgentRawData, Exception] = result.Error(ValueError("zomg!"))
     message = FetcherMessage.from_raw_data(error, duration, FetcherType.TCP)
     assert message.header.fetcher_type is FetcherType.TCP
     assert message.header.payload_type is PayloadType.ERROR
     # Comparison of exception is "interesting" in Python so we check the type and args.
     assert type(message.raw_data.error) is type(error.error)
     assert message.raw_data.error.args == error.error.args
예제 #5
0
 def test_from_raw_data_snmp(self, snmp_raw_data, duration):
     raw_data: result.Result[SNMPRawData,
                             Exception] = result.OK(snmp_raw_data)
     message = FetcherMessage.from_raw_data(raw_data, duration,
                                            FetcherType.SNMP)
     assert message.header.fetcher_type is FetcherType.SNMP
     assert message.header.payload_type is PayloadType.SNMP
     assert message.raw_data == raw_data
예제 #6
0
 def test_from_raw_data_standard(self, agent_raw_data, duration,
                                 fetcher_type):
     raw_data: result.Result[AgentRawData,
                             Exception] = result.OK(agent_raw_data)
     message = FetcherMessage.from_raw_data(raw_data, duration,
                                            fetcher_type)
     assert message.header.fetcher_type is fetcher_type
     assert message.header.payload_type is PayloadType.AGENT
     assert message.raw_data == raw_data
예제 #7
0
    def test_multiple_sources_from_the_same_host(
        self,
        hostname,
        ipaddress,
        mode,
        config_cache,
        host_config,
    ):
        sources = [
            ProgramSource.ds(
                hostname,
                ipaddress,
                mode=mode,
                preselected_sections=AUTO_DETECT,
                template="",
            ),
            TCPSource(
                hostname,
                ipaddress,
                mode=mode,
                preselected_sections=AUTO_DETECT,
            ),
        ]

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            make_nodes(
                config_cache,
                host_config,
                ipaddress,
                mode=mode,
                sources=sources,
            ),
            max_cachefile_age=0,
            host_config=host_config,
            fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK(source.default_raw_data),
                    Snapshot.null(),
                    source.fetcher_type,
                ) for source in sources
            ],
        )
        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"]])
예제 #8
0
 def fetcher_message(self, fetcher_payload, fetcher_stats):
     return FetcherMessage(
         FetcherHeader(
             FetcherType.TCP,
             PayloadType.AGENT,
             status=42,
             payload_length=len(fetcher_payload),
             stats_length=len(fetcher_stats),
         ),
         fetcher_payload,
         fetcher_stats,
     )
    def test_multiple_sources_from_different_hosts(self, hostname, ipaddress, mode, config_cache, host_config):
        sources = [
            ProgramSource.ds(hostname + "0", ipaddress, mode=mode, template=""),
            TCPSource(hostname + "1", ipaddress, mode=mode),
            TCPSource(hostname + "2", ipaddress, mode=mode),
        ]

        nodes = make_nodes(
            config_cache,
            host_config,
            ipaddress,
            mode=mode,
            sources=sources,
        )

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            nodes,
            max_cachefile_age=0,
            host_config=host_config,
            fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK(source.default_raw_data),
                    Snapshot.null(),
                    source.fetcher_type,
                )
                for _h, _i, sources in nodes for source in sources
            ],
            selected_sections=NO_SELECTION,
        )
        assert len(mhs) == 1

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

        section = mhs[key]

        assert len(section.sections) == len(sources)
        for source in sources:
            # yapf: disable
            assert (
                section.sections[SectionName("section_name_%s" % source.hostname)]
                == [["section_content"]])
예제 #10
0
    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=[
                    SNMPSource.snmp(
                        hostname,
                        ipaddress,
                        mode=mode,
                        selected_sections=NO_SELECTION,
                        on_scan_error="raise",
                    ),
                ],
            ),
            max_cachefile_age=0,
            host_config=host_config,
            fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK({}),
                    Snapshot.null(),
                    FetcherType.SNMP,
                ),
            ],
            selected_sections=NO_SELECTION,
        )
        assert len(mhs) == 1

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

        section = mhs[key]

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" %
                                            hostname)] == [["section_content"]]
예제 #11
0
    def test_no_sources(self, cluster, nodes, config_cache, host_config, mode):
        made_nodes = make_nodes(
            config_cache,
            host_config,
            None,
            mode=mode,
            sources=(),
        )

        mhs = MultiHostSections()
        update_host_sections(
            mhs,
            made_nodes,
            max_cachefile_age=0,
            host_config=host_config,
            fetcher_messages=[
                # We do not pass sources explicitly but still append Piggyback.
                FetcherMessage.from_raw_data(
                    result.OK(AgentRawData(b"")),
                    Snapshot.null(),
                    FetcherType.PIGGYBACK,
                ) for _n in made_nodes
            ],
            selected_sections=NO_SELECTION,
        )
        assert len(mhs) == len(nodes)

        key_clu = HostKey(cluster, None, SourceType.HOST)
        assert key_clu not in mhs

        for hostname, addr in nodes.items():
            key = HostKey(hostname, addr, SourceType.HOST)
            assert key in mhs

            section = mhs[key]
            # yapf: disable
            assert (section.sections[SectionName("section_name_%s" % hostname)]
                    == [["section_content_%s" % hostname]])
            assert not section.cache_info
            assert not section.piggybacked_raw_data
예제 #12
0
    def test_one_nonsnmp_source(self, hostname, ipaddress, mode, config_cache,
                                host_config, source):
        source = source(hostname, ipaddress, mode=mode)
        assert source.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,
            host_config=host_config,
            fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK(source.default_raw_data),
                    Snapshot.null(),
                    source.fetcher_type,
                ),
            ],
            selected_sections=NO_SELECTION,
        )
        assert len(mhs) == 1

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

        section = mhs[key]

        assert len(section.sections) == 1
        assert section.sections[SectionName("section_name_%s" %
                                            hostname)] == [["section_content"]]
예제 #13
0
 def test_from_bytes_success(self, message):
     assert FetcherMessage.from_bytes(bytes(message) + 42 * b"*") == message
예제 #14
0
def test_get_host_sections_cluster(mode, monkeypatch, mocker):
    hostname = "testhost"
    hosts = {
        "host0": "10.0.0.0",
        "host1": "10.0.0.1",
        "host2": "10.0.0.2",
    }
    address = "1.2.3.4"
    tags = {"agent": "no-agent"}
    section_name = SectionName("test_section")
    config_cache = make_scenario(hostname, tags).apply(monkeypatch)
    host_config = config.HostConfig.make_host_config(hostname)

    def lookup_ip_address(host_config, family=None, for_mgmt_board=False):
        return hosts[host_config.hostname]

    def check(_, *args, **kwargs):
        return result.OK(AgentHostSections(sections={section_name: [[str(section_name)]]}))

    monkeypatch.setattr(
        ip_lookup,
        "lookup_ip_address",
        lookup_ip_address,
    )
    monkeypatch.setattr(
        Source,
        "parse",
        check,
    )
    mocker.patch.object(
        cmk.utils.piggyback,
        "remove_source_status_file",
        autospec=True,
    )
    mocker.patch.object(
        cmk.utils.piggyback,
        "_store_status_file_of",
        autospec=True,
    )

    # Create a cluster
    host_config.nodes = list(hosts.keys())

    nodes = make_nodes(
        config_cache,
        host_config,
        address,
        mode=mode,
        sources=make_sources(host_config, address, mode=mode)
    )

    mhs = MultiHostSections()
    update_host_sections(
        mhs,
        nodes,
        max_cachefile_age=host_config.max_cachefile_age,
        host_config=host_config,
        fetcher_messages=[
                FetcherMessage.from_raw_data(
                    result.OK(source.default_raw_data),
                    Snapshot.null(),
                    source.fetcher_type,
                )
                for _h, _i, sources in nodes for source in sources
            ],
        selected_sections=NO_SELECTION,
    )
    assert len(mhs) == len(hosts) == 3
    cmk.utils.piggyback._store_status_file_of.assert_not_called()  # type: ignore[attr-defined]
    assert cmk.utils.piggyback.remove_source_status_file.call_count == 3  # type: ignore[attr-defined]

    for host, addr in hosts.items():
        remove_source_status_file = cmk.utils.piggyback.remove_source_status_file
        remove_source_status_file.assert_any_call(host)  # type: ignore[attr-defined]
        key = HostKey(host, addr, SourceType.HOST)
        assert key in mhs
        section = mhs[key]
        assert len(section.sections) == 1
        assert next(iter(section.sections)) == section_name
        assert not section.cache_info
        assert not section.piggybacked_raw_data
예제 #15
0
 def test_raw_data_snmp(self, snmp_raw_data, duration):
     raw_data: result.Result[SNMPRawData, Exception] = result.OK(snmp_raw_data)
     message = FetcherMessage.from_raw_data(raw_data, duration, FetcherType.SNMP)
     assert message.raw_data == raw_data
예제 #16
0
 def test_from_bytes_failure(self):
     with pytest.raises(ValueError):
         FetcherMessage.from_bytes(b"random bytes")
예제 #17
0
 def message(self, header, payload, stats):
     return FetcherMessage(header, payload, stats)