Module: check_mk
Branch: master
Commit: 65c7fb7835e8b5b2a4b2a7fc569dc3e74df2a83c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=65c7fb7835e8b5…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Jun 12 10:13:50 2018 +0200
Moved translate_piggyback_host where it belongs.
This was a clear case of feature envy, see:
https://refactoring.guru/smells/feature-envy
Furthermore, this decouples the import cycle between config and rulesets a
bit, which is a step in the right direction...
Change-Id: I9c53a870248cd63acd022857d3ef739ae73e612f
---
cmk_base/config.py | 15 ++++++++++++++-
cmk_base/data_sources/abstract.py | 3 +--
cmk_base/piggyback.py | 13 -------------
3 files changed, 15 insertions(+), 16 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 6c43e81..6984ad2 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -1455,7 +1455,20 @@ def decode_incoming_string(s, encoding="utf-8"):
return s.decode(fallback_agent_output_encoding)
-def get_piggyback_translations(hostname):
+def translate_piggyback_host(sourcehost, backedhost):
+ translation = _get_piggyback_translations(sourcehost)
+
+ # To make it possible to match umlauts we need to change the hostname
+ # to a unicode string which can then be matched with regexes etc.
+ # We assume the incoming name is correctly encoded in UTF-8
+ backedhost = decode_incoming_string(backedhost)
+
+ translated = cmk.translations.translate_hostname(translation, backedhost)
+
+ return translated.encode('utf-8') # change back to UTF-8 encoded string
+
+
+def _get_piggyback_translations(hostname):
"""Get a dict that specifies the actions to be done during the
hostname translation"""
rules = rulesets.host_extra_conf(hostname, piggyback_translation)
translations = {}
diff --git a/cmk_base/data_sources/abstract.py b/cmk_base/data_sources/abstract.py
index 7afea91..c002f33 100644
--- a/cmk_base/data_sources/abstract.py
+++ b/cmk_base/data_sources/abstract.py
@@ -42,7 +42,6 @@ import cmk_base.utils
import cmk_base.console as console
import cmk_base.config as config
import cmk_base.ip_lookup as ip_lookup
-import cmk_base.piggyback as piggyback
import cmk_base.checks as checks
import cmk_base.check_api as check_api
from cmk_base.exceptions import MKSkipCheck, MKAgentError, MKEmptyAgentData, MKSNMPError,
\
@@ -627,7 +626,7 @@ class CheckMKAgentDataSource(DataSource):
if not host:
host = None
else:
- host = piggyback.translate_piggyback_host(self._hostname, host)
+ host = config.translate_piggyback_host(self._hostname, host)
if host == self._hostname:
host = None # unpiggybacked "normal" host
diff --git a/cmk_base/piggyback.py b/cmk_base/piggyback.py
index 9309928..ecf6eef 100644
--- a/cmk_base/piggyback.py
+++ b/cmk_base/piggyback.py
@@ -151,19 +151,6 @@ def store_piggyback_raw_data(sourcehost, piggybacked_raw_data):
remove_source_status_file(sourcehost)
-def translate_piggyback_host(sourcehost, backedhost):
- translation = config.get_piggyback_translations(sourcehost)
-
- # To make it possible to match umlauts we need to change the hostname
- # to a unicode string which can then be matched with regexes etc.
- # We assume the incoming name is correctly encoded in UTF-8
- backedhost = config.decode_incoming_string(backedhost)
-
- translated = cmk.translations.translate_hostname(translation, backedhost)
-
- return translated.encode('utf-8') # change back to UTF-8 encoded string
-
-
def cleanup_piggyback_files():
"""This is a housekeeping job to clean up different old files from
the
piggyback directories.