Exemple #1
0
def test_create_agent_section_plugin():
    with pytest.raises(NotImplementedError):
        plugin = section_plugins.create_agent_section_plugin(
            name="norris",
            parsed_section_name="chuck",
            parse_function=_parse_dummy,
            supersedes=None,
            forbidden_names=[],
        )

    with pytest.raises(NotImplementedError):
        plugin = section_plugins.create_agent_section_plugin(
            name="norris",
            parsed_section_name=None,
            parse_function=_parse_dummy,
            supersedes=["Foo", "Bar"],
            forbidden_names=[],
        )

    plugin = section_plugins.create_agent_section_plugin(
        name="norris",
        parsed_section_name=None,  # "chuck"
        parse_function=_parse_dummy,
        supersedes=None,  # ["Foo", "Bar"],
        forbidden_names=[],
    )

    assert isinstance(plugin, section_types.AgentSectionPlugin)
    assert len(plugin) == 6
    assert plugin.name == SectionName("norris")
    assert plugin.parsed_section_name == ParsedSectionName(
        "norris")  # "chuck")
    assert plugin.parse_function is _parse_dummy
    assert plugin.host_label_function is section_plugins._noop_host_label_function
    assert plugin.supersedes == []  # [SectionName("Bar"), SectionName("Foo")]
Exemple #2
0
def create_agent_section_plugin_from_legacy(
    check_plugin_name: str,
    check_info_dict: Dict[str, Any],
    *,
    validate_creation_kwargs: bool,
) -> AgentSectionPlugin:
    if check_info_dict.get("node_info"):
        # We refuse to tranform these. The requirement of adding the node info
        # makes rewriting of the base code too difficult.
        # Affected Plugins must be migrated manually after CMK-4240 is done.
        raise NotImplementedError("cannot auto-migrate cluster aware plugins")

    parse_function = _create_agent_parse_function(
        check_info_dict.get('parse_function'), )

    host_label_function = _create_host_label_function(
        check_info_dict.get('inventory_function'),
        check_info_dict.get('extra_sections', []),
    )

    return create_agent_section_plugin(
        name=get_section_name(check_plugin_name),
        parse_function=parse_function,
        host_label_function=host_label_function,
        validate_creation_kwargs=validate_creation_kwargs,
    )
Exemple #3
0
def agent_section_fixture(monkeypatch):
    section = section_plugins.create_agent_section_plugin(name="unit_test_agent_section",)
    monkeypatch.setitem(
        agent_based_register._config.registered_agent_sections,
        section.name,
        section,
    )
    yield section
Exemple #4
0
def agent_section_fixture(monkeypatch):
    section = section_plugins.create_agent_section_plugin(
        name="unit_test_agent_section",
        parse_function=parse_to_string_table,
    )
    monkeypatch.setitem(
        config.registered_agent_sections,
        section.name,
        section,
    )
    yield section
Exemple #5
0
def agent_section(
    *,
    name: str,
    parse_function: Optional[AgentParseFunction] = None,
    parsed_section_name: Optional[str] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
) -> None:
    """Register an agent section to checkmk

    The section marked by '<<<name>>>' in the raw agent output will be processed
    according to the functions and options given to this function:

    Args:

      name:                The unique name of the section to be registered.
                           It must match the section header of the agent output ('<<<name>>>').

      parse_function:      The function responsible for parsing the raw agent data.
                           It must accept exactly one argument by the name 'string_table'.
                           It may return an arbitrary object. Note that if the return value is
                           `None`, no forther processing will take place (just as if the agent had
                           not sent any data).

      parsed_section_name: The name under which the parsed section will be available to the plugins.
                           Defaults to the original name.

      host_label_function: The function responsible for extracting host labels from the parsed data.
                           It must accept exactly one argument by the name 'section'.
                           When the function is called, it will be passed the parsed data as
                           returned by the parse function.
                           It is expected to yield objects of type :class:`HostLabel`.

      supersedes:          A list of section names which are superseded by this sections. If this
                           section will be parsed to something that is not `None` (see above) all
                           superseded section will not be considered at all.

    """
    section_plugin = create_agent_section_plugin(
        name=name,
        parsed_section_name=parsed_section_name,
        parse_function=parse_function,
        host_label_function=host_label_function,
        supersedes=supersedes,
        module=get_validated_plugin_module_name(),
    )

    if is_registered_section_plugin(section_plugin.name):
        raise ValueError("duplicate section definition: %s" %
                         section_plugin.name)

    add_section_plugin(section_plugin)
def test_create_agent_section_plugin():
    plugin = section_plugins.create_agent_section_plugin(
        name="norris",
        parsed_section_name="chuck",
        parse_function=_parse_dummy,
        supersedes=["foo", "bar"],
    )

    assert isinstance(plugin, AgentSectionPlugin)
    assert len(plugin) == 6
    assert plugin.name == SectionName("norris")
    assert plugin.parsed_section_name == ParsedSectionName("chuck")
    assert plugin.parse_function is _parse_dummy
    assert plugin.host_label_function is section_plugins._noop_host_label_function
    assert plugin.supersedes == {SectionName("bar"), SectionName("foo")}
Exemple #7
0
def create_agent_section_plugin_from_legacy(check_plugin_name,
                                            check_info_dict):
    # type: (str, Dict[str, Any]) -> AgentSectionPlugin

    parse_function = _create_agent_parse_function(
        check_info_dict.get('parse_function'), )

    host_label_function = _create_host_label_function(
        check_info_dict.get('inventory_function'),
        check_info_dict.get('extra_sections', []),
    )

    return create_agent_section_plugin(
        name=get_section_name(check_plugin_name),
        parse_function=parse_function,
        host_label_function=host_label_function,
        forbidden_names=[],
    )
