def get_all_snmp_tables(info):
     backend = SNMPTestBackend(SNMPConfig, logger)
     if not isinstance(info, list):
         return snmp_table.get_snmp_table(SectionName("unit_test"), info, backend=backend)
     return [
         snmp_table.get_snmp_table(SectionName("unit_test"), i, backend=backend) for i in info
     ]
Beispiel #2
0
    def _fetch_from_io(self, mode: Mode) -> SNMPRawData:
        """Select the sections we need to fetch and do that

        Note:
            There still may be some fetching from cache involved
            if the fetch interval was overridden by the user.

        Detection:

         * Mode.DISCOVERY:
           In this straight forward case we must determine all applicable sections for
           the device in question.

         * Mode.INVENTORY
           There is no need to try to detect all sections: For the inventory we have a
           set of sections known to be relevant for inventory plugins, and we can restrict
           detection to those.

         * Mode.CHECKING
           Sections needed for checking are known without detection. If the status data
           inventory is enabled, we detect from the inventory sections; but not those,
           which are fetched for checking anyway.

        """
        now = int(time.time())
        persisted_sections = (self._section_store.load() if mode is Mode.CHECKING else
                              PersistedSections[SNMPRawDataSection]({}))
        section_names = self._get_selection(mode)
        section_names |= self._detect(select_from=self._get_detected_sections(mode) - section_names)

        walk_cache = snmp_table.WalkCache(self._backend.hostname)
        if self._use_snmpwalk_cache(mode):
            walk_cache_msg = "SNMP walk cache is enabled: Use any locally cached information"
            walk_cache.load(trees=(tree for section_name in section_names
                                   for tree in self.plugin_store[section_name].trees),)
        else:
            walk_cache_msg = "SNMP walk cache is disabled"

        fetched_data: MutableMapping[SectionName, SNMPRawDataSection] = {}
        for section_name in self._sort_section_names(section_names):
            try:
                _from, until, _section = persisted_sections[section_name]
                if now > until:
                    raise LookupError(section_name)
            except LookupError:
                self._logger.debug("%s: Fetching data (%s)", section_name, walk_cache_msg)

                fetched_data[section_name] = [
                    snmp_table.get_snmp_table(
                        section_name=section_name,
                        tree=tree,
                        walk_cache=walk_cache,
                        backend=self._backend,
                    ) for tree in self.plugin_store[section_name].trees
                ]

        walk_cache.save()

        return fetched_data
Beispiel #3
0
 def get_all_snmp_tables(info):
     backend = SNMPTestBackend(SNMPConfig, logger)
     if not isinstance(info, list):
         return snmp_table.get_snmp_table(
             section_name=SectionName("unit_test"),
             tree=info,
             walk_cache={},
             backend=backend,
         )
     return [
         snmp_table.get_snmp_table(
             section_name=SectionName("unit_test"),
             tree=i,
             walk_cache={},
             backend=backend,
         ) for i in info
     ]
Beispiel #4
0
def test_get_simple_snmp_table_wrong_credentials(backend):
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config.update(credentials="dingdong")

    # TODO: Unify different error messages
    if backend.config.snmp_backend == SNMPBackend.inline:
        exc_match = "SNMP query timed out"
    else:
        exc_match = "Timeout: No Response from"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            section_name=SectionName("my_Section"),
            oid_info=INFO_TREE,
            backend=backend,
        )
Beispiel #5
0
def test_get_simple_snmp_table_not_resolvable(backend):
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config.update(ipaddress="bla.local")

    # TODO: Unify different error messages
    if backend.config.snmp_backend == SNMPBackend.inline:
        exc_match = "Failed to initiate SNMP"
    else:
        exc_match = "Unknown host"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            section_name=SectionName("my_Section"),
            oid_info=INFO_TREE,
            backend=backend,
        )
Beispiel #6
0
def test_get_simple_snmp_table_wrong_credentials(backend: SNMPBackend) -> None:
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config._replace(credentials="dingdong")

    # TODO: Unify different error messages
    if backend.config.snmp_backend == SNMPBackendEnum.INLINE:
        exc_match = "SNMP query timed out"
    else:
        exc_match = "Timeout: No Response from"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            section_name=SectionName("my_Section"),
            tree=INFO_TREE,
            walk_cache={},
            backend=backend,
        )
Beispiel #7
0
def test_get_simple_snmp_table_wrong_credentials(backend):
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config.update(credentials="dingdong")
    oid_info = SNMPTree(
        base=".1.3.6.1.2.1.1",
        oids=["1.0", "2.0", "5.0"],
    )

    # TODO: Unify different error messages
    if backend.config.snmp_backend == "inline":
        exc_match = "SNMP query timed out"
    else:
        exc_match = "Timeout: No Response from"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            section_name=SectionName("my_Section"),
            oid_info=oid_info,
            backend=backend,
        )
