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.
Module: check_mk
Branch: master
Commit: fd55a93615e1d95783edbcc9f55a5bc66d1be258
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fd55a93615e1d9…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Jun 11 10:59:53 2018 +0200
Fix dependencies of core-related modules, part 8.
Removed code duplication by moving the core factory to a separate module.
This brings the number of cycles down to 8 (from 11).
Change-Id: Idd235e40fd1025f7180478410a2ad50ad9b1516f
---
cmk_base/automations/check_mk.py | 37 ++-------------------------
cmk_base/core_factory.py | 54 ++++++++++++++++++++++++++++++++++++++++
cmk_base/modes/check_mk.py | 40 +++++------------------------
3 files changed, 62 insertions(+), 69 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 74f8f9f..6d79c74 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -46,6 +46,7 @@ from cmk_base.automations import automations, Automation, MKAutomationError
import cmk_base.check_utils
import cmk_base.autochecks
import cmk_base.nagios_utils
+from cmk_base.core_factory import create_core
class DiscoveryAutomation(Automation):
@@ -721,40 +722,6 @@ class AutomationRestart(Automation):
else:
return "restart"
- # TODO: _cmc_file, _create_config_hook, _precompile_hook, _create_core are copy-n-pasted from cmk_base.modes.check_mk.
- @staticmethod
- def _cmc_file(options):
- return options["cmc-file"] if options and "cmc-file" in options else "config"
-
-
- @staticmethod
- def _create_config_hook(options):
- from cmk_base.config import monitoring_core
- if monitoring_core == "cmc":
- from cmk_base.cee.core_cmc import create_config_hook as cch
- create_config_hook = lambda: cch(AutomationRestart._cmc_file(options))
- else:
- from cmk_base.core_nagios import create_config_hook
- return create_config_hook
-
-
- @staticmethod
- def _precompile_hook():
- from cmk_base.config import monitoring_core
- if monitoring_core == "cmc":
- from cmk_base.cee.core_cmc import precompile_hook
- else:
- from cmk_base.core_nagios import precompile_hook
- return precompile_hook
-
-
- # TODO: Change this naive dict representation of a core object into a real class!
- @staticmethod
- def _create_core(options=None):
- return {"create_config": AutomationRestart._create_config_hook(options),
- "precompile": AutomationRestart._precompile_hook()}
-
-
# TODO: Cleanup duplicate code with cmk_base.core.do_restart()
def execute(self, args):
# make sure, Nagios does not inherit any open
@@ -796,7 +763,7 @@ class AutomationRestart(Automation):
else:
backup_path = None
- core = AutomationRestart._create_core()
+ core = create_core()
try:
configuration_warnings = core_config.create_core_config(core)
diff --git a/cmk_base/core_factory.py b/cmk_base/core_factory.py
new file mode 100644
index 0000000..22afe7d
--- /dev/null
+++ b/cmk_base/core_factory.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+from cmk_base.config import monitoring_core
+
+
+# TODO: The "config" literal should really be centralized somehow.
+def _cmc_file(options):
+ return options["cmc-file"] if options and "cmc-file" in options else "config"
+
+
+def _create_config_hook(options):
+ if monitoring_core == "cmc":
+ from cmk_base.cee.core_cmc import create_config_hook as cch
+ create_config_hook = lambda: cch(_cmc_file(options))
+ else:
+ from cmk_base.core_nagios import create_config_hook
+ return create_config_hook
+
+
+def _precompile_hook():
+ if monitoring_core == "cmc":
+ from cmk_base.cee.core_cmc import precompile_hook
+ else:
+ from cmk_base.core_nagios import precompile_hook
+ return precompile_hook
+
+
+# TODO: Change this naive dict representation of a core object into a real class!
+def create_core(options=None):
+ return {"create_config": _create_config_hook(options), "precompile": _precompile_hook()}
diff --git a/cmk_base/modes/check_mk.py b/cmk_base/modes/check_mk.py
index 6f191c8..695796b 100644
--- a/cmk_base/modes/check_mk.py
+++ b/cmk_base/modes/check_mk.py
@@ -40,6 +40,7 @@ from cmk_base.exceptions import MKAgentError
from cmk_base.modes import modes, Mode, Option, keepalive_option
import cmk_base.check_utils
+from cmk_base.core_factory import create_core
# TODO: Investigate all modes and try to find out whether or not we can
# set needs_checks=False for them. This would save a lot of IO/time for
@@ -1008,38 +1009,9 @@ modes.register(Mode(
))
-# TODO: The "config" literal should really be centralized somehow.
-def _cmc_file(options):
- return options["cmc-file"] if options and "cmc-file" in options else "config"
-
-
-def _create_config_hook(options):
- from cmk_base.config import monitoring_core
- if monitoring_core == "cmc":
- from cmk_base.cee.core_cmc import create_config_hook as cch
- create_config_hook = lambda: cch(_cmc_file(options))
- else:
- from cmk_base.core_nagios import create_config_hook
- return create_config_hook
-
-
-def _precompile_hook():
- from cmk_base.config import monitoring_core
- if monitoring_core == "cmc":
- from cmk_base.cee.core_cmc import precompile_hook
- else:
- from cmk_base.core_nagios import precompile_hook
- return precompile_hook
-
-
-# TODO: Change this naive dict representation of a core object into a real class!
-def _create_core(options=None):
- return {"create_config": _create_config_hook(options), "precompile": _precompile_hook()}
-
-
def mode_update_no_precompile(options):
from cmk_base.core_config import do_update
- do_update(_create_core(options), with_precompile=False)
+ do_update(create_core(options), with_precompile=False)
modes.register(Mode(
long_option="update-no-precompile",
@@ -1096,7 +1068,7 @@ modes.register(Mode(
def mode_update(options):
from cmk_base.core_config import do_update
- do_update(_create_core(options), with_precompile=True)
+ do_update(create_core(options), with_precompile=True)
modes.register(Mode(
long_option="update",
@@ -1135,7 +1107,7 @@ modes.register(Mode(
def mode_restart():
import cmk_base.core
- cmk_base.core.do_restart(_create_core())
+ cmk_base.core.do_restart(create_core())
modes.register(Mode(
long_option="restart",
@@ -1157,7 +1129,7 @@ modes.register(Mode(
def mode_reload():
import cmk_base.core
- cmk_base.core.do_reload(_create_core())
+ cmk_base.core.do_reload(create_core())
modes.register(Mode(
long_option="reload",
@@ -1409,7 +1381,7 @@ modes.register(Mode(
def mode_discover_marked_hosts():
import cmk_base.discovery as discovery
- discovery.discover_marked_hosts(_create_core())
+ discovery.discover_marked_hosts(create_core())
modes.register(Mode(
long_option="discover-marked-hosts",