Пример #1
0
def test_get_section_cluster_kwargs(patch_register, required_sections,
                                    expected_result):

    node1_section_content = {
        SectionName("one"): NODE_1,
        SectionName("two"): NODE_1,
        SectionName("three"): NODE_1
    }

    node2_section_content = {
        SectionName("two"): NODE_2,
        SectionName("three"): NODE_2,
    }

    parsed_sections_broker = ParsedSectionsBroker()
    parsed_sections_broker.setdefault(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        AgentHostSections(sections=node1_section_content),
    )
    parsed_sections_broker.setdefault(
        HostKey("node2", "127.0.0.1", SourceType.HOST),
        AgentHostSections(sections=node2_section_content),
    )

    kwargs = parsed_sections_broker.get_section_cluster_kwargs(
        [
            HostKey("node1", "127.0.0.1", SourceType.HOST),
            HostKey("node2", "127.0.0.1", SourceType.HOST),
        ],
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs,\
           "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
Пример #2
0
def test_get_section_kwargs(
        required_sections: List[str],
        expected_result: Dict[str, Dict[str, str]]) -> None:

    node_sections = AgentHostSections(
        sections={
            SectionName("one"): NODE_1,
            SectionName("two"): NODE_1,
            SectionName("three"): NODE_1,
        })

    host_key = HostKey(HostName("node1"), HostAddress("127.0.0.1"),
                       SourceType.HOST)

    parsed_sections_broker = ParsedSectionsBroker({
        host_key: (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ]),
            SectionsParser(host_sections=node_sections),
        ),
    })

    kwargs = get_section_kwargs(
        parsed_sections_broker,
        host_key,
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs
Пример #3
0
def test__find_candidates():
    broker = ParsedSectionsBroker({
        # we just care about the keys here, content set to arbitrary values that can be parsed.
        # section names have been are chosen arbitrarily.
        HostKey("test_node", "1.2.3.4", SourceType.HOST): AgentHostSections({
            SectionName("kernel"): [],  # host only
            SectionName("uptime"): [['123']],  # host & mgmt
        }),
        HostKey("test_node", "1.2.3.4", SourceType.MANAGEMENT): SNMPHostSections({
            # host & mgmt:
            SectionName("uptime"): [['123']],  # type: ignore[dict-item]
            # mgmt only:
            SectionName("liebert_fans"): [[['Fan', '67', 'umin']]],  # type: ignore[dict-item]
            # is already mgmt_ prefixed:
            SectionName("mgmt_snmp_info"): [[['a', 'b', 'c', 'd']]],  # type: ignore[dict-item]
        }),
    })

    preliminary_candidates = list(agent_based_register.iter_all_check_plugins())
    parsed_sections_of_interest = {
        parsed_section_name for plugin in preliminary_candidates
        for parsed_section_name in plugin.sections
    }

    assert discovery._discovered_services._find_host_candidates(
        broker,
        preliminary_candidates,
        parsed_sections_of_interest,
    ) == {
        CheckPluginName('docker_container_status_uptime'),
        CheckPluginName("kernel"),
        CheckPluginName('kernel_performance'),
        CheckPluginName('kernel_util'),
        CheckPluginName("uptime"),
    }

    assert discovery._discovered_services._find_mgmt_candidates(
        broker,
        preliminary_candidates,
        parsed_sections_of_interest,
    ) == {
        CheckPluginName('mgmt_docker_container_status_uptime'),
        CheckPluginName("mgmt_liebert_fans"),
        CheckPluginName("mgmt_uptime"),
    }

    assert discovery._discovered_services._find_candidates(
        broker,
        run_only_plugin_names=None,
    ) == {
        CheckPluginName('docker_container_status_uptime'),
        CheckPluginName("kernel"),
        CheckPluginName('kernel_performance'),
        CheckPluginName('kernel_util'),
        CheckPluginName('mgmt_docker_container_status_uptime'),
        CheckPluginName("mgmt_liebert_fans"),
        CheckPluginName("mgmt_uptime"),
        CheckPluginName("uptime"),
    }
def _get_host_section_for_parse_sections_test():
    node_section_content = {
        SectionName("one"): NODE_1,
        SectionName("four"): NODE_1,
    }

    host_key = HostKey("node1", "127.0.0.1", SourceType.HOST)

    broker = ParsedSectionsBroker({host_key: AgentHostSections(sections=node_section_content)})

    return host_key, broker
Пример #5
0
def test_get_section_content(hostname, host_entries, cluster_node_keys,
                             expected_result):

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey(nodename, "127.0.0.1", SourceType.HOST): (
            ParsedSectionsResolver(
                # NOTE: this tests the legacy functionality. The "proper" ParsedSectionsBroker
                # methods are bypassed, so these will not matter at all:
                section_plugins=[], ),
            SectionsParser(host_sections=AgentHostSections(
                sections={
                    SectionName("section_plugin_name"): node_section_content
                })),
        )
        for nodename, node_section_content in host_entries
    })

    mhs = _MultiHostSections(parsed_sections_broker)

    section_content = mhs.get_section_content(
        HostKey(hostname, "127.0.0.1", SourceType.HOST),
        HOST_ONLY,
        "section_plugin_name",
        False,
        cluster_node_keys=cluster_node_keys,
        check_legacy_info=
        {},  # only for parse_function lookup, not needed in this test
    )
    assert expected_result == section_content

    section_content = mhs.get_section_content(
        HostKey(hostname, "127.0.0.1", SourceType.HOST),
        HOST_PRECEDENCE,
        "section_plugin_name",
        False,
        cluster_node_keys=cluster_node_keys,
        check_legacy_info=
        {},  # only for parse_function lookup, not needed in this test
    )
    assert expected_result == section_content

    section_content = mhs.get_section_content(
        HostKey(hostname, "127.0.0.1", SourceType.MANAGEMENT),
        MGMT_ONLY,
        "section_plugin_name",
        False,
        cluster_node_keys=None if cluster_node_keys is None else [
            HostKey(hn, ip, SourceType.MANAGEMENT)
            for (hn, ip, _st) in cluster_node_keys
        ],
        check_legacy_info=
        {},  # only for parse_function lookup, not needed in this test
    )
    assert section_content is None