Beispiel #8
0
def test_get_simple_snmp_table_not_resolvable(backend):
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config.update(ipaddress="bla.local")
    oid_info = SNMPTree(
        base=".1.3.6.1.2.1.1",
        oids=["1.0", "2.0", "5.0"],
    )

    # TODO: Unify different error messages
    if backend.config.snmp_backend == "inline":
        exc_match = "Failed to initiate SNMP"
    else:
        exc_match = "Unknown host"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            section_name=SectionName("my_Section"),
            oid_info=oid_info,
            backend=backend,
        )
Beispiel #9
0
def test_get_simple_snmp_table_wrong_credentials(backend):
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config.update(credentials="dingdong")
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.1",
        ["1.0", "2.0", "5.0"],
    )

    # TODO: Unify different error messages
    if backend.config.is_inline_snmp_host:
        exc_match = "SNMP query timed out"
    else:
        exc_match = "Timeout: No Response from"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            check_plugin_name="",
            oid_info=oid_info,
            backend=backend,
        )
Beispiel #10
0
def test_get_simple_snmp_table_not_resolvable(backend):
    if backend.config.is_usewalk_host:
        pytest.skip("Not relevant")

    backend.config = backend.config.update(ipaddress="bla.local")
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.1",
        ["1.0", "2.0", "5.0"],
    )

    # TODO: Unify different error messages
    if backend.config.is_inline_snmp_host:
        exc_match = "Failed to initiate SNMP"
    else:
        exc_match = "Unknown host"

    with pytest.raises(MKSNMPError, match=exc_match):
        snmp_table.get_snmp_table(
            check_plugin_name="",
            oid_info=oid_info,
            backend=backend,
        )
Beispiel #11
0
def test_get_data_types(backend, type_name, oid, expected_response):
    response = snmp_modes.get_single_oid(oid, backend=backend)
    assert response == expected_response
    assert isinstance(response, str)

    oid_start, oid_end = oid.rsplit(".", 1)
    table = snmp_table.get_snmp_table(
        check_plugin_name="",
        oid_info=(oid_start, [oid_end]),
        backend=backend,
    )

    assert table[0][0] == expected_response
    assert isinstance(table[0][0], str)
Beispiel #12
0
def test_get_data_types(backend, type_name, oid, expected_response):
    response = snmp_modes.get_single_oid(oid, backend=backend)
    assert response == expected_response
    assert isinstance(response, str)

    oid_start, oid_end = oid.rsplit(".", 1)
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=SNMPTree(base=oid_start, oids=[oid_end]),
        backend=backend,
    )

    assert table[0][0] == expected_response
    assert isinstance(table[0][0], str)
Beispiel #13
0
def test_get_simple_snmp_table_oid_end_bin(backend):
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.2.2.1",
        ["1", "2", "3", OID_END_BIN],
    )
    table = snmp_table.get_snmp_table(
        check_plugin_name="",
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u'1', u'lo', u'24', u'\x01'],
        [u'2', u'eth0', u'6', u'\x02'],
    ]
Beispiel #14
0
def test_get_simple_snmp_table_oid_string(backend):
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.2.2.1",
        ["1", "2", "3", OID_STRING],
    )
    table = snmp_table.get_snmp_table(
        check_plugin_name="",
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u'1', u'lo', u'24', u'.1.3.6.1.2.1.2.2.1.1.1'],
        [u'2', u'eth0', u'6', u'.1.3.6.1.2.1.2.2.1.1.2'],
    ]
Beispiel #15
0
def test_get_simple_snmp_table(backend):
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=INFO_TREE,
        backend=backend,
    )

    assert table == [
        [
            u'Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686',
            u'.1.3.6.1.4.1.8072.3.2.10',
            u'new system name',
        ],
    ]
    assert isinstance(table[0][0], str)
Beispiel #16
0
def test_get_simple_snmp_table_oid_bin(backend):
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.2.2.1",
        ["1", "2", "3", OID_BIN],
    )
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u'1', u'lo', u'24', u'\x01\x03\x06\x01\x02\x01\x02\x02\x01\x01\x01'],
        [u'2', u'eth0', u'6', u'\x01\x03\x06\x01\x02\x01\x02\x02\x01\x01\x02'],
    ]
Beispiel #17
0
def test_get_simple_snmp_table_oid_end(backend):
    oid_info = SNMPTree(
        base=".1.3.6.1.2.1.2.2.1",
        oids=["1", "2", "3", OIDEnd()],
    )
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u'1', u'lo', u'24', u'1'],
        [u'2', u'eth0', u'6', u'2'],
    ]
Beispiel #18
0
def test_get_data_types(backend, type_name, oid, expected_response):
    response = snmp_modes.get_single_oid(oid, backend=backend)
    assert response == expected_response
    assert isinstance(response, str)

    oid_start, oid_end = oid.rsplit(".", 1)
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        tree=BackendSNMPTree(base=oid_start,
                             oids=[BackendOIDSpec(oid_end, "string", False)]),
        backend=backend,
        walk_cache={},
    )

    assert table[0][0] == expected_response
    assert isinstance(table[0][0], str)
