Module: check_mk
Branch: master
Commit: 0aa7adf337ab1025c6a1d6b39c0242e5fe8d316a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0aa7adf337ab10…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Feb 21 18:57:47 2019 +0100
Inline SNMP now also uses host_config data structures
Made some classic SNMP methods also use host_config instead of
single host attributes.
Change-Id: I80f71743447c5c51f9eda63c0a5c08447c3aeeed
---
cmk_base/classic_snmp.py | 16 ++++++-------
cmk_base/snmp.py | 34 +++++++--------------------
cmk_base/snmp_scan.py | 4 ++--
tests/unit/cmk_base/snmp/test_classic_snmp.py | 17 ++++++++++----
4 files changed, 32 insertions(+), 39 deletions(-)
diff --git a/cmk_base/classic_snmp.py b/cmk_base/classic_snmp.py
index 277e08f..d06ee45 100644
--- a/cmk_base/classic_snmp.py
+++ b/cmk_base/classic_snmp.py
@@ -51,8 +51,8 @@ from cmk_base.exceptions import MKSNMPError
def walk(host_config, oid, hex_plain=False, context_name=None):
- protospec = _snmp_proto_spec(host_config.hostname)
- portspec = _snmp_port_spec(host_config.hostname)
+ protospec = _snmp_proto_spec(host_config)
+ portspec = _snmp_port_spec(host_config)
command = _snmp_walk_command(host_config, context_name)
command += [
"-OQ", "-OU", "-On", "-Ot",
@@ -144,8 +144,8 @@ def get(host_config, oid, context_name=None):
oid_prefix = oid
commandtype = "get"
- protospec = _snmp_proto_spec(host_config.hostname)
- portspec = _snmp_port_spec(host_config.hostname)
+ protospec = _snmp_proto_spec(host_config)
+ portspec = _snmp_port_spec(host_config)
command = _snmp_base_command(commandtype, host_config, context_name) + \
[ "-On", "-OQ", "-Oe", "-Ot",
"%s%s%s" % (protospec, host_config.ipaddress, portspec),
@@ -183,16 +183,16 @@ def get(host_config, oid, context_name=None):
return value
-def _snmp_port_spec(hostname):
- port = config.snmp_port_of(hostname)
+def _snmp_port_spec(host_config):
+ port = config.snmp_port_of(host_config.hostname)
if port is None:
return ""
return ":%d" % port
-def _snmp_proto_spec(hostname):
- if config.is_ipv6_primary(hostname):
+def _snmp_proto_spec(host_config):
+ if config.is_ipv6_primary(host_config.hostname):
return "udp6:"
return ""
diff --git a/cmk_base/snmp.py b/cmk_base/snmp.py
index 891ee7c..e7128b6 100644
--- a/cmk_base/snmp.py
+++ b/cmk_base/snmp.py
@@ -94,15 +94,15 @@ def write_single_oid_cache(host_config):
store.save_data_to_file(cache_path, _g_single_oid_cache, pretty=False)
-def set_single_oid_cache(hostname, oid, value):
+def set_single_oid_cache(host_config, oid, value):
_g_single_oid_cache[oid] = value
-def _is_in_single_oid_cache(hostname, oid):
+def _is_in_single_oid_cache(host_config, oid):
return oid in _g_single_oid_cache
-def _get_oid_from_single_oid_cache(hostname, oid):
+def _get_oid_from_single_oid_cache(host_config, oid):
return _g_single_oid_cache.get(oid)
@@ -258,9 +258,9 @@ def get_single_oid(host_config, oid, check_plugin_name=None,
do_snmp_scan=True):
oid = '.' + oid
# TODO: Use generic cache mechanism
- if _is_in_single_oid_cache(host_config.hostname, oid):
+ if _is_in_single_oid_cache(host_config, oid):
console.vverbose(" Using cached OID %s: " % oid)
- value = _get_oid_from_single_oid_cache(host_config.hostname, oid)
+ value = _get_oid_from_single_oid_cache(host_config, oid)
console.vverbose("%s%s%s%s\n" % (tty.bold, tty.green, value,
tty.normal))
return value
@@ -287,12 +287,7 @@ def get_single_oid(host_config, oid, check_plugin_name=None,
do_snmp_scan=True):
for context_name in snmp_contexts:
try:
if config.is_inline_snmp_host(host_config.hostname):
- value = inline_snmp.get(
- host_config.hostname,
- oid,
- ipaddress=host_config.ipaddress,
- context_name=context_name,
- credentials=host_config.credentials)
+ value = inline_snmp.get(host_config, oid, context_name=context_name)
else:
value = classic_snmp.get(host_config, oid,
context_name=context_name)
@@ -308,18 +303,13 @@ def get_single_oid(host_config, oid, check_plugin_name=None,
do_snmp_scan=True):
else:
console.vverbose("failed.\n")
- set_single_oid_cache(host_config.hostname, oid, value)
+ set_single_oid_cache(host_config, oid, value)
return value
def walk_for_export(host_config, oid):
if config.is_inline_snmp_host(host_config.hostname):
- rows = inline_snmp.walk(
- host_config.hostname,
- None,
- oid,
- ipaddress=host_config.ipaddress,
- credentials=host_config.credentials)
+ rows = inline_snmp.walk(host_config, None, oid)
return inline_snmp.convert_rows_for_stored_walk(rows)
return classic_snmp.walk(host_config, oid, hex_plain=True)
@@ -404,13 +394,7 @@ def _perform_snmpwalk(host_config, check_plugin_name, base_oid,
fetchoid):
for context_name in snmp_contexts:
if config.is_inline_snmp_host(host_config.hostname):
rows = inline_snmp.walk(
- host_config.hostname,
- check_plugin_name,
- fetchoid,
- base_oid,
- context_name=context_name,
- ipaddress=host_config.ipaddress,
- credentials=host_config.credentials)
+ host_config, check_plugin_name, fetchoid, base_oid,
context_name=context_name)
else:
rows = classic_snmp.walk(host_config, fetchoid, context_name=context_name)
diff --git a/cmk_base/snmp_scan.py b/cmk_base/snmp_scan.py
index 048c25e..6a2495e 100644
--- a/cmk_base/snmp_scan.py
+++ b/cmk_base/snmp_scan.py
@@ -88,8 +88,8 @@ def _snmp_scan(host_config,
# Fake OID values to prevent issues with a lot of scan functions
console.vverbose(" Skipping system description OID "
"(Set .1.3.6.1.2.1.1.1.0 and .1.3.6.1.2.1.1.2.0 to
\"\")\n")
- snmp.set_single_oid_cache(host_config.hostname, ".1.3.6.1.2.1.1.1.0",
"")
- snmp.set_single_oid_cache(host_config.hostname, ".1.3.6.1.2.1.1.2.0",
"")
+ snmp.set_single_oid_cache(host_config, ".1.3.6.1.2.1.1.1.0",
"")
+ snmp.set_single_oid_cache(host_config, ".1.3.6.1.2.1.1.2.0",
"")
found_check_plugin_names = []
if for_inv:
diff --git a/tests/unit/cmk_base/snmp/test_classic_snmp.py
b/tests/unit/cmk_base/snmp/test_classic_snmp.py
index 6068c13..4fda7fd 100644
--- a/tests/unit/cmk_base/snmp/test_classic_snmp.py
+++ b/tests/unit/cmk_base/snmp/test_classic_snmp.py
@@ -5,22 +5,31 @@ import cmk_base.classic_snmp as classic_snmp
import cmk_base.snmp_utils as snmp_utils
+(a)pytest.fixture
+def host_config():
+ return snmp_utils.SNMPHostConfig(
+ hostname="localhost",
+ ipaddress="127.0.0.1",
+ credentials="public",
+ )
+
+
@pytest.mark.parametrize("port,expected", [
(None, ""),
(1234, ":1234"),
])
-def test_snmp_port_spec(monkeypatch, port, expected):
+def test_snmp_port_spec(monkeypatch, port, expected, host_config):
monkeypatch.setattr(config, "snmp_port_of", lambda h: port)
- assert classic_snmp._snmp_port_spec("localhost") == expected
+ assert classic_snmp._snmp_port_spec(host_config) == expected
@pytest.mark.parametrize("is_ipv6,expected", [
(True, "udp6:"),
(False, ""),
])
-def test_snmp_proto_spec(monkeypatch, is_ipv6, expected):
+def test_snmp_proto_spec(monkeypatch, is_ipv6, expected, host_config):
monkeypatch.setattr(config, "is_ipv6_primary", lambda h: is_ipv6)
- assert classic_snmp._snmp_proto_spec("localhost") == expected
+ assert classic_snmp._snmp_proto_spec(host_config) == expected
SNMPSettings = collections.namedtuple("SNMPSettings", [