예제 #1
0
    def _update_with_parse_function(self, section_content, section_name):
        # type: (AbstractSectionContent, SectionName) -> ParsedSectionContent
        """Transform the section_content using the defined parse functions.

        Some checks define a parse function that is used to transform the section_content
        somehow. It is applied by this function.

        Please note that this is not a check/subcheck individual setting. This option is related
        to the agent section.

        All exceptions raised by the parse function will be catched and re-raised as
        MKParseFunctionError() exceptions."""

        if section_name not in config.check_info:
            return section_content

        parse_function = cast(Callable[[AbstractSectionContent], ParsedSectionContent],
                              config.check_info[section_name]["parse_function"])
        if not parse_function:
            return section_content

        # TODO: Item state needs to be handled in local objects instead of the
        # item_state._cached_item_states object
        orig_item_state_prefix = item_state.get_item_state_prefix()
        try:
            item_state.set_item_state_prefix(section_name, None)
            return parse_function(section_content)
        except Exception:
            if cmk.utils.debug.enabled():
                raise
            raise MKParseFunctionError(*sys.exc_info())
        finally:
            item_state.set_item_state_prefix(*orig_item_state_prefix)

        return section_content
예제 #2
0
    def get_parsed_section(
            self,
            host_name,  # type: HostName
            ip_address,  # type: Optional[HostAddress]
            section_name,  # type: PluginName
    ):
        # type: (...) -> Optional[ParsedSectionContent]
        cache_key = (host_name, ip_address, section_name)
        if cache_key in self._parsed_sections:
            return self._parsed_sections[cache_key]

        section_def = self.get_raw_section(host_name, ip_address, section_name)
        if section_def is None:
            # no section creating the desired one was found, assume a 'default' section:
            raw_section_name = section_name
            parse_function = parse_string_table  # type: Union[SNMPParseFunction, AgentParseFunction]
        else:
            raw_section_name = section_def.name
            parse_function = section_def.parse_function

        try:
            hosts_raw_sections = self._multi_host_sections[(
                host_name, ip_address)].sections
            string_table = hosts_raw_sections[str(raw_section_name)]
        except KeyError:
            return self._parsed_sections.setdefault(cache_key, None)

        try:
            parsed = parse_function(string_table)
        except Exception:
            if cmk.utils.debug.enabled():
                raise
            raise MKParseFunctionError(*sys.exc_info())

        return self._parsed_sections.setdefault(cache_key, parsed)
예제 #3
0
    def _update_with_parse_function(
        section_content: AbstractSectionContent,
        section_name: SectionName,
    ) -> ParsedSectionContent:
        """Transform the section_content using the defined parse functions.

        Some checks define a parse function that is used to transform the section_content
        somehow. It is applied by this function.

        Please note that this is not a check/subcheck individual setting. This option is related
        to the agent section.

        All exceptions raised by the parse function will be catched and re-raised as
        MKParseFunctionError() exceptions."""
        section_plugin = config.get_registered_section_plugin(section_name)
        if section_plugin is None:
            # use legacy parse function for unmigrated sections
            parse_function = config.check_info.get(str(section_name),
                                                   {}).get("parse_function")
            if parse_function is None:
                return section_content
        else:
            # TODO (mo): deal with the parsed_section_name feature (CMK-4006)
            if str(section_plugin.name) != str(
                    section_plugin.parsed_section_name):
                raise NotImplementedError()
            parse_function = section_plugin.parse_function

        # TODO (mo): make this unnecessary
        parse_function = cast(
            Callable[[AbstractSectionContent], ParsedSectionContent],
            parse_function)

        # TODO: Item state needs to be handled in local objects instead of the
        # item_state._cached_item_states object
        # TODO (mo): ValueStores (formally Item state) need to be *only* available
        # from within the check function, nowhere else.
        orig_item_state_prefix = item_state.get_item_state_prefix()
        try:
            item_state.set_item_state_prefix(section_name, None)
            return parse_function(section_content)

        except item_state.MKCounterWrapped:
            raise

        except Exception:
            if cmk.utils.debug.enabled():
                raise
            raise MKParseFunctionError(*sys.exc_info())

        finally:
            item_state.set_item_state_prefix(*orig_item_state_prefix)
예제 #4
0
    def _update_with_parse_function(
        section_content: AbstractSectionContent,
        section_name: SectionName,
        check_legacy_info: Dict[str, Dict[str, Any]],
    ) -> ParsedSectionContent:
        """Transform the section_content using the defined parse functions.

        Some checks define a parse function that is used to transform the section_content
        somehow. It is applied by this function.

        Please note that this is not a check/subcheck individual setting. This option is related
        to the agent section.

        All exceptions raised by the parse function will be catched and re-raised as
        MKParseFunctionError() exceptions."""
        # We can use the migrated section: we refuse to migrate sections with
        # "'node_info'=True", so the auto-migrated ones will keep working.
        # This function will never be called on checks programmed against the new
        # API (or migrated manually)
        if not agent_based_register.is_registered_section_plugin(section_name):
            # use legacy parse function for unmigrated sections
            parse_function = check_legacy_info.get(str(section_name),
                                                   {}).get("parse_function")
        else:
            section_plugin = agent_based_register.get_section_plugin(
                section_name)
            parse_function = cast(
                Callable[[AbstractSectionContent], ParsedSectionContent],
                section_plugin.parse_function)

        if parse_function is None:
            return section_content

        # (mo): ValueStores (formally Item state) need to be *only* available
        # from within the check function, nowhere else.
        orig_item_state_prefix = item_state.get_item_state_prefix()
        try:
            item_state.set_item_state_prefix(section_name, None)
            return parse_function(section_content)

        except item_state.MKCounterWrapped:
            raise

        except Exception:
            if cmk.utils.debug.enabled():
                raise
            raise MKParseFunctionError(*sys.exc_info())

        finally:
            item_state.set_item_state_prefix(*orig_item_state_prefix)