def test_cache_ignores_non_save_oids(self) -> None: """ If one plugin wants live data, and the other one wants cached data, the live data requirement should win. """ fetchoid = ".1.2.3" path = f"OID{fetchoid}" cache = MockWalkCache({path: [("23", b"42")]}) assert not cache cache.load( trees=[ BackendSNMPTree( base=".1.2", oids=[BackendOIDSpec("3", "string", False)], ), BackendSNMPTree( base=".1.2", oids=[BackendOIDSpec("3", "string", True)], ), ] ) assert fetchoid not in cache
def store(self): return SNMPPluginStore({ SectionName("section0"): SNMPPluginStoreItem( [ BackendSNMPTree(base=".1.2.3", oids=[ BackendOIDSpec("4.5", "string", False), BackendOIDSpec("9.7", "string", False) ]), BackendSNMPTree(base=".8.9.0", oids=[ BackendOIDSpec("1.2", "string", False), BackendOIDSpec("3.4", "string", False) ]), ], SNMPDetectSpec([[ ("oid0", "regex0", True), ("oid1", "regex1", True), ("oid2", "regex2", False), ]]), ), SectionName("section1"): SNMPPluginStoreItem( [ BackendSNMPTree(base=".1.2.3", oids=[ BackendOIDSpec("4.5", "string", False), BackendOIDSpec("6.7.8", "string", False) ]) ], SNMPDetectSpec([[ ("oid3", "regex3", True), ("oid4", "regex4", False), ]]), ), })
def fetcher_fixture(self, file_cache): SNMPFetcher.snmp_plugin_store = SNMPPluginStore({ SectionName("pim"): SNMPPluginStoreItem( trees=[ BackendSNMPTree(base=".1.1.1", oids=[ BackendOIDSpec("1.2", "string", False), BackendOIDSpec("3.4", "string", False) ]) ], detect_spec=SNMPDetectSpec([[("1.2.3.4", "pim device", True)]]), ), SectionName("pam"): SNMPPluginStoreItem( trees=[ BackendSNMPTree( base=".1.2.3", oids=[ BackendOIDSpec("4.5", "string", False), BackendOIDSpec("6.7", "string", False), BackendOIDSpec("8.9", "string", False) ], ), ], detect_spec=SNMPDetectSpec([[("1.2.3.4", "pam device", True)]]), ), SectionName("pum"): SNMPPluginStoreItem( trees=[ BackendSNMPTree(base=".2.2.2", oids=[BackendOIDSpec("2.2", "string", False)]), BackendSNMPTree(base=".3.3.3", oids=[BackendOIDSpec("2.2", "string", False)]), ], detect_spec=SNMPDetectSpec([[]]), ), }) return SNMPFetcher( file_cache, disabled_sections=set(), configured_snmp_sections=set(), inventory_snmp_sections=set(), on_error="raise", missing_sys_description=False, use_snmpwalk_cache=False, do_status_data_inventory=False, snmp_config=SNMPHostConfig( is_ipv6_primary=False, hostname="bob", ipaddress="1.2.3.4", credentials="public", port=42, is_bulkwalk_host=False, is_snmpv2or3_without_bulkwalk_host=False, bulk_walk_size_of=0, timing={}, oid_range_limits=[], snmpv3_contexts=[], character_encoding=None, is_usewalk_host=False, snmp_backend=SNMPBackend.classic, ), )
def deserialize(cls, serialized: Mapping[str, Any]) -> "SNMPPluginStoreItem": return cls( [BackendSNMPTree.from_json(tree) for tree in serialized["trees"]], SNMPDetectSpec.from_json(serialized["detect_spec"]), serialized["inventory"], )
def _make_snmp_plugin_store() -> SNMPPluginStore: return SNMPPluginStore({ s.name: SNMPPluginStoreItem( [BackendSNMPTree.from_frontend(base=t.base, oids=t.oids) for t in s.trees], SNMPDetectSpec(s.detect_spec)) for s in agent_based_register.iter_all_snmp_sections() })
def snmp_plugin_fixture(self) -> None: SNMPFetcher.plugin_store = SNMPPluginStore({ SectionName("pim"): SNMPPluginStoreItem( trees=[ BackendSNMPTree( base=".1.1.1", oids=[ BackendOIDSpec("1.2", "string", False), BackendOIDSpec("3.4", "string", False), ], ) ], detect_spec=SNMPDetectSpec([[("1.2.3.4", "pim device", True)] ]), inventory=False, ), SectionName("pam"): SNMPPluginStoreItem( trees=[ BackendSNMPTree( base=".1.2.3", oids=[ BackendOIDSpec("4.5", "string", False), BackendOIDSpec("6.7", "string", False), BackendOIDSpec("8.9", "string", False), ], ), ], detect_spec=SNMPDetectSpec([[("1.2.3.4", "pam device", True)] ]), inventory=False, ), SectionName("pum"): SNMPPluginStoreItem( trees=[ BackendSNMPTree( base=".2.2.2", oids=[BackendOIDSpec("2.2", "string", False)]), BackendSNMPTree( base=".3.3.3", oids=[BackendOIDSpec("2.2", "string", False)]), ], detect_spec=SNMPDetectSpec([[]]), inventory=False, ), })
def deserialize(cls, serialized: Dict[str, Any]) -> "SNMPPluginStoreItem": try: return cls( [BackendSNMPTree.from_json(tree) for tree in serialized["trees"]], SNMPDetectSpec.from_json(serialized["detect_spec"]), serialized["inventory"], ) except (LookupError, TypeError, ValueError) as exc: raise ValueError(serialized) from exc
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=BackendSNMPTree( base=oid_start, oids=[BackendOIDSpec(oid_end, "string", False)]), backend=backend, ) assert table[0][0] == expected_response assert isinstance(table[0][0], str)
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"), oid_info=oid_info, backend=backend, ) assert table == [ [u''], [ u'\x00\x12yb\xf9@', ], ]
def test_snmptree_from_frontend(): base = "1.2" tree = BackendSNMPTree.from_frontend( base=base, oids=[ OIDSpecTuple('2', "string", False), OIDSpecTuple('2', "string", True), OIDSpecTuple('2', "binary", False), OIDSpecTuple(SpecialColumn.END, "string", False), ], ) assert tree.base == base assert tree.oids == [ BackendOIDSpec("2", "string", False), BackendOIDSpec("2", "string", True), BackendOIDSpec("2", "binary", False), BackendOIDSpec(SpecialColumn.END, "string", False), ]
def test_get_simple_snmp_table_with_hex_str(backend: SNMPBackend) -> None: 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@", ], ]
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'], ]
def test_cache_keeps_stored_data(self) -> None: fetchoid = ".1.2.3" path = f"OID{fetchoid}" cache = MockWalkCache({path: [("23", b"43")]}) assert not cache cache.load( trees=[ BackendSNMPTree( base=".1.2", oids=[BackendOIDSpec("3", "string", True)], ), ], ) assert fetchoid in cache cache.save() assert path in cache.mock_stored_on_fs
def test_snmptree_from_frontend(): base = "1.2" tree = BackendSNMPTree.from_frontend( base=base, oids=[ # this function will vanish, still use it for a moment: _create_oid_entry('2'), _create_oid_entry(OIDCached('2')), _create_oid_entry(OIDBytes('2')), _create_oid_entry(SpecialColumn.END), ], ) assert tree.base == base assert tree.oids == [ BackendOIDSpec("2", "string", False), BackendOIDSpec("2", "string", True), BackendOIDSpec("2", "binary", False), BackendOIDSpec(SpecialColumn.END, "string", False), ]
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"), tree=oid_info, walk_cache={}, backend=backend, ) assert table == [ ["1", "lo", "24", "1"], ["2", "eth0", "6", "2"], ]
def test_get_simple_snmp_table_oid_bin(backend): oid_info = BackendSNMPTree( base=".1.3.6.1.2.1.2.2.1", # deprecated with checkmk version 2.0 oids=[ BackendOIDSpec("1", "string", False), BackendOIDSpec("2", "string", False), BackendOIDSpec("3", "string", False), BackendOIDSpec(SpecialColumn.BIN, "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'\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'], ]
def test_get_simple_snmp_table_oid_bin(backend): oid_info = BackendSNMPTree( base=".1.3.6.1.2.1.2.2.1", # deprecated with checkmk version 2.0 oids=[ BackendOIDSpec("1", "string", False), BackendOIDSpec("2", "string", False), BackendOIDSpec("3", "string", False), BackendOIDSpec(SpecialColumn.BIN, "string", False), ], ) table = snmp_table.get_snmp_table( section_name=SectionName("my_Section"), tree=oid_info, walk_cache={}, backend=backend, ) assert table == [ ["1", "lo", "24", "\x01\x03\x06\x01\x02\x01\x02\x02\x01\x01\x01"], ["2", "eth0", "6", "\x01\x03\x06\x01\x02\x01\x02\x02\x01\x01\x02"], ]
def test_get_simple_snmp_table_oid_string(backend): oid_info = BackendSNMPTree( base=".1.3.6.1.2.1.2.2.1", # deprecated with checkmk version 2.0 oids=[ BackendOIDSpec("1", "string", False), BackendOIDSpec("2", "string", False), BackendOIDSpec("3", "string", False), BackendOIDSpec(SpecialColumn.STRING, "string", False), ], ) table = snmp_table.get_snmp_table( section_name=SectionName("my_Section"), tree=oid_info, walk_cache={}, 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'], ]
from typing import List, MutableMapping, Tuple import pytest from cmk.utils.exceptions import MKSNMPError from cmk.utils.type_defs import SectionName import cmk.snmplib.snmp_modes as snmp_modes import cmk.snmplib.snmp_table as snmp_table from cmk.snmplib.type_defs import BackendOIDSpec, BackendSNMPTree, SNMPBackendEnum, SpecialColumn INFO_TREE = BackendSNMPTree( base=".1.3.6.1.2.1.1", oids=[ BackendOIDSpec("1.0", "string", False), BackendOIDSpec("2.0", "string", False), BackendOIDSpec("5.0", "string", False), ], ) # Found no other way to achieve this # https://github.com/pytest-dev/pytest/issues/363 @pytest.fixture(scope="module") def monkeymodule(request): from _pytest.monkeypatch import ( MonkeyPatch, # type: ignore[import] # pylint: disable=import-outside-toplevel ) mpatch = MonkeyPatch() yield mpatch
) assert tree.base == base assert tree.oids == [ BackendOIDSpec("2", "string", False), BackendOIDSpec("2", "string", True), BackendOIDSpec("2", "binary", False), BackendOIDSpec(SpecialColumn.END, "string", False), ] @pytest.mark.parametrize("tree", [ BackendSNMPTree( base=".1.2.3", oids=[ BackendOIDSpec("4.5.6", "string", False), BackendOIDSpec("7.8.9", "string", False), ], ), BackendSNMPTree( base=".1.2.3", oids=[ BackendOIDSpec("4.5.6", "binary", False), BackendOIDSpec("7.8.9", "string", True), BackendOIDSpec(SpecialColumn.END, "string", False), ], ), ]) def test_serialize_snmptree(tree): assert tree.from_json(json.loads(json.dumps(tree.to_json()))) == tree
def walk(self, oid, check_plugin_name=None, table_base_oid=None, context_name=None): return [("%s.%s" % (oid, r), b"C0FEFE") for r in (1, 2, 3)] @pytest.mark.parametrize( "snmp_info, expected_values", [ ( BackendSNMPTree( base=".1.3.6.1.4.1.13595.2.2.3.1", oids=[ BackendOIDSpec(SpecialColumn.END, "string", False), BackendOIDSpec("16", "binary", False), ], ), [ ["1", [67, 48, 70, 69, 70, 69]], ["2", [67, 48, 70, 69, 70, 69]], ["3", [67, 48, 70, 69, 70, 69]], ], ), ], ) def test_get_snmp_table(monkeypatch, snmp_info, expected_values): def get_all_snmp_tables(info): backend = SNMPTestBackend(SNMPConfig, logger) if not isinstance(info, list):