Module: check_mk
Branch: master
Commit: ec1b31ab4f2877fd05eb65b4a3da4b460b402157
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ec1b31ab4f2877…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri May 25 17:46:42 2018 +0200
6174 FIX Fixed discovery for host with management board without IP addresses configured
The discovery was not working correctly for hosts that have the management board
protocol enabled but no explicit IP address set and no management board IP configured.
Change-Id: I993adc25839541715532c7b0c6034fbcfaa10f35
---
.werks/6174 | 12 ++++++++++
cmk_base/data_sources/abstract.py | 9 ++++++-
tests/unit/cmk_base/test_config.py | 15 ++++++++++++
tests/unit/cmk_base/test_data_sources_unit.py | 34 +++++++++++++++++++++++++++
4 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/.werks/6174 b/.werks/6174
new file mode 100644
index 0000000..8692e9f
--- /dev/null
+++ b/.werks/6174
@@ -0,0 +1,12 @@
+Title: Fixed discovery for host with management board without IP addresses configured
+Level: 1
+Component: core
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1527261139
+
+The discovery was not working correctly for hosts that have the management board
+protocol enabled but no explicit IP address set and no management board IP configured.
diff --git a/cmk_base/data_sources/abstract.py b/cmk_base/data_sources/abstract.py
index 9959eb0..61db24b 100644
--- a/cmk_base/data_sources/abstract.py
+++ b/cmk_base/data_sources/abstract.py
@@ -813,6 +813,9 @@ class ManagementBoardDataSource(DataSource):
def _management_board_ipaddress(self, hostname):
mgmt_ipaddress = config.management_address_of(hostname)
+ if mgmt_ipaddress is None:
+ return None
+
if not self._is_ipaddress(mgmt_ipaddress):
return ip_lookup.lookup_ip_address(mgmt_ipaddress)
else:
@@ -820,7 +823,11 @@ class ManagementBoardDataSource(DataSource):
# TODO: Why is it used only here?
- def _is_ipaddress(self, address):
+ @staticmethod
+ def _is_ipaddress(address):
+ if address is None:
+ return False
+
try:
socket.inet_pton(socket.AF_INET, address)
return True
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 6727434..36f106d 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -46,3 +46,18 @@ def test_is_ipv6_primary_host(monkeypatch, hostname, tags, result,
ruleset):
monkeypatch.setattr(config, "tags_of_host", lambda h: {hostname: tags}[h])
monkeypatch.setattr(config, "primary_address_family", ruleset)
assert config.is_ipv6_primary(hostname) == result
+
+
+(a)pytest.mark.parametrize("result,attrs"ttrs", [
+ ("127.0.1.1", {}),
+ ("127.0.1.1", {"management_address": ""}),
+ ("127.0.0.1", {"management_address": "127.0.0.1"}),
+ ("lolo", {"management_address": "lolo"}),
+])
+def test_management_address_of(monkeypatch, attrs, result):
+ # Host IP address is 127.0.1.1
+ monkeypatch.setitem(config.ipaddresses, "hostname", "127.0.1.1")
+
+ monkeypatch.setitem(config.host_attributes, "hostname", attrs)
+
+ assert config.management_address_of("hostname") == result
diff --git a/tests/unit/cmk_base/test_data_sources_unit.py
b/tests/unit/cmk_base/test_data_sources_unit.py
index 65503ef..cd63008 100644
--- a/tests/unit/cmk_base/test_data_sources_unit.py
+++ b/tests/unit/cmk_base/test_data_sources_unit.py
@@ -1,6 +1,10 @@
import pytest
+import cmk_base.data_sources.abstract
import cmk_base.data_sources.snmp
+import cmk_base.ip_lookup as ip_lookup
+import cmk_base.config as config
+import cmk_base.exceptions
def test_data_source_cache_default():
source = cmk_base.data_sources.snmp.SNMPDataSource("hostname",
"ipaddress")
@@ -34,3 +38,33 @@ def test_disable_data_source_cache_no_write(mocker):
disabled_checker = mocker.patch.object(source, "is_agent_cache_disabled")
assert source._write_cache_file("X") is None
disabled_checker.assert_called_once()
+
+
+def test_mgmt_board_data_source_is_ip_address():
+ _is_ipaddress =
cmk_base.data_sources.abstract.ManagementBoardDataSource._is_ipaddress
+ assert _is_ipaddress(None) == False
+ assert _is_ipaddress("localhost") == False
+ assert _is_ipaddress("abc 123") == False
+ assert _is_ipaddress("127.0.0.1") == True
+ assert _is_ipaddress("::1") == True
+ assert _is_ipaddress("fe80::807c:f8ff:fea9:9f12") == True
+
+
+(a)pytest.mark.parametrize("result,address,resolvable"able", [
+ (None, None, True),
+ ("127.0.0.1", "127.0.0.1", True),
+ ("127.0.1.1", "lolo", True),
+ (None, "lolo", False),
+])
+def test_mgmt_board_data_source_management_board_ipaddress(monkeypatch, result, address,
resolvable):
+ source =
cmk_base.data_sources.snmp.SNMPManagementBoardDataSource("hostname",
"ipaddress")
+
+ if resolvable:
+ monkeypatch.setattr(ip_lookup, "lookup_ip_address", lambda h:
"127.0.1.1")
+ else:
+ def raise_exc(h):
+ raise cmk_base.exceptions.MKIPAddressLookupError("Failed to...")
+ monkeypatch.setattr(ip_lookup, "lookup_ip_address", raise_exc)
+
+ monkeypatch.setattr(config, "management_address_of", lambda h: address)
+ assert source._management_board_ipaddress("hostname") == result