Module: check_mk
Branch: master
Commit: 51169a3d66a18418f8b76f804dbaa4086bb15d70
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=51169a3d66a184…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Jun 7 11:16:55 2018 +0200
Fix dependencies of core-related modules, part 5.
The hook for config generation is now passed down the call hierarchy. This
removes one part of the wrong dependency direction, the other part
(precompilation hook) will follow later.
Note that we introduce a tiny bit of duplication in AutomationRestart, but
this is already a copy-n-paste hell, so this should be fixed later, anyway.
The "threading through" part is a direct consequence of the current non-OO
design and will vanish when we have an object representing the configured
core (Nagios or CMC).
Change-Id: I1230efa6c05c988ea6882bc2192b2d48832d61a8
---
cmk_base/automations/check_mk.py | 7 ++++++-
cmk_base/core.py | 8 ++++----
cmk_base/core_config.py | 20 ++++++--------------
cmk_base/core_nagios.py | 2 +-
cmk_base/discovery.py | 6 +++---
cmk_base/modes/check_mk.py | 22 ++++++++++++++++------
6 files changed, 36 insertions(+), 29 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 382a1c8..86b4a2c 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -764,7 +764,12 @@ class AutomationRestart(Automation):
backup_path = None
try:
- configuration_warnings = core_config.create_core_config()
+ if config.monitoring_core == "cmc":
+ from cmk_base.cee.core_cmc import create_config_hook as cch
+ create_config_hook = lambda: cch("config")
+ else:
+ from cmk_base.core_nagios import create_config_hook
+ configuration_warnings =
core_config.create_core_config(create_config_hook)
try:
import cmk_base.cee.agent_bakery
diff --git a/cmk_base/core.py b/cmk_base/core.py
index 1e8c225..d719857 100644
--- a/cmk_base/core.py
+++ b/cmk_base/core.py
@@ -65,12 +65,12 @@ _restart_lock_fd = None
# | Invoke actions affecting the core like reload/restart |
# '----------------------------------------------------------------------'
-def do_reload():
- do_restart(True)
+def do_reload(create_config_hook):
+ do_restart(create_config_hook, only_reload=True)
# TODO: Cleanup duplicate code with automation_restart()
-def do_restart(only_reload = False):
+def do_restart(create_config_hook, only_reload=False):
try:
backup_path = None
@@ -88,7 +88,7 @@ def do_restart(only_reload = False):
backup_path = None
try:
- core_config.do_create_config(with_agents=True, cmc_file="config")
+ core_config.do_create_config(create_config_hook, with_agents=True)
except Exception, e:
# TODO: Replace by MKBailOut()/MKTerminate()?
console.error("Error creating configuration: %s\n" % e)
diff --git a/cmk_base/core_config.py b/cmk_base/core_config.py
index f71e7ff..5d33998 100644
--- a/cmk_base/core_config.py
+++ b/cmk_base/core_config.py
@@ -218,10 +218,10 @@ def check_icmp_arguments_of(hostname, add_defaults=True,
family=None):
# '----------------------------------------------------------------------'
# TODO: Move to modes?
-def do_create_config(with_agents, cmc_file):
+def do_create_config(create_config_hook, with_agents):
console.output("Generating configuration for core (type %s)..." %
config.monitoring_core)
- create_core_config(cmc_file)
+ create_core_config(create_config_hook)
console.output(tty.ok + "\n")
if with_agents:
@@ -232,25 +232,17 @@ def do_create_config(with_agents, cmc_file):
pass
-def create_core_config(cmc_file="config"):
+def create_core_config(create_config_hook):
initialize_warnings()
_verify_non_duplicate_hosts()
_verify_non_deprecated_checkgroups()
- _create_config(cmc_file)
+ create_config_hook()
cmk.password_store.save(config.stored_passwords)
return get_configuration_warnings()
-def _create_config(cmc_file):
- if config.monitoring_core == "cmc":
- from cmk_base.cee.core_cmc import create_config_hook
- else:
- from cmk_base.core_nagios import create_config_hook
- create_config_hook(cmc_file)
-
-
# Verify that the user has no deprecated check groups configured.
def _verify_non_deprecated_checkgroups():
groups = checks.checks_by_checkgroup()
@@ -282,9 +274,9 @@ def precompile():
precompile_hook()
-def do_update(with_precompile, cmc_file):
+def do_update(create_config_hook, with_precompile):
try:
- do_create_config(with_agents=with_precompile, cmc_file=cmc_file)
+ do_create_config(create_config_hook, with_agents=with_precompile)
if with_precompile:
precompile()
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 0261f1b..8f58446 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -57,7 +57,7 @@ import cmk_base.check_utils
# | Create a configuration file for Nagios core with hosts + services |
# '----------------------------------------------------------------------'
-def create_config_hook(_cmc_file):
+def create_config_hook():
with file(cmk.paths.nagios_objects_file, "w") as out:
create_config(out, None)
diff --git a/cmk_base/discovery.py b/cmk_base/discovery.py
index 43407ac..0b9a05c 100644
--- a/cmk_base/discovery.py
+++ b/cmk_base/discovery.py
@@ -461,7 +461,7 @@ def _get_autodiscovery_dir():
return cmk.paths.var_dir + '/autodiscovery'
-def discover_marked_hosts():
+def discover_marked_hosts(create_config_hook):
console.verbose("Doing discovery for all marked hosts:\n")
autodiscovery_dir = _get_autodiscovery_dir()
@@ -497,9 +497,9 @@ def discover_marked_hosts():
if activation_required:
console.verbose("\nRestarting monitoring core with updated
configuration...\n")
if config.monitoring_core == "cmc":
- core.do_reload()
+ core.do_reload(create_config_hook)
else:
- core.do_restart()
+ core.do_restart(create_config_hook)
def _discover_marked_host(hostname, all_hosts, now_ts, oldest_queued):
diff --git a/cmk_base/modes/check_mk.py b/cmk_base/modes/check_mk.py
index d457409..7a3d9d3 100644
--- a/cmk_base/modes/check_mk.py
+++ b/cmk_base/modes/check_mk.py
@@ -1010,12 +1010,22 @@ modes.register(Mode(
# TODO: The "config" literal should really be centralized somehow.
def _cmc_file(options):
- return options["cmc-file"] if "cmc-file" in options else
"config"
+ return options["cmc-file"] if options and "cmc-file" in options
else "config"
+
+
+def _create_config_hook(options=None):
+ 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 mode_update_no_precompile(options):
from cmk_base.core_config import do_update
- do_update(with_precompile=False, cmc_file=_cmc_file(options))
+ do_update(_create_config_hook(options), with_precompile=False)
modes.register(Mode(
long_option="update-no-precompile",
@@ -1072,7 +1082,7 @@ modes.register(Mode(
def mode_update(options):
from cmk_base.core_config import do_update
- do_update(with_precompile=True, cmc_file=_cmc_file(options))
+ do_update(_create_config_hook(options), with_precompile=True)
modes.register(Mode(
long_option="update",
@@ -1111,7 +1121,7 @@ modes.register(Mode(
def mode_restart():
import cmk_base.core as core
- core.do_restart()
+ core.do_restart(_create_config_hook())
modes.register(Mode(
long_option="restart",
@@ -1133,7 +1143,7 @@ modes.register(Mode(
def mode_reload():
import cmk_base.core as core
- core.do_reload()
+ core.do_reload(_create_config_hook())
modes.register(Mode(
long_option="reload",
@@ -1385,7 +1395,7 @@ modes.register(Mode(
def mode_discover_marked_hosts():
import cmk_base.discovery as discovery
- discovery.discover_marked_hosts()
+ discovery.discover_marked_hosts(_create_config_hook())
modes.register(Mode(
long_option="discover-marked-hosts",