Exemplo n.º 1
0
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, "")
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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), )
Exemplo n.º 6
0
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"
Exemplo n.º 7
0
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"
Exemplo n.º 8
0
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
Exemplo n.º 9
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)
Exemplo n.º 10
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)
Exemplo n.º 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(
        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)
Exemplo n.º 12
0
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"
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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"
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
def test_get_single_oid_not_existing(backend):
    assert snmp_modes.get_single_oid(".1.3.100.200.300.400", backend=backend) is None
Exemplo n.º 17
0
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"
    )