Beispiel #19
0
def test_get_simple_snmp_table(backend):
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        tree=INFO_TREE,
        walk_cache={},
        backend=backend,
    )

    assert table == [
        [
            "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686",
            ".1.3.6.1.4.1.8072.3.2.10",
            "new system name",
        ],
    ]
    assert isinstance(table[0][0], str)
Beispiel #20
0
def test_get_simple_snmp_table_fills_cache(backend):

    walk_cache: MutableMapping[str, Tuple[bool, List[Tuple[str, bytes]]]] = {}

    _ = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        tree=INFO_TREE,
        walk_cache=walk_cache,
        backend=backend,
    )

    assert sorted(walk_cache) == [
        ".1.3.6.1.2.1.1.1.0",
        ".1.3.6.1.2.1.1.2.0",
        ".1.3.6.1.2.1.1.5.0",
    ]
Beispiel #21
0
def test_get_simple_snmp_table_oid_end_bin(backend):
    oid_info = SNMPTree(
        base=".1.3.6.1.2.1.2.2.1",
        # deprecated with checkmk version 2.0
        oids=["1", "2", "3", OID_END_BIN],  # type: ignore[list-item]
    )
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u'1', u'lo', u'24', u'\x01'],
        [u'2', u'eth0', u'6', u'\x02'],
    ]
Beispiel #22
0
def test_get_simple_snmp_table_bulkwalk(backend, bulk):
    backend.config = backend.config.update(is_bulkwalk_host=bulk)
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        tree=INFO_TREE,
        walk_cache={},
        backend=backend,
    )

    assert table == [
        [
            u'Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686',
            u'.1.3.6.1.4.1.8072.3.2.10',
            u'new system name',
        ],
    ]
    assert isinstance(table[0][0], str)
Beispiel #23
0
def test_get_simple_snmp_table_with_hex_str(backend):
    oid_info = SNMPTree(
        base=".1.3.6.1.2.1.2.2.1",
        oids=["6"],
    )

    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u''],
        [
            u'\x00\x12yb\xf9@',
        ],
    ]
Beispiel #24
0
def test_get_simple_snmp_table_bulkwalk(backend: SNMPBackend,
                                        bulk: bool) -> None:
    backend.config = backend.config._replace(is_bulkwalk_host=bulk)
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        tree=INFO_TREE,
        walk_cache={},
        backend=backend,
    )

    assert table == [
        [
            "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686",
            ".1.3.6.1.4.1.8072.3.2.10",
            "new system name",
        ],
    ]
    assert isinstance(table[0][0], str)
Beispiel #25
0
def test_get_simple_snmp_table_with_hex_str(backend):
    oid_info = BackendSNMPTree(
        base=".1.3.6.1.2.1.2.2.1",
        oids=[BackendOIDSpec("6", "string", False)],
    )

    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        tree=oid_info,
        walk_cache={},
        backend=backend,
    )

    assert table == [
        [""],
        [
            "\x00\x12yb\xf9@",
        ],
    ]
Beispiel #26
0
def test_get_simple_snmp_table(backend):
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.1",
        ["1.0", "2.0", "5.0"],
    )
    table = snmp_table.get_snmp_table(
        check_plugin_name="",
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [
            u'Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686',
            u'.1.3.6.1.4.1.8072.3.2.10',
            u'new system name',
        ],
    ]
    assert isinstance(table[0][0], str)
Beispiel #27
0
def test_get_simple_snmp_table(backend):
    oid_info = SNMPTree(
        base=".1.3.6.1.2.1.1",
        oids=["1.0", "2.0", "5.0"],
    )
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [
            u'Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686',
            u'.1.3.6.1.4.1.8072.3.2.10',
            u'new system name',
        ],
    ]
    assert isinstance(table[0][0], str)
Beispiel #28
0
def test_get_simple_snmp_table_with_hex_str(backend):
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.2.2.1",
        [
            "6",
        ],
    )

    table = snmp_table.get_snmp_table(
        check_plugin_name="",
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u''],
        [
            u'\x00\x12yb\xf9@',
        ],
    ]
Beispiel #29
0
def test_get_simple_snmp_table_with_hex_str(backend):
    oid_info: OIDWithColumns = (
        ".1.3.6.1.2.1.2.2.1",
        [
            "6",
        ],
    )

    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u''],
        [
            u'\x00\x12yb\xf9@',
        ],
    ]
Beispiel #30
0
def test_get_simple_snmp_table_oid_end(backend):
    oid_info = BackendSNMPTree(
        base=".1.3.6.1.2.1.2.2.1",
        oids=[
            BackendOIDSpec("1", "string", False),
            BackendOIDSpec("2", "string", False),
            BackendOIDSpec("3", "string", False),
            BackendOIDSpec(SpecialColumn.END, "string", False),
        ],
    )
    table = snmp_table.get_snmp_table(
        section_name=SectionName("my_Section"),
        oid_info=oid_info,
        backend=backend,
    )

    assert table == [
        [u'1', u'lo', u'24', u'1'],
        [u'2', u'eth0', u'6', u'2'],
    ]