From d9636cc109603b73d3861dcf8fd05e1773464d16 Mon Sep 17 00:00:00 2001 From: Lars Michelsen Date: Mon, 15 Apr 2019 19:39:54 +0200 Subject: [PATCH] Cleanup all direct config.service_extra_conf calls Change-Id: I74b08c1ca5aa141c626f0933478db6122da8a995 --- cmk_base/check_api.py | 10 ++++-- cmk_base/config.py | 31 +++++-------------- cmk_base/core_config.py | 22 +++++++------ cmk_base/core_nagios.py | 55 +++++++++++++++++---------------- cmk_base/dump_host.py | 2 +- tests/unit/cmk_base/test_core_config.py | 7 +++-- 6 files changed, 61 insertions(+), 66 deletions(-) diff --git a/cmk_base/check_api.py b/cmk_base/check_api.py index 8ecf4da741..8bc1798d2c 100644 --- a/cmk_base/check_api.py +++ b/cmk_base/check_api.py @@ -201,7 +201,11 @@ def savefloat(f): return 0.0 -service_extra_conf = _config.service_extra_conf +# Compatibility wrapper for the pre 1.6 existant conf.service_extra_conf() +def service_extra_conf(hostname, service, ruleset): + return _config.get_config_cache().service_extra_conf(hostname, service, ruleset) + + host_extra_conf = _config.host_extra_conf in_binary_hostlist = _config.in_binary_hostlist host_extra_conf_merged = _config.host_extra_conf_merged @@ -483,8 +487,8 @@ def check_levels(value, def get_effective_service_level(): """Get the service level that applies to the current service. This can only be used within check functions, not during discovery nor parsing.""" - service_levels = _config.service_extra_conf(host_name(), service_description(), - _config.service_service_levels) + service_levels = _config.get_config_cache().service_extra_conf( + host_name(), service_description(), _config.service_service_levels) if service_levels: return service_levels[0] diff --git a/cmk_base/config.py b/cmk_base/config.py index e37b453a66..7e731e6791 100644 --- a/cmk_base/config.py +++ b/cmk_base/config.py @@ -1095,7 +1095,7 @@ def _get_exit_code_spec(spec, data_source_id): def check_period_of(hostname, service): - periods = service_extra_conf(hostname, service, check_periods) + periods = get_config_cache().service_extra_conf(hostname, service, check_periods) if periods: period = periods[0] if period == "24X7": @@ -1545,23 +1545,6 @@ def get_http_proxy(http_proxy): #. -# .--Service rules-------------------------------------------------------. -# | ____ _ _ | -# | / ___| ___ _ ____ _(_) ___ ___ _ __ _ _| | ___ ___ | -# | \___ \ / _ \ '__\ \ / / |/ __/ _ \ | '__| | | | |/ _ \/ __| | -# | ___) | __/ | \ V /| | (_| __/ | | | |_| | | __/\__ \ | -# | |____/ \___|_| \_/ |_|\___\___| |_| \__,_|_|\___||___/ | -# | | -# +----------------------------------------------------------------------+ -# | Service rule set matching | -# '----------------------------------------------------------------------' - - -def service_extra_conf(hostname, service, ruleset): - return get_config_cache().service_extra_conf(hostname, service, ruleset) - - -#. # .--Host rulesets-------------------------------------------------------. # | _ _ _ _ _ | # | | | | | ___ ___| |_ _ __ _ _| | ___ ___ ___| |_ ___ | @@ -2477,11 +2460,13 @@ def _update_with_default_check_parameters(checktype, params): def _update_with_configured_check_parameters(host, checktype, item, params): descr = service_description(host, checktype, item) + config_cache = get_config_cache() + # Get parameters configured via checkgroup_parameters - entries = _get_checkgroup_parameters(host, checktype, item) + entries = _get_checkgroup_parameters(config_cache, host, checktype, item) # Get parameters configured via check_parameters - entries += service_extra_conf(host, descr, check_parameters) + entries += config_cache.service_extra_conf(host, descr, check_parameters) if entries: if _has_timespecific_params(entries): @@ -2510,7 +2495,7 @@ def _has_timespecific_params(entries): return False -def _get_checkgroup_parameters(host, checktype, item): +def _get_checkgroup_parameters(config_cache, host, checktype, item): checkgroup = check_info[checktype]["group"] if not checkgroup: return [] @@ -2521,10 +2506,10 @@ def _get_checkgroup_parameters(host, checktype, item): try: # checks without an item if item is None and checkgroup not in service_rule_groups: - return host_extra_conf(host, rules) + return config_cache.host_extra_conf(host, rules) # checks with an item need service-specific rules - return service_extra_conf(host, item, rules) + return config_cache.service_extra_conf(host, item, rules) except MKGeneralException as e: raise MKGeneralException(str(e) + " (on host %s, checktype %s)" % (host, checktype)) diff --git a/cmk_base/core_config.py b/cmk_base/core_config.py index bc8039f70d..138d25f16a 100644 --- a/cmk_base/core_config.py +++ b/cmk_base/core_config.py @@ -172,12 +172,13 @@ def autodetect_plugin(command_line): return command_line -def icons_and_actions_of(what, hostname, svcdesc=None, checkname=None, params=None): +# TODO: Better move to cmk_base.config module +def _icons_and_actions_of(config_cache, what, hostname, svcdesc=None, checkname=None, params=None): if what == 'host': - return list(set(config.host_extra_conf(hostname, config.host_icons_and_actions))) + return list(set(config_cache.host_extra_conf(hostname, config.host_icons_and_actions))) else: actions = set( - config.service_extra_conf(hostname, svcdesc, config.service_icons_and_actions)) + config_cache.service_extra_conf(hostname, svcdesc, config.service_icons_and_actions)) # Some WATO rules might register icons on their own if checkname: @@ -343,7 +344,8 @@ def _extra_service_attributes(hostname, description, config_cache, checkname, pa # Add service custom_variables. Name conflicts are prevented by the GUI, but just # to be sure, add them first. The other definitions will override the custom attributes. - for varname, value in custom_service_attributes_of(hostname, description).iteritems(): + for varname, value in _custom_service_attributes_of(config_cache, hostname, + description).iteritems(): attrs["_%s" % varname.upper()] = value for key, conflist in config.extra_service_conf.items(): @@ -359,17 +361,17 @@ def _extra_service_attributes(hostname, description, config_cache, checkname, pa attrs["_%s" % varname.upper()] = value # Add custom user icons and actions - actions = icons_and_actions_of("service", hostname, description, checkname, params) + actions = _icons_and_actions_of(config_cache, "service", hostname, description, checkname, + params) if actions: attrs["_ACTIONS"] = ','.join(actions) return attrs -# TODO: Hand over config_cache and use it instead of config.service_extra_conf -def custom_service_attributes_of(hostname, service_description): +def _custom_service_attributes_of(config_cache, hostname, service_description): return dict( - itertools.chain(*config.service_extra_conf(hostname, service_description, - config.custom_service_attributes))) + itertools.chain(*config_cache.service_extra_conf(hostname, service_description, + config.custom_service_attributes))) #. @@ -443,7 +445,7 @@ def get_host_attributes(hostname, config_cache): attrs["_FILENAME"] = path # Add custom user icons and actions - actions = icons_and_actions_of("host", hostname) + actions = _icons_and_actions_of(config_cache, "host", hostname) if actions: attrs["_ACTIONS"] = ",".join(actions) diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py index 600b381307..d123be7e0f 100644 --- a/cmk_base/core_nagios.py +++ b/cmk_base/core_nagios.py @@ -169,11 +169,11 @@ def _create_nagios_config_host(cfg, config_cache, hostname): cfg.outfile.write("# ----------------------------------------------------\n") host_attrs = core_config.get_host_attributes(hostname, config_cache) if config.generate_hostconf: - _create_nagios_hostdefs(cfg, hostname, host_attrs) + _create_nagios_hostdefs(cfg, config_cache, hostname, host_attrs) _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs) -def _create_nagios_hostdefs(cfg, hostname, attrs): +def _create_nagios_hostdefs(cfg, config_cache, hostname, attrs): is_clust = config.is_cluster(hostname) ip = attrs["address"] @@ -229,8 +229,8 @@ def _create_nagios_hostdefs(cfg, hostname, attrs): # Get parents manually defined via extra_host_conf["parents"]. Only honor # variable "parents" and implicit parents if this setting is empty - extra_conf_parents = config.host_extra_conf(hostname, - config.extra_host_conf.get("parents", [])) + extra_conf_parents = config_cache.host_extra_conf(hostname, + config.extra_host_conf.get("parents", [])) if not extra_conf_parents: parents_list = config.parents_of(hostname) @@ -248,7 +248,8 @@ def _create_nagios_hostdefs(cfg, hostname, attrs): host_spec["alias"] = alias # Custom configuration last -> user may override all other values - host_spec.update(_extra_host_conf_of(hostname, exclude=["parents"] if is_clust else [])) + host_spec.update( + _extra_host_conf_of(config_cache, hostname, exclude=["parents"] if is_clust else [])) cfg.outfile.write(_format_nagios_object("host", host_spec).encode("utf-8")) @@ -326,8 +327,8 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): # (if configured) the snmp_check_interval for snmp based checks check_interval = 1 # default hardcoded interval # Customized interval of Check_MK service - values = config.service_extra_conf(hostname, "Check_MK", - config.extra_service_conf.get('check_interval', [])) + values = config_cache.service_extra_conf( + hostname, "Check_MK", config.extra_service_conf.get('check_interval', [])) if values: try: check_interval = int(values[0]) @@ -348,7 +349,7 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): service_spec.update( core_config.get_service_attributes( hostname, description, config_cache, checkname=checkname, params=params)) - service_spec.update(_extra_service_conf_of(cfg, hostname, description)) + service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname, description)) outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) @@ -363,11 +364,11 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): "service_description": "Check_MK", } service_spec.update(core_config.get_service_attributes(hostname, "Check_MK", config_cache)) - service_spec.update(_extra_service_conf_of(cfg, hostname, "Check_MK")) + service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname, "Check_MK")) outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) # legacy checks via legacy_checks - legchecks = config.host_extra_conf(hostname, config.legacy_checks) + legchecks = config_cache.host_extra_conf(hostname, config.legacy_checks) if len(legchecks) > 0: outfile.write("\n\n# Legacy checks\n") for command, description, has_perfdata in legchecks: @@ -400,7 +401,7 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): "active_checks_enabled": 1, } service_spec.update(core_config.get_service_attributes(hostname, description, config_cache)) - service_spec.update(_extra_service_conf_of(cfg, hostname, description)) + service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname, description)) outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) # write service dependencies for legacy checks @@ -409,7 +410,7 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): # legacy checks via active_checks actchecks = [] for acttype, rules in config.active_checks.items(): - entries = config.host_extra_conf(hostname, rules) + entries = config_cache.host_extra_conf(hostname, rules) if entries: # Skip Check_MK HW/SW Inventory for all ping hosts, even when the user has enabled # the inventory for ping only hosts @@ -474,14 +475,14 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): } service_spec.update( core_config.get_service_attributes(hostname, description, config_cache)) - service_spec.update(_extra_service_conf_of(cfg, hostname, description)) + service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname, description)) outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) # write service dependencies for active checks outfile.write(get_dependencies(hostname, description).encode("utf-8")) # Legacy checks via custom_checks - custchecks = config.host_extra_conf(hostname, config.custom_checks) + custchecks = config_cache.host_extra_conf(hostname, config.custom_checks) if custchecks: outfile.write("\n\n# Custom checks\n") for entry in custchecks: @@ -547,7 +548,7 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): service_spec.update(freshness) service_spec.update( core_config.get_service_attributes(hostname, description, config_cache)) - service_spec.update(_extra_service_conf_of(cfg, hostname, description)) + service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname, description)) outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) # write service dependencies for custom checks @@ -575,7 +576,8 @@ def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs): } service_spec.update( core_config.get_service_attributes(hostname, service_discovery_name, config_cache)) - service_spec.update(_extra_service_conf_of(cfg, hostname, service_discovery_name)) + service_spec.update( + _extra_service_conf_of(cfg, config_cache, hostname, service_discovery_name)) outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) if have_at_least_one_service: @@ -620,7 +622,7 @@ def _add_ping_service(cfg, config_cache, hostname, ipaddress, family, descr, nod "check_command": "%s!%s" % (ping_command, arguments), } service_spec.update(core_config.get_service_attributes(hostname, descr, config_cache)) - service_spec.update(_extra_service_conf_of(cfg, hostname, descr)) + service_spec.update(_extra_service_conf_of(cfg, config_cache, hostname, descr)) cfg.outfile.write(_format_nagios_object("service", service_spec).encode("utf-8")) @@ -879,44 +881,45 @@ def _quote_nagios_string(s): return "'" + s.replace('\\', '\\\\').replace("'", "'\"'\"'").replace('!', '\\!') + "'" -def _extra_host_conf_of(hostname, exclude=None): +def _extra_host_conf_of(config_cache, hostname, exclude=None): if exclude is None: exclude = [] - return _extra_conf_of(config.extra_host_conf, hostname, None, exclude) + return _extra_conf_of(config_cache, config.extra_host_conf, hostname, None, exclude) # Collect all extra configuration data for a service -def _extra_service_conf_of(cfg, hostname, description): +def _extra_service_conf_of(cfg, config_cache, hostname, description): service_spec = {} # Contact groups - sercgr = config.service_extra_conf(hostname, description, config.service_contactgroups) + sercgr = config_cache.service_extra_conf(hostname, description, config.service_contactgroups) cfg.contactgroups_to_define.update(sercgr) if len(sercgr) > 0: if config.enable_rulebased_notifications: sercgr.append("check-mk-notify") # not nessary if not explicit groups defined service_spec["contact_groups"] = ",".join(sercgr) - sergr = config.service_extra_conf(hostname, description, config.service_groups) + sergr = config_cache.service_extra_conf(hostname, description, config.service_groups) if len(sergr) > 0: service_spec["service_groups"] = ",".join(sergr) if config.define_servicegroups: cfg.servicegroups_to_define.update(sergr) - service_spec.update(_extra_conf_of(config.extra_service_conf, hostname, description)) + service_spec.update( + _extra_conf_of(config_cache, config.extra_service_conf, hostname, description)) return service_spec -def _extra_conf_of(confdict, hostname, service, exclude=None): +def _extra_conf_of(config_cache, confdict, hostname, service, exclude=None): if exclude is None: exclude = [] result = {} for key, conflist in confdict.items(): if service is not None: - values = config.service_extra_conf(hostname, service, conflist) + values = config_cache.service_extra_conf(hostname, service, conflist) else: - values = config.host_extra_conf(hostname, conflist) + values = config_cache.host_extra_conf(hostname, conflist) if exclude and key in exclude: continue diff --git a/cmk_base/dump_host.py b/cmk_base/dump_host.py index 7e4067aee5..e646b4df2c 100644 --- a/cmk_base/dump_host.py +++ b/cmk_base/dump_host.py @@ -127,7 +127,7 @@ def dump_host(hostname): _evaluate_params(params), cmk_base.utils.make_utf8(description), cmk_base.utils.make_utf8(",".join( - config.service_extra_conf(hostname, description, config.service_groups))), + config_cache.service_extra_conf(hostname, description, config.service_groups))), ",".join(deps) ] for checktype, item, params, description, deps in check_items], " ") diff --git a/tests/unit/cmk_base/test_core_config.py b/tests/unit/cmk_base/test_core_config.py index 36d0426fe1..b6c31a88bd 100644 --- a/tests/unit/cmk_base/test_core_config.py +++ b/tests/unit/cmk_base/test_core_config.py @@ -74,7 +74,8 @@ def test_get_tag_attributes(tag_groups, result): def test_custom_service_attributes_of(monkeypatch): - attributes = core_config.custom_service_attributes_of("luluhost", "laladescr") + config_cache = config.get_config_cache() + attributes = core_config._custom_service_attributes_of(config_cache, "luluhost", "laladescr") assert attributes == {} monkeypatch.setattr(config, "all_hosts", ["luluhost"]) @@ -84,9 +85,9 @@ def test_custom_service_attributes_of(monkeypatch): ([('ding', '2'), ('ding', '2a'), ('dong', '3')], [], config.ALL_HOSTS, config.ALL_SERVICES, {}), ]) - config.get_config_cache().initialize() + config_cache.initialize() - attributes = core_config.custom_service_attributes_of("luluhost", "laladescr") + attributes = core_config._custom_service_attributes_of(config_cache, "luluhost", "laladescr") assert attributes == { "deng": "1", "ding": "2a", -- 2.11.4.GIT