Module: check_mk
Branch: master
Commit: 4c956063d52a7758f8870ceda429373476518961
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4c956063d52a77…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Mon Mar 26 09:44:58 2018 +0200
Centralize loading of EC configuration.
Apart from reducing fragile copy-n-paste, this makes sure that the rule
proxies are always bound.
Change-Id: I7fca2f6da3fd144aac5d6ea978f633dcc8a0fa48
---
cmk/ec/export.py | 39 +++++++++++++++++++++++----------------
cmk/ec/main.py | 15 +--------------
web/htdocs/mkeventd.py | 6 +-----
3 files changed, 25 insertions(+), 35 deletions(-)
diff --git a/cmk/ec/export.py b/cmk/ec/export.py
index 12e6b67..76d566b 100644
--- a/cmk/ec/export.py
+++ b/cmk/ec/export.py
@@ -186,23 +186,35 @@ def remove_exported_rule_pack(id_):
# Only called from cmk.ec.main.load_configuration.
-def bind_to_rule_pack_proxies(rule_packs, mkp_rule_packs):
+def _bind_to_rule_pack_proxies(rule_packs, mkp_rule_packs):
# type: (Any, Any) -> None
"""
Binds all proxy rule packs of the variable rule_packs to
the corresponding mkp_rule_packs.
"""
for rule_pack in rule_packs:
- if not isinstance(rule_pack, MkpRulePackProxy):
- continue
-
try:
- rule_pack.bind_to(mkp_rule_packs[rule_pack.id_])
+ if isinstance(rule_pack, MkpRulePackProxy):
+ rule_pack.bind_to(mkp_rule_packs[rule_pack.id_])
except KeyError:
raise MkpRulePackBindingError('Exported rule pack with ID "%s"
not found.'
% rule_pack.id_)
+def load_config(settings):
+ # type: (cmk.ec.settings.Settings) -> Dict[str, Any]
+ """Load event console configuration."""
+ config = cmk.ec.defaults.default_config()
+ config["MkpRulePackProxy"] = MkpRulePackProxy
+ for path in [settings.paths.main_config_file.value] + \
+ sorted(settings.paths.config_dir.value.glob('**/*.mk')):
+ with open(str(path)) as file_object:
+ exec(file_object, config) # pylint: disable=exec-used
+ config.pop("MkpRulePackProxy", None)
+ _bind_to_rule_pack_proxies(config['rule_packs'],
config['mkp_rule_packs'])
+ return config
+
+
# NOTE: This is the *only* place which can introduce legacy rules. Can we avoid
# that and put it directly into the default rule pack?
def load_rule_packs():
@@ -212,26 +224,21 @@ def load_rule_packs():
a site. Proxy objects in the rule packs are already bound to the referenced
object.
"""
- context = cmk.ec.defaults.default_config()
- context["MkpRulePackProxy"] = MkpRulePackProxy
- for path in [rule_pack_dir() / "rules.mk"] +
sorted(mkp_rule_pack_dir().glob('*.mk')):
- cmk.store.load_mk_file(str(path), context)
+ config = load_config(_default_settings())
# Convert some data fields into a new format
- for rule in context["rules"]:
+ for rule in config["rules"]:
if "livetime" in rule:
livetime = rule["livetime"]
if not isinstance(livetime, tuple):
rule["livetime"] = (livetime, ["open"])
# Convert old plain rules into a list of one rule pack
- if context["rules"] and not context["rule_packs"]:
- context["rule_packs"] = [
- cmk.ec.defaults.default_rule_pack(context["rules"])]
-
- bind_to_rule_pack_proxies(context['rule_packs'],
context['mkp_rule_packs'])
+ if config["rules"] and not config["rule_packs"]:
+ config["rule_packs"] = [
+ cmk.ec.defaults.default_rule_pack(config["rules"])]
- return context['rules'], context['rule_packs']
+ return config['rules'], config['rule_packs']
def save_rule_packs(legacy_rules, rule_packs, pretty_print=False, dir_=None):
diff --git a/cmk/ec/main.py b/cmk/ec/main.py
index 26534b5..610d08c 100644
--- a/cmk/ec/main.py
+++ b/cmk/ec/main.py
@@ -4944,20 +4944,7 @@ def load_slave_status(settings):
def load_configuration(settings):
global g_config, g_last_config_reload
- g_config = cmk.ec.defaults.default_config()
- g_config['MkpRulePackProxy'] = cmk.ec.export.MkpRulePackProxy # needed for
exec
-
- config_path = settings.paths.main_config_file.value
- if not config_path.exists():
- bail_out("Main configuration file %s missing." % config_path)
- for path in [config_path] +
sorted(settings.paths.config_dir.value.glob('**/*.mk')):
- with open(str(path)) as f:
- exec(f, g_config)
-
- try:
- cmk.ec.export.bind_to_rule_pack_proxies(g_config['rule_packs'],
g_config['mkp_rule_packs'])
- except cmk.ec.export.MkpRulePackBindingError as e:
- bail_out("%s" % e)
+ g_config = cmk.ec.export.load_config(settings)
# If not set by command line, set the log level by configuration
if settings.options.verbosity == 0:
diff --git a/web/htdocs/mkeventd.py b/web/htdocs/mkeventd.py
index edf0faa..aa51900 100644
--- a/web/htdocs/mkeventd.py
+++ b/web/htdocs/mkeventd.py
@@ -148,15 +148,11 @@ def eventd_configuration():
if cached_config and cached_config[0] is html:
return cached_config[1]
- config = cmk.ec.defaults.default_config()
- config["MkpRulePackProxy"] = cmk.ec.export.MkpRulePackProxy
settings = cmk.ec.settings.settings('',
Path(cmk.paths.omd_root),
Path(cmk.paths.default_config_dir),
[''])
- for path in [settings.paths.main_config_file.value] +
sorted(settings.paths.config_dir.value.glob('**/*.mk')):
- cmk.store.load_mk_file(str(path), config)
-
+ config = cmk.ec.export.load_config(settings)
cached_config = (html, config)
return config