Exemple #8
0
def agent_section(
        #*,
        name=None,  # type: Optional[str]
        parsed_section_name=None,  # type: Optional[str]
        parse_function=None,  # type: Optional[AgentParseFunction]
        host_label_function=None,  # type: Optional[HostLabelFunction]
        supersedes=None,  # type: Optional[List[str]]
):
    # type: (...) -> None
    """Register an agent section to checkmk

    The section marked by '<<<name>>>' in the raw agent output will be processed
    according to the functions and options given to this function:

    :param name: The name of the section to be processed. It must be unique, and match
        the section header of the agent oufput ('<<<name>>>').
    :params parsed_section_name: not yet implemented.
    :params parse_function: The function responsible for parsing the raw agent data.
        It must accept exactly one argument by the name 'string_table'.
        It may return an arbitrary object. Note that if the return value is falsey,
        no forther processing will take place.
    :params host_label_function: The function responsible for extracting HostLabels from
        the parsed data. It must accept exactly one argument by the name 'section'. When
        the function is called, it will be passed the parsed data as returned by the
        parse function. It is expected to yield objects of type 'HostLabel'.
    :params supersedes: not yet implemented.
    """
    # TODO (mo): unhack this CMK-3983
    if name is None or parse_function is None:
        raise TypeError()

    forbidden_names = list(config.registered_agent_sections) + list(
        config.registered_snmp_sections)

    section_plugin = create_agent_section_plugin(
        name=name,
        parsed_section_name=parsed_section_name,
        parse_function=parse_function,
        host_label_function=host_label_function,
        supersedes=supersedes,
        forbidden_names=forbidden_names,
    )

    config.registered_agent_sections[section_plugin.name] = section_plugin
Exemple #9
0
def agent_section(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: AgentParseFunction,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
) -> None:
    """Register an agent section to checkmk

    The section marked by '<<<name>>>' in the raw agent output will be processed
    according to the functions and options given to this function:

    :param name: The name of the section to be processed. It must be unique, and match
        the section header of the agent oufput ('<<<name>>>').
    :params parsed_section_name: not yet implemented.
    :params parse_function: The function responsible for parsing the raw agent data.
        It must accept exactly one argument by the name 'string_table'.
        It may return an arbitrary object. Note that if the return value is falsey,
        no forther processing will take place.
    :params host_label_function: The function responsible for extracting HostLabels from
        the parsed data. It must accept exactly one argument by the name 'section'. When
        the function is called, it will be passed the parsed data as returned by the
        parse function. It is expected to yield objects of type 'HostLabel'.
    :params supersedes: not yet implemented.
    """
    section_plugin = create_agent_section_plugin(
        name=name,
        parsed_section_name=parsed_section_name,
        parse_function=parse_function,
        host_label_function=host_label_function,
        supersedes=supersedes,
        module=get_plugin_module_name(),
    )

    if (section_plugin.name in config.registered_agent_sections
            or section_plugin.name in config.registered_snmp_sections):
        raise ValueError("duplicate section definition: %s" %
                         section_plugin.name)

    config.registered_agent_sections[section_plugin.name] = section_plugin
Exemple #10
0
def agent_section(
    *,
    name: str,
    parse_function: Optional[AgentParseFunction] = None,
    parsed_section_name: Optional[str] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    host_label_default_parameters: Optional[Dict[str, Any]] = None,
    host_label_ruleset_name: Optional[str] = None,
    host_label_ruleset_type: RuleSetType = RuleSetType.MERGED,
    supersedes: Optional[List[str]] = None,
) -> None:
    """Register an agent section to checkmk

    The section marked by '<<<name>>>' in the raw agent output will be processed
    according to the functions and options given to this function:

    Args:

      name:                The unique name of the section to be registered.
                           It must match the section header of the agent output ('<<<name>>>').

      parse_function:      The function responsible for parsing the raw agent data.
                           It must accept exactly one argument by the name 'string_table'.
                           It may return an arbitrary object. Note that if the return value is
                           `None`, no forther processing will take place (just as if the agent had
                           not sent any data).
                           This function may raise arbitrary exceptions, which will be dealt with
                           by the checking engine. You should expect well formatted data.

      parsed_section_name: The name under which the parsed section will be available to the plugins.
                           Defaults to the original name.

      host_label_function: The function responsible for extracting host labels from the parsed data.
                           It must accept exactly one argument by the name 'section'.
                           When the function is called, it will be passed the parsed data as
                           returned by the parse function.
                           It is expected to yield objects of type :class:`HostLabel`.

      host_label_default_parameters: Default parameters for the host label function. Must match
                           the ValueSpec of the corresponding WATO ruleset, if it exists.

      host_label_ruleset_name: The name of the host label ruleset.

      host_label_ruleset_type: The ruleset type is either :class:`RuleSetType.ALL` or
                           :class:`RuleSetType.MERGED`.
                           It describes whether this plugins needs the merged result of the
                           effective rules, or every individual rule matching for the current host.

      supersedes:          A list of section names which are superseded by this sections. If this
                           section will be parsed to something that is not `None` (see above) all
                           superseded section will not be considered at all.

    """
    section_plugin = create_agent_section_plugin(
        name=name,
        parsed_section_name=parsed_section_name,
        parse_function=parse_function,
        host_label_function=host_label_function,
        host_label_default_parameters=host_label_default_parameters,
        host_label_ruleset_name=host_label_ruleset_name,
        host_label_ruleset_type=host_label_ruleset_type,
        supersedes=supersedes,
        module=get_validated_plugin_module_name(),
    )

    if is_registered_section_plugin(section_plugin.name):
        raise ValueError("duplicate section definition: %s" %
                         section_plugin.name)

    add_section_plugin(section_plugin)