Example #1
0
 def test_rule_indipendent(
     self,
     monkeypatch,
     hostname,
     ipaddress,
 ):
     plugin = section_plugins.create_snmp_section_plugin(
         name="norris",
         parse_function=lambda string_table: None,
         trees=[
             SNMPTree(
                 base='.1.2.3',
                 oids=['2.3'],
             ),
         ],
         detect_spec=SNMPDetectSpec([[('.1.2.3.4.5', 'Foo.*', True)]]),
     )
     snmp_configurator = self.do_monkeypatch_and_make_configurator(
         monkeypatch,
         plugin,
         hostname,
         ipaddress,
     )
     assert snmp_configurator._make_snmp_scan_sections() == [
         SNMPScanSection(
             plugin.name,
             plugin.detect_spec,
         )
     ]
Example #2
0
    def _make_snmp_scan_sections(self) -> Iterable[SNMPScanSection]:
        """Create list of all SNMP scan specifications.

        Here, we evaluate the rule_dependent_detect_spec-attribute of SNMPSectionPlugin. This
        attribute is not an official part of the API. It allows for dynamically computing SNMP
        detection specifications based on user-defined discovery rulesets. This is needed by some
        check plugins, such as if and if64.

        In case this attribute is not set, we simply use SNMPSectionPlugin.detect_spec, which is
        the official way for specifying detection conditions.
        """
        snmp_scan_sections = []
        config_cache = config.get_config_cache()

        for snmp_section_plugin in agent_based_register.iter_all_snmp_sections(
        ):
            if snmp_section_plugin.rule_dependent_detect_spec is None:
                detect_spec = snmp_section_plugin.detect_spec
            else:
                # fetch user-defined discovery rules
                rules_for_host = [
                    config_cache.host_extra_conf(
                        self.hostname,
                        agent_based_register.get_discovery_ruleset(ruleset),
                    ) for ruleset in
                    snmp_section_plugin.rule_dependent_detect_spec.rulesets
                ]
                # call evaluator with these rules, returning an SNMPDetectSpec
                detect_spec = snmp_section_plugin.rule_dependent_detect_spec.evaluator(
                    *rules_for_host)

            snmp_scan_sections.append(
                SNMPScanSection(snmp_section_plugin.name, detect_spec))

        return snmp_scan_sections
Example #3
0
 def sections(self) -> Iterable[SNMPScanSection]:
     sections = []
     for name, section in config.registered_snmp_sections.items():
         if self._for_inventory and not inventory_plugins.is_snmp_plugin(
                 str(name)):
             continue
         sections.append(SNMPScanSection(section.name, section.detect_spec))
     return sections
Example #4
0
def test_snmp_scan_find_plugins__success(backend):
    sections = [
        SNMPScanSection(_.name, _.detect_spec) for _ in config.registered_snmp_sections.values()
    ]
    found = snmp_scan._snmp_scan_find_plugins(
        sections,
        do_snmp_scan=False,
        on_error="raise",
        backend=backend,
    )

    assert sections
    assert found
    assert len(sections) > len(found)
Example #5
0
def test_snmp_scan_find_plugins__success(backend):
    sections = [
        SNMPScanSection(_.name, _.detect_spec)
        for _ in agent_based_register.iter_all_snmp_sections()
    ]
    found = snmp_scan._snmp_scan_find_sections(
        sections,
        on_error="raise",
        backend=backend,
    )

    assert sections
    assert found
    assert len(sections) > len(found)
Example #6
0
def test_gather_available_raw_section_names_defaults(backend, mocker):
    assert snmp_cache.get_oid_from_single_oid_cache(snmp_scan.OID_SYS_DESCR)
    assert snmp_cache.get_oid_from_single_oid_cache(snmp_scan.OID_SYS_OBJ)

    assert snmp_scan.gather_available_raw_section_names(
        [SNMPScanSection(_.name, _.detect_spec) for _ in config.registered_snmp_sections.values()],
        on_error="raise",
        do_snmp_scan=False,
        binary_host=False,
        backend=backend,
    ) == {
        "hr_mem",
        "mgmt_snmp_info",
        "mgmt_snmp_uptime",
        "snmp_info",
        "snmp_os",
        "snmp_uptime",
    }
Example #7
0
def test_gather_available_raw_section_names_defaults(backend, mocker):
    assert snmp_cache.get_oid_from_single_oid_cache(snmp_scan.OID_SYS_DESCR)
    assert snmp_cache.get_oid_from_single_oid_cache(snmp_scan.OID_SYS_OBJ)

    assert snmp_scan.gather_available_raw_section_names(
        [
            SNMPScanSection(_.name, _.detect_spec)
            for _ in agent_based_register.iter_all_snmp_sections()
        ],
        on_error="raise",
        binary_host=False,
        backend=backend,
    ) == {
        SectionName("hr_mem"),
        SectionName("mgmt_snmp_info"),
        SectionName("snmp_info"),
        SectionName("snmp_os"),
        SectionName("snmp_uptime"),
    }
Example #8
0
    def test_rule_dependent(
        self,
        monkeypatch,
        discovery_rulesets,
        hostname,
        ipaddress,
    ):
        detect_spec_1 = SNMPDetectSpec([[('.1.2.3.4.5', 'Bar.*', False)]])
        detect_spec_2 = SNMPDetectSpec([[('.7.8.9', 'huh.*', True)]])

        def evaluator(discovery_ruleset):
            if len(discovery_ruleset) > 0 and discovery_ruleset[0]:
                return detect_spec_1
            return detect_spec_2

        plugin = section_plugins.create_snmp_section_plugin(
            name="norris",
            parse_function=lambda string_table: None,
            trees=[
                SNMPTree(
                    base='.1.2.3',
                    oids=['2.3'],
                ),
            ],
            detect_spec=SNMPDetectSpec([[('.1.2.3.4.5', 'Foo.*', True)]]),
            rule_dependent_detect_spec=SNMPRuleDependentDetectSpec(
                [RuleSetName('discovery_ruleset')],
                evaluator,
            ),
        )
        snmp_configurator = self.do_monkeypatch_and_make_configurator(
            monkeypatch,
            plugin,
            hostname,
            ipaddress,
        )
        assert snmp_configurator._make_snmp_scan_sections() == [
            SNMPScanSection(
                plugin.name,
                detect_spec_1,
            )
        ]
Example #9
0
 def sections(self) -> Iterable[SNMPScanSection]:
     return [
         SNMPScanSection(section.name, section.detect_spec)
         for section in config.registered_snmp_sections.values()
     ]
Example #10
0
 def sections() -> Iterable[SNMPScanSection]:
     return [
         SNMPScanSection(section.name, section.detect_spec)
         for section in agent_based_register.iter_all_snmp_sections()
     ]