Exemple #1
0
def create_snmp_section_plugin(
    *,
    name: str,
    detect_spec: SNMPDetectSpec,
    trees: List[SNMPTree],
    parsed_section_name: Optional[str] = None,
    parse_function: Optional[SNMPParseFunction] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    module: Optional[str] = None,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    section_name = SectionName(name)

    _validate_detect_spec(detect_spec)
    _validate_snmp_trees(trees)

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        _create_snmp_parse_function(parse_function, trees),
        _create_host_label_function(
            host_label_function,
            default_params=None,
        ),
        _create_supersedes(section_name, supersedes),
        detect_spec,
        trees,
        module,
    )
Exemple #2
0
def create_snmp_section_plugin(
    *,
    name: str,
    detect_spec: SNMPDetectBaseType,
    fetch: Union[SNMPTree, List[SNMPTree]],
    parsed_section_name: Optional[str] = None,
    parse_function: Union[SimpleSNMPParseFunction, SNMPParseFunction,
                          None] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    module: Optional[str] = None,
    validate_creation_kwargs: bool = True,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    section_name = SectionName(name)

    # normalize to List[SNMPTree]
    tree_list = [fetch] if isinstance(fetch, SNMPTree) else fetch

    if validate_creation_kwargs:
        _validate_detect_spec(detect_spec)
        _validate_fetch_spec(tree_list)

        if parse_function is not None:
            needs_bytes = any(oid.encoding == "binary" for tree in tree_list
                              for oid in tree.oids)
            _validate_parse_function(
                parse_function,
                expected_annotation=_create_parse_annotation(
                    needs_bytes=needs_bytes,
                    is_list=isinstance(fetch, list),
                ),
            )

        if host_label_function is not None:
            validate_function_arguments(
                type_label="host_label",
                function=host_label_function,
                has_item=False,
                default_params=None,  # CMK-5181
                sections=[ParsedSectionName("__always_just_one_section__")],
            )

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        _create_snmp_parse_function(parse_function,
                                    isinstance(fetch, SNMPTree)),
        _create_host_label_function(host_label_function),
        _create_supersedes(section_name, supersedes),
        detect_spec,
        tree_list,
        module,
    )
Exemple #3
0
def create_snmp_section_plugin(
    *,
    name: str,
    detect_spec: SNMPDetectSpec,
    trees: List[SNMPTree],
    parsed_section_name: Optional[str] = None,
    parse_function: Optional[SNMPParseFunction] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    module: Optional[str] = None,
    validate_creation_kwargs: bool = True,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    section_name = SectionName(name)

    if validate_creation_kwargs:
        _validate_detect_spec(detect_spec)
        _validate_snmp_trees(trees)

        if parse_function is not None:
            needs_bytes = any(
                isinstance(oid, OIDBytes) for tree in trees
                for oid in tree.oids)
            _validate_parse_function(
                parse_function,
                expected_annotation=(  #
                    (SNMPStringByteTable,
                     "SNMPStringByteTable") if needs_bytes else
                    (SNMPStringTable, "SNMPStringTable")),
            )

        if host_label_function is not None:
            validate_function_arguments(
                type_label="host_label",
                function=host_label_function,
                has_item=False,
                default_params=None,  # CMK-5181
                sections=[ParsedSectionName("__always_just_one_section__")],
            )

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        _create_snmp_parse_function(parse_function, trees),
        _create_host_label_function(host_label_function),
        _create_supersedes(section_name, supersedes),
        detect_spec,
        trees,
        module,
    )
