def _snmp_scan_cache_description( binary_host: bool, *, backend: ABCSNMPBackend, ) -> None: if not binary_host: for oid, name in [ (OID_SYS_DESCR, "system description"), (OID_SYS_OBJ, "system object"), ]: value = snmp_modes.get_single_oid( oid, backend=backend, ) if value is None: raise MKSNMPError( "Cannot fetch %s OID %s. Please check your SNMP " "configuration. Possible reason might be: Wrong credentials, " "wrong SNMP version, Firewall rules, etc." % (name, oid),) else: # Fake OID values to prevent issues with a lot of scan functions console.vverbose( " Skipping system description OID " '(Set %s and %s to "")\n', OID_SYS_DESCR, OID_SYS_OBJ, ) snmp_cache.set_single_oid_cache(OID_SYS_DESCR, "") snmp_cache.set_single_oid_cache(OID_SYS_OBJ, "")
def test_get_single_oid_not_resolvable(backend): if backend.config.is_usewalk_host: pytest.skip("Not relevant") backend.config = backend.config.update(ipaddress="bla.local") assert snmp_modes.get_single_oid(".1.3.6.1.2.1.1.7.0", backend=backend) is None
def test_get_single_oid_wrong_credentials(backend): if backend.config.is_usewalk_host: pytest.skip("Not relevant") backend.config = backend.config.update(credentials="dingdong") result = snmp_modes.get_single_oid(".1.3.6.1.2.1.1.1.0", backend=backend) assert result is None
def test_get_single_oid_cache(backend): oid = ".1.3.6.1.2.1.1.1.0" expected_value = "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686" assert snmp_modes.get_single_oid(oid, backend=backend) == expected_value assert oid in snmp_cache.single_oid_cache() cached_oid = snmp_cache.single_oid_cache()[oid] assert cached_oid == expected_value assert isinstance(cached_oid, str)
def _prefetch_description_object(*, backend: SNMPBackend) -> None: for oid, name in ( (OID_SYS_DESCR, "system description"), (OID_SYS_OBJ, "system object"), ): if snmp_modes.get_single_oid(oid, backend=backend) is None: raise MKSNMPError( "Cannot fetch %s OID %s. Please check your SNMP " "configuration. Possible reason might be: Wrong credentials, " "wrong SNMP version, Firewall rules, etc." % (name, oid), )
def test_get_single_oid_ipv6(backend): if backend.config.is_usewalk_host: pytest.skip("Not relevant") backend.config = backend.config.update( is_ipv6_primary=True, ipaddress="::1", ) result = snmp_modes.get_single_oid(".1.3.6.1.2.1.1.1.0", backend=backend) assert result == "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686"
def test_get_single_oid_snmpv3(backend): if backend.config.is_usewalk_host: pytest.skip("Not relevant") backend.config = backend.config.update(credentials=( 'authNoPriv', 'md5', 'authOnlyUser', 'authOnlyUser', )) result = snmp_modes.get_single_oid(".1.3.6.1.2.1.1.1.0", backend=backend) assert result == "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686"
def _evaluate_snmp_detection_atom(atom: SNMPDetectAtom, cp_name: str, do_snmp_scan: bool, *, backend: ABCSNMPBackend) -> bool: oid, pattern, flag = atom value = snmp_modes.get_single_oid( oid, cp_name, do_snmp_scan=do_snmp_scan, backend=backend, ) if value is None: # check for "not_exists" return pattern == '.*' and not flag # ignore case! return bool(regex(pattern, re.IGNORECASE).fullmatch(value)) is flag
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)
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)
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)
def test_get_single_oid_snmpv3_higher_encryption(backend): if backend.config.is_usewalk_host: pytest.skip("Not relevant") backend.config = backend.config.update( credentials=( "authPriv", "SHA-512", "authPrivUser", "A_long_authKey", "DES", "A_long_privKey", ), ) # TODO: Reorganize snmp tests: at the moment we create *all* snmpsimd processes at setup # but with different ports. Those different processes are then used in test_snmp_modes.py and # backend_snmp.py... backend.port = 1341 result = snmp_modes.get_single_oid(".1.3.6.1.2.1.1.1.0", backend=backend) assert result == "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686"
def test_get_single_oid(backend): result = snmp_modes.get_single_oid(".1.3.6.1.2.1.1.1.0", backend=backend) assert result == "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016 i686" assert isinstance(result, str)
def test_get_single_oid_next(backend): assert snmp_modes.get_single_oid( ".1.3.6.1.2.1.1.9.1.*", backend=backend) == ".1.3.6.1.6.3.10.3.1.1"
def test_get_single_non_prefixed_oid(backend): with pytest.raises(MKGeneralException, match="does not begin with"): snmp_modes.get_single_oid("1.3.6.1.2.1.1.1.0", backend=backend)
def test_get_single_oid_not_existing(backend): assert snmp_modes.get_single_oid(".1.3.100.200.300.400", backend=backend) is None
def test_get_single_oid_value(backend): assert ( snmp_modes.get_single_oid(".1.3.6.1.2.1.1.9.1.2.1", backend=backend) == ".1.3.6.1.6.3.10.3.1.1" )