From 73015f9555de25aa7fb6d57088d8afcde9326b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Herbel?= Date: Thu, 16 Jan 2025 11:37:27 +0100 Subject: [PATCH] Automation calls via helper: Stop calling `config.{load, load_all_plugins}` When called via the automation helper, everything should already be loaded. Calls such as `config.load(with_conf_d=False)` render the automation helper in an inconsistent state. CMK-21103 Change-Id: Iddc5f29fa689fe7eca4d5298436ef87f4b4c5175 --- cmk/base/automations/check_mk.py | 51 ++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/cmk/base/automations/check_mk.py b/cmk/base/automations/check_mk.py index e1b18de5e0d..524637276a9 100644 --- a/cmk/base/automations/check_mk.py +++ b/cmk/base/automations/check_mk.py @@ -705,7 +705,7 @@ def execute( called_from_automation_helper: bool, ) -> AutodiscoveryResult: with redirect_stdout(open(os.devnull, "w")): - result = _execute_autodiscovery() + result = _execute_autodiscovery(called_from_automation_helper) return AutodiscoveryResult(*result) @@ -713,14 +713,17 @@ def execute( automations.register(AutomationAutodiscovery()) -def _execute_autodiscovery() -> tuple[Mapping[HostName, DiscoveryResult], bool]: +def _execute_autodiscovery( + called_from_automation_helper: bool, +) -> tuple[Mapping[HostName, DiscoveryResult], bool]: # pylint: disable=too-many-branches file_cache_options = FileCacheOptions(use_outdated=True) if not (autodiscovery_queue := AutoQueue(autodiscovery_dir)): return {}, False - config.load() + if not called_from_automation_helper: + config.load() config_cache = config.get_config_cache() ab_plugins = agent_based_register.get_previously_loaded_plugins() autochecks_config = config.AutochecksConfigurer(config_cache) @@ -1011,17 +1014,18 @@ def execute( config_cache = config.get_config_cache() - # Not loading all checks improves performance of the calls and as a result the - # responsiveness of the "service discovery" page. For real hosts we don't need the checks, - # because we already have calculated service names. For clusters we have to load all - # checks for config_cache.set_autochecks, because it needs to calculate the - # service_descriptions of existing services to decided whether or not they are clustered - # (See autochecks.set_autochecks_of_cluster()) - if hostname in config_cache.hosts_config.clusters: - config.load_all_plugins( - local_checks_dir=local_checks_dir, - checks_dir=checks_dir, - ) + if not called_from_automation_helper: + # Not loading all checks improves performance of the calls and as a result the + # responsiveness of the "service discovery" page. For real hosts we don't need the checks, + # because we already have calculated service names. For clusters we have to load all + # checks for config_cache.set_autochecks, because it needs to calculate the + # service_descriptions of existing services to decided whether or not they are clustered + # (See autochecks.set_autochecks_of_cluster()) + if hostname in config_cache.hosts_config.clusters: + config.load_all_plugins( + local_checks_dir=local_checks_dir, + checks_dir=checks_dir, + ) new_services = [ AutocheckServiceWithNodes( @@ -1949,21 +1953,22 @@ def execute( args: list[str], called_from_automation_helper: bool, ) -> GetConfigurationResult: - config.load(with_conf_d=False) - # We read the list of variable names from stdin since # that could be too much for the command line variable_names = ast.literal_eval(sys.stdin.read()) - missing_variables = [v for v in variable_names if not hasattr(config, v)] - - if missing_variables: - config.load_all_plugins( - local_checks_dir=local_checks_dir, - checks_dir=checks_dir, - ) + if not called_from_automation_helper: config.load(with_conf_d=False) + missing_variables = [v for v in variable_names if not hasattr(config, v)] + + if missing_variables: + config.load_all_plugins( + local_checks_dir=local_checks_dir, + checks_dir=checks_dir, + ) + config.load(with_conf_d=False) + result = {} for varname in variable_names: if hasattr(config, varname):