Пример #6
0
def test_get_section_cluster_kwargs(required_sections, expected_result):

    node1_sections = AgentHostSections(
        sections={
            SectionName("one"): NODE_1,
            SectionName("two"): NODE_1,
            SectionName("three"): NODE_1
        })

    node2_sections = AgentHostSections(sections={
        SectionName("two"): NODE_2,
        SectionName("three"): NODE_2,
    })

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey("node1", "127.0.0.1", SourceType.HOST): (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ], ),
            SectionsParser(host_sections=node1_sections),
        ),
        HostKey("node2", "127.0.0.1", SourceType.HOST): (
            ParsedSectionsResolver(section_plugins=[
                SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR
            ], ),
            SectionsParser(host_sections=node2_sections),
        ),
    })

    kwargs = get_section_cluster_kwargs(
        parsed_sections_broker,
        [
            HostKey("node1", "127.0.0.1", SourceType.HOST),
            HostKey("node2", "127.0.0.1", SourceType.HOST),
        ],
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs
Пример #7
0
def test_get_section_content(hostname, host_entries, cluster_node_keys,
                             expected_result):

    parsed_sections_broker = ParsedSectionsBroker()
    for nodename, node_section_content in host_entries:
        parsed_sections_broker.setdefault(
            HostKey(nodename, "127.0.0.1", SourceType.HOST),
            AgentHostSections(
                sections={
                    SectionName("section_plugin_name"): node_section_content
                }),
        )

    mhs = MultiHostSections(parsed_sections_broker)

    section_content = mhs.get_section_content(
        HostKey(hostname, "127.0.0.1", SourceType.HOST),
        check_api_utils.HOST_ONLY,
        "section_plugin_name",
        False,
        cluster_node_keys=cluster_node_keys,
        check_legacy_info=
        {},  # only for parse_function lookup, not needed in this test
    )
    assert expected_result == section_content

    section_content = mhs.get_section_content(
        HostKey(hostname, "127.0.0.1", SourceType.HOST),
        check_api_utils.HOST_PRECEDENCE,
        "section_plugin_name",
        False,
        cluster_node_keys=cluster_node_keys,
        check_legacy_info=
        {},  # only for parse_function lookup, not needed in this test
    )
    assert expected_result == section_content

    section_content = mhs.get_section_content(
        HostKey(hostname, "127.0.0.1", SourceType.MANAGEMENT),
        check_api_utils.MGMT_ONLY,
        "section_plugin_name",
        False,
        cluster_node_keys=None if cluster_node_keys is None else [
            HostKey(hn, ip, SourceType.MANAGEMENT)
            for (hn, ip, _st) in cluster_node_keys
        ],
        check_legacy_info=
        {},  # only for parse_function lookup, not needed in this test
    )
    assert section_content is None
def test_get_parsed_section(patch_register, node_section_content, expected_result):

    parsed_sections_broker = ParsedSectionsBroker({
        HostKey("node1", "127.0.0.1", SourceType.HOST):
            AgentHostSections(sections=node_section_content)
    })

    content = parsed_sections_broker.get_parsed_section(
        HostKey("node1", "127.0.0.1", SourceType.HOST),
        ParsedSectionName("parsed"),
    )

    assert expected_result == content,\
           "Section content: Expected '%s' but got '%s'" % (expected_result, content)
def test_attribute_defaults(mode, monkeypatch):
    hostname = "testhost"
    Scenario().add_host(hostname).apply(monkeypatch)

    host_config = config.get_config_cache().get_host_config(hostname)
    ipaddress = ip_lookup.lookup_mgmt_board_ip_address(host_config)

    source = IPMISource(hostname, ipaddress, mode=mode)
    assert source.hostname == hostname
    assert source.ipaddress == ipaddress
    assert source.mode is mode
    assert source.description == "Management board - IPMI"
    assert source.source_type is SourceType.MANAGEMENT
    assert source.summarize(result.OK(AgentHostSections())) == (0, "Version: unknown")
    assert source.id == "mgmt_ipmi"
def test_get_section_kwargs(patch_register, required_sections, expected_result):

    node_section_content = {
        SectionName("one"): NODE_1,
        SectionName("two"): NODE_1,
        SectionName("three"): NODE_1
    }

    host_key = HostKey("node1", "127.0.0.1", SourceType.HOST)

    parsed_sections_broker = ParsedSectionsBroker(
        {host_key: AgentHostSections(sections=node_section_content)})

    kwargs = parsed_sections_broker.get_section_kwargs(
        host_key,
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs,\
           "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
Пример #11
0
def test_get_section_kwargs(patch_register, required_sections, expected_result):

    node_sections = AgentHostSections(sections={
        SectionName("one"): NODE_1,
        SectionName("two"): NODE_1,
        SectionName("three"): NODE_1
    })

    host_key = HostKey("node1", "127.0.0.1", SourceType.HOST)

    parsed_sections_broker = ParsedSectionsBroker({
        host_key: SectionsParser(host_sections=node_sections),
    })

    kwargs = parsed_sections_broker.get_section_kwargs(
        host_key,
        [ParsedSectionName(n) for n in required_sections],
    )

    assert expected_result == kwargs
Пример #12
0
 def test_defaults(self, source, mode):
     assert source.summarize(result.OK(AgentHostSections()), mode=mode) == [
         ActiveCheckResult(0, "Success")
     ]
Пример #13
0
def _collect_host_sections(
    *,
    nodes: Iterable[Tuple[HostName, Optional[HostAddress],
                          Sequence['Source']]],
    file_cache_max_age: int,
    fetcher_messages: Sequence['FetcherMessage'],
    selected_sections: 'SectionNameCollection',
) -> Tuple[  #
        Mapping[HostKey, HostSections],  #
        Sequence[Tuple['Source', result.Result[HostSections, Exception]]]  #
]:
    """Gather ALL host info data for any host (hosts, nodes, clusters) in Checkmk.

    Communication errors are not raised through by this functions. All agent related errors are
    caught by the source.run() method and saved in it's _exception attribute. The caller should
    use source.get_summary_result() to get the state, output and perfdata of the agent execution
    or source.exception to get the exception object.
    """
    console.vverbose("%s+%s %s\n", tty.yellow, tty.normal,
                     "Parse fetcher results".upper())

    flat_node_sources = [(hn, ip, src) for hn, ip, sources in nodes
                         for src in sources]

    # TODO (ml): Can we somehow verify that this is correct?
    # if fetcher_message["fetcher_type"] != source.id:
    #     raise LookupError("Checker and fetcher missmatch")
    # (mo): this is not enough, but better than nothing:
    if len(flat_node_sources) != len(fetcher_messages):
        raise LookupError("Checker and fetcher missmatch")

    collected_host_sections: Dict[HostKey, HostSections] = {}
    results: List[Tuple['Source', result.Result[HostSections, Exception]]] = []
    # Special agents can produce data for the same check_plugin_name on the same host, in this case
    # the section lines need to be extended
    for fetcher_message, (hostname, ipaddress,
                          source) in zip(fetcher_messages, flat_node_sources):
        console.vverbose("  Source: %s/%s\n" %
                         (source.source_type, source.fetcher_type))

        source.file_cache_max_age = file_cache_max_age

        host_sections = collected_host_sections.setdefault(
            HostKey(hostname, ipaddress, source.source_type),
            source.default_host_sections,
        )

        source_result = source.parse(fetcher_message.raw_data,
                                     selection=selected_sections)
        results.append((source, source_result))
        if source_result.is_ok():
            console.vverbose(
                "  -> Add sections: %s\n" %
                sorted([str(s) for s in source_result.ok.sections.keys()]))
            host_sections.add(source_result.ok)
        else:
            console.vverbose("  -> Not adding sections: %s\n" %
                             source_result.error)

    for hostname, ipaddress, _sources in nodes:
        # Store piggyback information received from all sources of this host. This
        # also implies a removal of piggyback files received during previous calls.
        host_sections = collected_host_sections.setdefault(
            HostKey(hostname, ipaddress, SourceType.HOST),
            AgentHostSections(),
        )
        cmk.utils.piggyback.store_piggyback_raw_data(
            hostname,
            host_sections.piggybacked_raw_data,
        )

    return collected_host_sections, results
Пример #14
0
NODE_1: AgentRawDataSection = [
    ["node1", "data 1"],
    ["node1", "data 2"],
]

NODE_2: AgentRawDataSection = [
    ["node2", "data 1"],
    ["node2", "data 2"],
]


@pytest.mark.parametrize(
    "node_sections,expected_result",
    [
        (AgentHostSections(sections={}), None),
        (
            AgentHostSections(sections={SectionName("one"): NODE_1}),
            {
                "parsed_by": "one",
                "node": "node1"
            },
        ),
        (
            AgentHostSections(sections={SectionName("two"): NODE_1}),
            {
                "parsed_by": "two",
                "node": "node1"
            },
        ),
        (
 def check(_, *args, **kwargs):
     return result.OK(AgentHostSections(sections={section_name: [[str(section_name)]]}))
Пример #16
0
def _get_parser() -> SectionsParser:
    return SectionsParser(
        AgentHostSections(sections={
            SectionName("one"): NODE_1,
            SectionName("four"): NODE_1,
        }), )
 def sections_parser(self) -> SectionsParser:
     return SectionsParser(host_sections=AgentHostSections(sections={
         SectionName("one"): [],
         SectionName("two"): [],
     }))
Пример #18
0
 def test_defaults(self, source):
     assert source.summarize(result.OK(AgentHostSections())) == (0,
                                                                 "Success")
Пример #19
0
def _cluster_scenario(monkeypatch):
    hostname = "test-clusterhost"
    ipaddress = "1.2.3.4"
    node1_hostname = 'test-node1'
    node2_hostname = 'test-node2'

    def fake_lookup_ip_address(*_a, **_kw):
        return ipaddress

    monkeypatch.setattr(ip_lookup, "lookup_ip_address", fake_lookup_ip_address)

    ts = Scenario()
    ts.add_host(node1_hostname)
    ts.add_host(node2_hostname)
    ts.add_cluster(hostname, nodes=[node1_hostname, node2_hostname])
    ts.set_ruleset("inventory_df_rules", [{
        'value': {
            'ignore_fs_types': ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660'],
            'never_ignore_mountpoints': ['~.*/omd/sites/[^/]+/tmp$']
        },
        'condition': {
            'host_labels': {
                'cmk/check_mk_server': 'yes'
            }
        }
    }])
    ts.set_ruleset("clustered_services", [([], [node1_hostname], ['fs_'])])
    host_config = ts.apply(monkeypatch).get_host_config(hostname)

    DiscoveredHostLabelsStore(node1_hostname).save({
        'node1_existing_label': {
            'plugin_name': 'node1_plugin',
            'value': 'true',
        }
    })

    DiscoveredHostLabelsStore(hostname).save({
        'existing_label': {
            'plugin_name': 'foo',
            'value': 'bar',
        },
        'another_label': {
            'plugin_name': 'labels',
            'value': 'true',
        }
    })

    broker = ParsedSectionsBroker({
        HostKey(hostname=node1_hostname,
                ipaddress=ipaddress,
                source_type=SourceType.HOST):
        SectionsParser(host_sections=AgentHostSections(sections={
            SectionName("labels"): [
                [
                    '{"cmk/check_mk_server":"yes"}',
                ],
            ],
            SectionName("df"): [
                [
                    '/dev/sda1',
                    'vfat',
                    '523248',
                    '3668',
                    '519580',
                    '1%',
                    '/boot/test-efi',
                ],
                [
                    'tmpfs',
                    'tmpfs',
                    '8152916',
                    '244',
                    '8152672',
                    '1%',
                    '/opt/omd/sites/test-heute/tmp',
                ],
            ],
        }, ), ),
        HostKey(hostname=node2_hostname,
                ipaddress=ipaddress,
                source_type=SourceType.HOST):
        SectionsParser(host_sections=AgentHostSections(sections={
            SectionName("labels"): [
                [
                    '{"node2_live_label":"true"}',
                ],
            ],
            SectionName("df"): [
                [
                    '/dev/sda1',
                    'vfat',
                    '523248',
                    '3668',
                    '519580',
                    '1%',
                    '/boot/test-efi',
                ],
                [
                    'tmpfs',
                    'tmpfs',
                    '8152916',
                    '244',
                    '8152672',
                    '1%',
                    '/opt/omd/sites/test-heute2/tmp',
                ],
            ],
        }, ), ),
    })

    return ClusterScenario(
        host_config,
        ipaddress,
        broker,
        node1_hostname,
        node2_hostname,
    )
Пример #20
0
def _realhost_scenario(monkeypatch):
    hostname = "test-realhost"
    ipaddress = "1.2.3.4"
    ts = Scenario().add_host(hostname, ipaddress=ipaddress)
    ts.set_ruleset("inventory_df_rules", [{
        'value': {
            'ignore_fs_types': ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660'],
            'never_ignore_mountpoints': ['~.*/omd/sites/[^/]+/tmp$']
        },
        'condition': {
            'host_labels': {
                'cmk/check_mk_server': 'yes'
            }
        }
    }])
    ts.apply(monkeypatch)

    def fake_lookup_ip_address(*_a, **_kw):
        return ipaddress

    monkeypatch.setattr(ip_lookup, "lookup_ip_address", fake_lookup_ip_address)

    DiscoveredHostLabelsStore(hostname).save({
        'existing_label': {
            'plugin_name': 'foo',
            'value': 'bar',
        },
        'another_label': {
            'plugin_name': 'labels',
            'value': 'true',
        }
    })

    broker = ParsedSectionsBroker({
        HostKey(hostname=hostname,
                ipaddress=ipaddress,
                source_type=SourceType.HOST):
        AgentHostSections(
            sections={
                SectionName("labels"): [
                    [
                        '{"cmk/check_mk_server":"yes"}',
                    ],
                ],
                SectionName("df"): [
                    [
                        '/dev/sda1',
                        'vfat',
                        '523248',
                        '3668',
                        '519580',
                        '1%',
                        '/boot/test-efi',
                    ],
                    [
                        'tmpfs',
                        'tmpfs',
                        '8152916',
                        '244',
                        '8152672',
                        '1%',
                        '/opt/omd/sites/test-heute/tmp',
                    ],
                ],
            })
    })

    return RealHostScenario(hostname, ipaddress, broker)