Module: check_mk
Branch: master
Commit: 53baf34b5b56bfeb010412f4c5e28fa9676118b2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=53baf34b5b56bf…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Feb 21 19:21:37 2019 +0100
Moved SNMP port attribute to SNMPHostConfig
Change-Id: Ie2cf927a99f680c9f454f24ac6e2ce94a67704ea
---
cmk_base/automations/check_mk.py | 1 +
cmk_base/classic_snmp.py | 6 ++----
cmk_base/config.py | 3 ++-
cmk_base/data_sources/snmp.py | 10 ++++------
cmk_base/snmp.py | 1 +
cmk_base/snmp_utils.py | 1 +
tests/integration/cmk_base/snmp/test_snmp.py | 6 ++++--
tests/unit/cmk_base/snmp/test_classic_snmp.py | 17 ++++++++++++++---
8 files changed, 29 insertions(+), 16 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index dd56735..beec622 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -1233,6 +1233,7 @@ class AutomationDiagHost(Automation):
hostname=hostname,
ipaddress=ipaddress,
credentials=config.snmp_credentials_of(hostname),
+ port=config.snmp_port_of(hostname),
)
data = snmp.get_snmp_table(
host_config,
diff --git a/cmk_base/classic_snmp.py b/cmk_base/classic_snmp.py
index d06ee45..78b07fe 100644
--- a/cmk_base/classic_snmp.py
+++ b/cmk_base/classic_snmp.py
@@ -184,11 +184,9 @@ def get(host_config, oid, context_name=None):
def _snmp_port_spec(host_config):
- port = config.snmp_port_of(host_config.hostname)
- if port is None:
+ if host_config.port == 161:
return ""
-
- return ":%d" % port
+ return ":%d" % host_config.port
def _snmp_proto_spec(host_config):
diff --git a/cmk_base/config.py b/cmk_base/config.py
index b76b69c..f9f6480 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -1041,9 +1041,10 @@ def oid_range_limits_of(hostname):
def snmp_port_of(hostname):
+ # type: (str) -> int
ports = host_extra_conf(hostname, snmp_ports)
if len(ports) == 0:
- return None # do not specify a port, use default
+ return 161
return ports[0]
diff --git a/cmk_base/data_sources/snmp.py b/cmk_base/data_sources/snmp.py
index e9fddfb..2bd6f9c 100644
--- a/cmk_base/data_sources/snmp.py
+++ b/cmk_base/data_sources/snmp.py
@@ -64,6 +64,7 @@ class SNMPDataSource(DataSource):
self._ignore_check_interval = False
self._fetched_check_plugin_names = []
self._credentials = config.snmp_credentials_of(self._hostname)
+ self._port = config.snmp_port_of(self._hostname)
def id(self):
return "snmp"
@@ -79,6 +80,7 @@ class SNMPDataSource(DataSource):
hostname=self._hostname,
ipaddress=self._ipaddress,
credentials=self._credentials,
+ port=self._port,
)
def describe(self):
@@ -100,12 +102,8 @@ class SNMPDataSource(DataSource):
else:
bulk = "no"
- portinfo = config.snmp_port_of(self._hostname)
- if portinfo is None:
- portinfo = 'default'
-
- return "%s (%s, Bulk walk: %s, Port: %s, Inline: %s)" % \
- (self.title(), credentials_text, bulk, portinfo, inline)
+ return "%s (%s, Bulk walk: %s, Port: %d, Inline: %s)" % \
+ (self.title(), credentials_text, bulk, self._port, inline)
def _from_cache_file(self, raw_data):
return ast.literal_eval(raw_data)
diff --git a/cmk_base/snmp.py b/cmk_base/snmp.py
index f9decdd..b2fadf3 100644
--- a/cmk_base/snmp.py
+++ b/cmk_base/snmp.py
@@ -147,6 +147,7 @@ def create_snmp_host_config(hostname):
hostname=hostname,
ipaddress=ip_lookup.lookup_ipv4_address(hostname),
credentials=config.snmp_credentials_of(hostname),
+ port=config.snmp_port_of(hostname),
)
diff --git a/cmk_base/snmp_utils.py b/cmk_base/snmp_utils.py
index 0f80127..7885152 100644
--- a/cmk_base/snmp_utils.py
+++ b/cmk_base/snmp_utils.py
@@ -75,4 +75,5 @@ SNMPHostConfig = NamedTuple("SNMPHostConfig", [
("hostname", str),
("ipaddress", str),
("credentials", SNMPCredentials),
+ ("port", int),
])
diff --git a/tests/integration/cmk_base/snmp/test_snmp.py
b/tests/integration/cmk_base/snmp/test_snmp.py
index 3ccadb9..81705a8 100644
--- a/tests/integration/cmk_base/snmp/test_snmp.py
+++ b/tests/integration/cmk_base/snmp/test_snmp.py
@@ -1,7 +1,6 @@
import subprocess
import logging
import os
-import copy
import pytest # type: ignore
from pathlib2 import Path
from testlib import cmk_path, wait_until
@@ -42,7 +41,6 @@ def snmpsim(site, request, tmp_path_factory, monkeymodule):
cmd = "%s/.venv/bin/snmpsimd.py" % cmk_path()
source_data_dir = Path(request.fspath.dirname) / "snmp_data"
- monkeymodule.setattr(config, "snmp_port_of", lambda h: 1337)
log.set_verbosity(2)
debug.enable()
@@ -75,6 +73,7 @@ def snmpsim(site, request, tmp_path_factory, monkeymodule):
ipaddress="127.0.0.1",
hostname="localhost",
credentials="public",
+ port=1337,
)
# Ensure that snmpsim is ready for clients before starting with the tests
@@ -141,6 +140,7 @@ def test_get_single_oid_ipv6(snmpsim, backend, monkeypatch):
ipaddress="::1",
hostname="localhost",
credentials="public",
+ port=1337,
)
monkeypatch.setattr(config, "is_ipv6_primary", lambda h: True)
result = snmp.get_single_oid(host_config, ".1.3.6.1.2.1.1.1.0")
@@ -155,6 +155,7 @@ def test_get_single_oid_snmpv3(snmpsim, backend, monkeypatch):
ipaddress="127.0.0.1",
hostname="localhost",
credentials=('authNoPriv', 'md5', 'authOnlyUser',
'authOnlyUser'),
+ port=1337,
)
result = snmp.get_single_oid(host_config, ".1.3.6.1.2.1.1.1.0")
assert result == "Linux zeus 4.8.6.5-smp #2 SMP Sun Nov 13 14:58:11 CDT 2016
i686"
@@ -253,6 +254,7 @@ def test_get_single_oid_not_resolvable(snmpsim, backend):
ipaddress="bla.local",
hostname="localhost",
credentials="public",
+ port=1337,
)
assert snmp.get_single_oid(host_config, ".1.3.6.1.2.1.1.7.0") is None
diff --git a/tests/unit/cmk_base/snmp/test_classic_snmp.py
b/tests/unit/cmk_base/snmp/test_classic_snmp.py
index 4fda7fd..870e1a8 100644
--- a/tests/unit/cmk_base/snmp/test_classic_snmp.py
+++ b/tests/unit/cmk_base/snmp/test_classic_snmp.py
@@ -11,15 +11,21 @@ def host_config():
hostname="localhost",
ipaddress="127.0.0.1",
credentials="public",
+ port=161,
)
@pytest.mark.parametrize("port,expected", [
- (None, ""),
+ (161, ""),
(1234, ":1234"),
])
-def test_snmp_port_spec(monkeypatch, port, expected, host_config):
- monkeypatch.setattr(config, "snmp_port_of", lambda h: port)
+def test_snmp_port_spec(port, expected):
+ host_config = snmp_utils.SNMPHostConfig(
+ hostname="localhost",
+ ipaddress="127.0.0.1",
+ credentials="public",
+ port=port,
+ )
assert classic_snmp._snmp_port_spec(host_config) == expected
@@ -48,6 +54,7 @@ SNMPSettings = collections.namedtuple("SNMPSettings", [
hostname="localhost",
ipaddress="127.0.0.1",
credentials="public",
+ port=161,
),
is_bulkwalk_host=True,
bulk_walk_size_of=10,
@@ -66,6 +73,7 @@ SNMPSettings = collections.namedtuple("SNMPSettings", [
hostname="lohost",
ipaddress="127.0.0.1",
credentials="public",
+ port=161,
),
is_bulkwalk_host=False,
bulk_walk_size_of=5,
@@ -84,6 +92,7 @@ SNMPSettings = collections.namedtuple("SNMPSettings", [
hostname="lohost",
ipaddress="public",
credentials=("authNoPriv", "abc", "md5",
"abc"),
+ port=161,
),
is_bulkwalk_host=False,
bulk_walk_size_of=5,
@@ -102,6 +111,7 @@ SNMPSettings = collections.namedtuple("SNMPSettings", [
hostname="lohost",
ipaddress="public",
credentials=('noAuthNoPriv', 'secname'),
+ port=161,
),
is_bulkwalk_host=False,
bulk_walk_size_of=5,
@@ -120,6 +130,7 @@ SNMPSettings = collections.namedtuple("SNMPSettings", [
hostname="lohost",
ipaddress="127.0.0.1",
credentials=('authPriv', 'md5', 'secname',
'auhtpassword', 'DES', 'privacybla'),
+ port=161,
),
is_bulkwalk_host=False,
bulk_walk_size_of=5,