Exemple #4
0
def create_snmp_section_plugin(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: SNMPParseFunction,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    detect_spec: SNMPDetectSpec,
    trees: List[SNMPTree],
    module: Optional[str] = None,
    rule_dependent_detect_spec: Optional[SNMPRuleDependentDetectSpec] = None,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.

    Args:
        rule_dependent_detect_spec: Not an official part of the API. Used for dynamically computing
        SNMP detection conditions based on the configured discovery rules for the current host. This
        is needed by for example needed by some interface checks.
    """
    section_name = SectionName(name)

    if any(isinstance(oid, OIDBytes) for tree in trees for oid in tree.oids):
        expected_annotation: Tuple[Type, str] = (SNMPStringByteTable,
                                                 "SNMPStringByteTable")
    else:
        expected_annotation = (SNMPStringTable, "SNMPStringTable")

    _validate_parse_function(parse_function,
                             expected_annotation=expected_annotation)
    _validate_detect_spec(detect_spec)
    _validate_snmp_trees(trees)

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        parse_function,
        _create_host_label_function(
            host_label_function,
            default_params=None,
        ),
        _create_supersedes(section_name, supersedes),
        detect_spec,
        trees,
        module,
        rule_dependent_detect_spec,
    )
Exemple #5
0
def create_snmp_section_plugin(
    *,
    name: str,
    parsed_section_name: Optional[str] = None,
    parse_function: SNMPParseFunction,
    host_label_function: Optional[HostLabelFunction] = None,
    supersedes: Optional[List[str]] = None,
    detect_spec: SNMPDetectSpec,
    trees: List[SNMPTree],
    module: Optional[str] = None,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    # TODO (mo): Well, implement it, and remove pragma below!
    if supersedes is not None:
        raise NotImplementedError("supersedes is not yet available")
    if parsed_section_name is not None:
        raise NotImplementedError("parsed_section_name is not yet available")

    section_name = SectionName(name)

    if any(isinstance(oid, OIDBytes) for tree in trees for oid in tree.oids):
        expected_annotation: Tuple[Type, str] = (SNMPStringByteTable, "SNMPStringByteTable")
    else:
        expected_annotation = (SNMPStringTable, "SNMPStringTable")

    _validate_parse_function(parse_function, expected_annotation=expected_annotation)
    _validate_detect_spec(detect_spec)
    _validate_snmp_trees(trees)

    return SNMPSectionPlugin(
        section_name,
        ParsedSectionName(parsed_section_name if parsed_section_name else str(section_name)),
        parse_function,
        _create_host_label_function(host_label_function),
        _create_supersedes(supersedes),
        detect_spec,
        trees,
        module,
    )
Exemple #6
0
def create_snmp_section_plugin(
    *,
    name: str,
    detect_spec: SNMPDetectBaseType,
    fetch: Union[SNMPTree, List[SNMPTree]],
    parsed_section_name: Optional[str] = None,
    parse_function: Union[SimpleSNMPParseFunction, SNMPParseFunction,
                          None] = None,
    host_label_function: Optional[HostLabelFunction] = None,
    host_label_default_parameters: Optional[Dict] = None,
    host_label_ruleset_name: Optional[str] = None,
    host_label_ruleset_type: RuleSetType = "merged",
    supersedes: Optional[List[str]] = None,
    module: Optional[str] = None,
    validate_creation_kwargs: bool = True,
) -> SNMPSectionPlugin:
    """Return an SNMPSectionPlugin object after validating and converting the arguments one by one

    For a detailed description of the parameters please refer to the exposed function in the
    'register' namespace of the API.
    """
    section_name = SectionName(name)

    # normalize to List[SNMPTree]
    tree_list = [fetch] if isinstance(fetch, SNMPTree) else fetch

    if validate_creation_kwargs:
        _validate_detect_spec(detect_spec)
        _validate_fetch_spec(tree_list)

        if parse_function is not None:
            needs_bytes = any(oid.encoding == "binary" for tree in tree_list
                              for oid in tree.oids)
            _validate_parse_function(
                parse_function,
                expected_annotation=_create_parse_annotation(
                    needs_bytes=needs_bytes,
                    is_list=isinstance(fetch, list),
                ),
            )

        if host_label_function is not None:
            _validate_host_label_kwargs(
                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,
            )

    return SNMPSectionPlugin(
        name=section_name,
        parsed_section_name=ParsedSectionName(
            parsed_section_name if parsed_section_name else str(section_name)),
        parse_function=_create_snmp_parse_function(parse_function,
                                                   isinstance(fetch,
                                                              SNMPTree)),
        host_label_function=_create_host_label_function(host_label_function),
        host_label_default_parameters=host_label_default_parameters,
        host_label_ruleset_name=(None if host_label_ruleset_name is None else
                                 RuleSetName(host_label_ruleset_name)),
        host_label_ruleset_type=host_label_ruleset_type,
        supersedes=_create_supersedes(section_name, supersedes),
        detect_spec=detect_spec,
        trees=tree_list,
        module=module,
    )