Module: check_mk
Branch: master
Commit: 0c53533873306298564486f22aafab741baaf45d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0c535338733062…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Jun 11 12:17:24 2018 +0200
Make the monitoring core an abstract class with 2 concrete implementations.
Probably lots of stuff should really be moved into NagiosCore and CMC, but
this is left as an easy exercise for the reader... ;-) More seriously: The
current state is structurally the correct way to handle the different cores,
moving functionality into the subclasses and nuking non-OO comparisons like
'monitoring_core == "cmc"' on the way can be done incrementally later.
Change-Id: Ia41316a30419b50ab89716822c451a4bed5a4fce
---
cmk_base/automations/check_mk.py | 2 +-
cmk_base/core.py | 2 +-
cmk_base/core_config.py | 22 ++++++++++++++++++++--
cmk_base/core_factory.py | 32 ++++++--------------------------
cmk_base/core_nagios.py | 26 ++++++++++++++------------
5 files changed, 42 insertions(+), 42 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index 6d79c74..672438b 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -784,7 +784,7 @@ class AutomationRestart(Automation):
if backup_path:
os.remove(backup_path)
- core["precompile"]()
+ core.precompile()
cmk_base.core.do_core_action(self._mode())
else:
diff --git a/cmk_base/core.py b/cmk_base/core.py
index c540e23..aa63a36 100644
--- a/cmk_base/core.py
+++ b/cmk_base/core.py
@@ -97,7 +97,7 @@ def do_restart(core, only_reload=False):
if backup_path:
os.remove(backup_path)
- core["precompile"]()
+ core.precompile()
do_core_action(only_reload and "reload" or "restart")
else:
diff --git a/cmk_base/core_config.py b/cmk_base/core_config.py
index 03544f2..d913099 100644
--- a/cmk_base/core_config.py
+++ b/cmk_base/core_config.py
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import abc
import os
import sys
@@ -40,6 +41,23 @@ import cmk_base.checks as checks
import cmk_base.rulesets as rulesets
import cmk_base.ip_lookup as ip_lookup
+
+class MonitoringCore(object):
+ __metaclass__ = abc.ABCMeta
+
+ @abc.abstractmethod
+ def __init__(self):
+ super(MonitoringCore, self).__init__()
+
+ @abc.abstractmethod
+ def create_config(self):
+ pass
+
+ @abc.abstractmethod
+ def precompile(self):
+ pass
+
+
_ignore_ip_lookup_failures = False
_failed_ip_lookups = []
@@ -237,7 +255,7 @@ def create_core_config(core):
_verify_non_duplicate_hosts()
_verify_non_deprecated_checkgroups()
- core["create_config"]()
+ core.create_config()
cmk.password_store.save(config.stored_passwords)
return get_configuration_warnings()
@@ -270,7 +288,7 @@ def do_update(core, with_precompile):
try:
do_create_config(core, with_agents=with_precompile)
if with_precompile:
- core["precompile"]()
+ core.precompile()
except Exception, e:
console.error("Configuration Error: %s\n" % e)
diff --git a/cmk_base/core_factory.py b/cmk_base/core_factory.py
index 22afe7d..037321c 100644
--- a/cmk_base/core_factory.py
+++ b/cmk_base/core_factory.py
@@ -24,31 +24,11 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-from cmk_base.config import monitoring_core
+import cmk_base.config
-
-# 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()}
+ if cmk_base.config.monitoring_core == "cmc":
+ from cmk_base.cee.core_cmc import CMC
+ return CMC(options)
+ from cmk_base.core_nagios import NagiosCore
+ return NagiosCore()
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 8f58446..707ffbb 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -46,6 +46,20 @@ import cmk_base.data_sources as data_sources
import cmk_base.check_utils
+class NagiosCore(core_config.MonitoringCore):
+ def __init__(self):
+ super(NagiosCore, self).__init__()
+
+ def create_config(self):
+ with file(cmk.paths.nagios_objects_file, "w") as out:
+ create_config(out, None)
+
+ def precompile(self):
+ console.output("Precompiling host checks...")
+ precompile_hostchecks()
+ console.output(tty.ok + "\n")
+
+
# .--Create config-------------------------------------------------------.
# | ____ _ __ _ |
# | / ___|_ __ ___ __ _| |_ ___ ___ ___ _ __ / _(_) __ _ |
@@ -57,11 +71,6 @@ import cmk_base.check_utils
# | Create a configuration file for Nagios core with hosts + services |
# '----------------------------------------------------------------------'
-def create_config_hook():
- with file(cmk.paths.nagios_objects_file, "w") as out:
- create_config(out, None)
-
-
def create_config(outfile, hostnames):
global hostgroups_to_define
hostgroups_to_define = set([])
@@ -862,13 +871,6 @@ def _extra_conf_of(confdict, hostname, service, exclude=None):
# | in adhoc mode (about 75%). |
# '----------------------------------------------------------------------'
-# TODO: Move to modes
-def precompile_hook():
- console.output("Precompiling host checks...")
- precompile_hostchecks()
- console.output(tty.ok + "\n")
-
-
# Find files to be included in precompile host check for a certain
# check (for example df or mem.used). In case of checks with a period
# (subchecks) we might have to include both "mem" and "mem.used".
The