def __init__(self): # Local import to have faster pytest initialization import cmk.base.api.agent_based.register as register # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} with cmk_debug_enabled(): # fail if a plugin can't be loaded config.load_all_agent_based_plugins( check_api.get_check_api_context) inventory_plugins.load_legacy_inventory_plugins( check_api.get_check_api_context, register.inventory_plugins_legacy.get_inventory_context, ) # some sanitiy checks, may decrease as we migrate assert len(config.check_info) > 1000 assert len(config.snmp_info) > 400 assert len(inventory_plugins._inv_info) > 60 self._snmp_sections = copy.deepcopy( register._config.registered_snmp_sections) self._agent_sections = copy.deepcopy( register._config.registered_agent_sections) self._check_plugins = copy.deepcopy( register._config.registered_check_plugins) self._inventory_plugins = copy.deepcopy( register._config.registered_inventory_plugins)
def _config_load_all_checks(): # Local import to have faster pytest initialization import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} with cmk_debug_enabled(): # fail if a plugin can't be loaded config.load_all_agent_based_plugins(check_api.get_check_api_context) assert len(config.check_info) > 1000 # sanitiy check
def execute(self, cmd: str, args: List[str]) -> Any: self._handle_generic_arguments(args) try: try: automation = self._automations[cmd] except KeyError: raise MKAutomationError("Automation command '%s' is not implemented." % cmd) if automation.needs_checks: with redirect_stdout(open(os.devnull, "w")): log.setup_console_logging() config.load_all_agent_based_plugins( check_api.get_check_api_context, inventory_plugins.load_legacy_inventory_plugins, ) if automation.needs_config: config.load(validate_hosts=False) result = automation.execute(args) except (MKAutomationError, MKTimeout) as e: console.error("%s\n" % e) if cmk.utils.debug.enabled(): raise return 1 except Exception as e: if cmk.utils.debug.enabled(): raise console.error("%s\n" % e) return 2 finally: profiling.output_profile() out.output(result.serialize()) out.output("\n") return 0
def __init__(self) -> None: # Local import to have faster pytest initialization import cmk.base.api.agent_based.register as register # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} config.load_all_agent_based_plugins( check_api.get_check_api_context, inventory_plugins.load_legacy_inventory_plugins, ) # our test environment does not deal with namespace packages properly. load plus plugins: try: load_plugins = list( load_plugins_with_exceptions( "plus.cmk.base.plugins.agent_based")) except ModuleNotFoundError: pass else: for _plugin, exception in load_plugins: raise exception inventory_plugins.load_legacy_inventory_plugins( check_api.get_check_api_context, register.inventory_plugins_legacy.get_inventory_context, ) self._snmp_sections = copy.deepcopy( register._config.registered_snmp_sections) self._agent_sections = copy.deepcopy( register._config.registered_agent_sections) self._check_plugins = copy.deepcopy( register._config.registered_check_plugins) self._inventory_plugins = copy.deepcopy( register._config.registered_inventory_plugins)
def execute(self, cmd: str, args: List[str]) -> Any: self._handle_generic_arguments(args) try: try: automation = self._automations[cmd] except KeyError: raise MKAutomationError( "Automation command '%s' is not implemented." % cmd) if automation.needs_checks: config.load_all_agent_based_plugins( check_api.get_check_api_context) if automation.needs_config: config.load(validate_hosts=False) result = automation.execute(args) except (MKAutomationError, MKTimeout) as e: console.error("%s\n" % e) if cmk.utils.debug.enabled(): raise return 1 except Exception as e: if cmk.utils.debug.enabled(): raise console.error("%s\n" % e) return 2 finally: profiling.output_profile() out.output(python_printer.pformat(result)) out.output('\n') return 0
def __init__(self): # Local import to have faster pytest initialization import cmk.base.api.agent_based.register as register # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.config as config # pylint: disable=bad-option-value,import-outside-toplevel import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=bad-option-value,import-outside-toplevel config._initialize_data_structures() assert config.check_info == {} config.load_all_agent_based_plugins(check_api.get_check_api_context) inventory_plugins.load_legacy_inventory_plugins( check_api.get_check_api_context, register.inventory_plugins_legacy.get_inventory_context, ) self._snmp_sections = copy.deepcopy( register._config.registered_snmp_sections) self._agent_sections = copy.deepcopy( register._config.registered_agent_sections) self._check_plugins = copy.deepcopy( register._config.registered_check_plugins) self._inventory_plugins = copy.deepcopy( register._config.registered_inventory_plugins)
def test_test_check_1_merged_rule(request, site, web): # noqa: F811 # pylint: disable=redefined-outer-name test_check_path = "local/lib/check_mk/base/plugins/agent_based/test_check_1.py" def cleanup(): if site.file_exists("etc/check_mk/conf.d/test_check_1.mk"): site.delete_file("etc/check_mk/conf.d/test_check_1.mk") if site.file_exists("var/check_mk/autochecks/modes-test-host.mk"): site.delete_file("var/check_mk/autochecks/modes-test-host.mk") site.delete_file(test_check_path) request.addfinalizer(cleanup) site.write_file( test_check_path, """ import pprint from .agent_based_api.v1 import register, Service def discover(params, section): yield Service(item=pprint.pformat(params)) def check(item, section): return yield register.check_plugin( name="test_check_1", discovery_function=discover, discovery_ruleset_name="discover_test_check_1", discovery_ruleset_type=register.RuleSetType.MERGED, discovery_default_parameters={"default": 42}, check_function=check, service_name="Foo %s", ) """) site.write_file("var/check_mk/agent_output/modes-test-host", "<<<test_check_1>>>\n1 2\n") config.load_all_agent_based_plugins(check_api.get_check_api_context) config.load(with_conf_d=False) web.discover_services("modes-test-host") # Verify that the discovery worked as expected services = autochecks.parse_autochecks_file("modes-test-host", config.service_description) for service in services: if str(service.check_plugin_name) == "test_check_1": assert service.item == "Parameters({'default': 42})" break else: assert False, '"test_check_1" not discovered' # And now overwrite the setting in the config site.write_file("etc/check_mk/conf.d/test_check_1.mk", "discover_test_check_1 = [{'value': {'levels': (1, 2)}, 'condition': {}}]\n") # rediscover with the setting in the config site.delete_file("var/check_mk/autochecks/modes-test-host.mk") web.discover_services("modes-test-host") services = autochecks.parse_autochecks_file("modes-test-host", config.service_description) for service in services: if str(service.check_plugin_name) == "test_check_1": assert service.item == "Parameters({'default': 42, 'levels': (1, 2)})" break else: assert False, '"test_check_1" not discovered'
def update(): config.load_all_agent_based_plugins(check_api.get_check_api_context) config.load() config_cache = config.get_config_cache() update_service_info(config_cache, get_hostnames(config_cache))