Пример #1
0
def test_factory_snmp_backend_unknown_backend(snmp_config):
    with pytest.raises(NotImplementedError, match="Unknown SNMP backend"):
        snmp_config = snmp_config._replace(snmp_backend="bla")
        if inline:
            assert isinstance(factory.backend(snmp_config, logging.getLogger()),
                              inline.InlineSNMPBackend)
        else:
            assert isinstance(
                factory.backend(snmp_config, logging.getLogger()),
                ClassicSNMPBackend,
            )
Пример #2
0
    def __call__(
        self,
        snmp_config: SNMPHostConfig,
        on_error: str,
        do_snmp_scan: bool,
        for_mgmt_board: bool,
    ) -> Set[SectionName]:
        """Returns a list of raw sections that shall be processed by this source.

        The logic is only processed once. Once processed, the answer is cached.
        """
        if self._filter_function is None:
            raise MKGeneralException(
                "The check type filter function has not been set")

        if self._cached_result is not None:
            return self._cached_result

        # Make hostname globally available for scan functions.
        # This is rarely used, but e.g. the scan for if/if64 needs
        # this to evaluate if_disabled_if64_checks.
        check_api_utils.set_hostname(snmp_config.hostname)
        found_plugins = self._filter_function(
            self.sections(),
            on_error=on_error,
            do_snmp_scan=do_snmp_scan,
            binary_host=config.get_config_cache().in_binary_hostlist(
                snmp_config.hostname,
                config.snmp_without_sys_descr,
            ),
            backend=factory.backend(snmp_config),
        )
        self._cached_result = {
            SectionName(s)
            for s in config.filter_by_management_board(
                snmp_config.hostname,
                found_plugins,
                for_mgmt_board=for_mgmt_board,
                for_discovery=True,
            )
        }
        return self._cached_result
Пример #3
0
    def __call__(
        self,
        snmp_config: SNMPHostConfig,
    ) -> Set[SectionName]:
        """Returns a list of raw sections that shall be processed by this source.

        The logic is only processed once. Once processed, the answer is cached.
        """
        if self._cached_result is not None:
            return self._cached_result

        self._cached_result = gather_available_raw_section_names(
            self.sections(),
            on_error=self.on_error,
            do_snmp_scan=self.do_snmp_scan,
            binary_host=config.get_config_cache().in_binary_hostlist(
                snmp_config.hostname,
                config.snmp_without_sys_descr,
            ),
            backend=factory.backend(snmp_config),
        )
        return self._cached_result
Пример #4
0
def _snmp_scan(host_config,
               on_error="ignore",
               for_inv=False,
               do_snmp_scan=True,
               for_mgmt_board=False):
    # type: (SNMPHostConfig, str, bool, bool, bool) -> Set[CheckPluginName]
    import cmk.base.inventory_plugins as inventory_plugins  # pylint: disable=import-outside-toplevel
    backend = factory.backend(host_config)

    # Make hostname globally available for scan functions.
    # This is rarely used, but e.g. the scan for if/if64 needs
    # this to evaluate if_disabled_if64_checks.
    check_api_utils.set_hostname(host_config.hostname)

    snmp_cache.initialize_single_oid_cache(host_config)
    console.vverbose("  SNMP scan:\n")
    if not config.get_config_cache().in_binary_hostlist(
            host_config.hostname, config.snmp_without_sys_descr):
        for oid, name in [(".1.3.6.1.2.1.1.1.0", "system description"),
                          (".1.3.6.1.2.1.1.2.0", "system object")]:
            value = snmp.get_single_oid(host_config,
                                        oid,
                                        do_snmp_scan=do_snmp_scan,
                                        backend=backend)
            if value is None:
                raise MKSNMPError(
                    "Cannot fetch %s OID %s. This might be OK for some bogus devices. "
                    "In that case please configure the ruleset \"Hosts without system "
                    "description OID\" to tell Check_MK not to fetch the system "
                    "description and system object OIDs." % (name, oid))
    else:
        # Fake OID values to prevent issues with a lot of scan functions
        console.vverbose(
            "       Skipping system description OID "
            "(Set .1.3.6.1.2.1.1.1.0 and .1.3.6.1.2.1.1.2.0 to \"\")\n")
        snmp_cache.set_single_oid_cache(".1.3.6.1.2.1.1.1.0", "")
        snmp_cache.set_single_oid_cache(".1.3.6.1.2.1.1.2.0", "")

    # TODO (mo): Assumption here is that inventory plugins are significantly fewer
    #            than check plugins. We should pass an explicit list along, instead
    #            of this flag. That way we would also get rid of the import above.
    if for_inv:
        section_names = [PluginName(n) for n in inventory_plugins.inv_info]
        these_sections = [
            config.registered_snmp_sections[section_name]
            for section_name in section_names
            if section_name in config.registered_snmp_sections
        ]
    else:
        these_sections = list(config.registered_snmp_sections.values())

    found_plugins = set()  # type: Set[CheckPluginName]

    for section_plugin in these_sections:
        try:
            if _evaluate_snmp_detection(
                    section_plugin.detect_spec,
                    host_config,
                    str(section_plugin.name),
                    do_snmp_scan,
                    backend=backend,
            ):
                found_plugins.add(str(section_plugin.name))
        except MKGeneralException:
            # some error messages which we explicitly want to show to the user
            # should be raised through this
            raise
        except Exception:
            if on_error == "warn":
                console.warning("   Exception in SNMP scan function of %s" %
                                section_plugin.name)
            elif on_error == "raise":
                raise

    _output_snmp_check_plugins("SNMP scan found", found_plugins)

    filtered = config.filter_by_management_board(
        host_config.hostname,
        found_plugins,
        for_mgmt_board,
        for_discovery=True,
        for_inventory=for_inv,
    )

    _output_snmp_check_plugins("SNMP filtered check plugin names", filtered)
    snmp_cache.write_single_oid_cache(host_config)
    return filtered
Пример #5
0
def test_factory_snmp_backend_inline_legacy(snmp_config):
    snmp_config = snmp_config._replace(snmp_backend=SNMPBackend.inline_legacy)
    if inline:
        assert isinstance(factory.backend(snmp_config, logging.getLogger()),
                          inline.InlineSNMPBackend)
Пример #6
0
def test_factory_snmp_backend_inline(snmp_config):
    snmp_config = snmp_config._replace(snmp_backend=SNMPBackend.inline)
    if pysnmp_backend:
        assert isinstance(factory.backend(snmp_config, logging.getLogger()),
                          pysnmp_backend.PySNMPBackend)
Пример #7
0
def test_factory_snmp_backend_classic(snmp_config):
    assert isinstance(factory.backend(snmp_config, logging.getLogger()), ClassicSNMPBackend)