def test_test_check_2(request, test_cfg, site, web): test_check_path = "local/share/check_mk/checks/test_check_2" def cleanup(): if site.file_exists("etc/check_mk/conf.d/test_check_2.mk"): site.delete_file("etc/check_mk/conf.d/test_check_2.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, """ discover_service = False def inventory(info): if discover_service: return [(None, {})] def check(item, params, info): return 0, "OK, discovered!" check_info["test_check_2"] = { "check_function" : check, "inventory_function" : inventory, "service_description" : "Testcheck 2", } """) site.write_file("var/check_mk/agent_output/modes-test-host", "<<<test_check_2>>>\n1 2\n") config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)]) config.load(with_conf_d=False) # Verify that the default variable is in the check context and # not in the global checks module context assert "discover_service" not in config.__dict__ assert "test_check_2" in config._check_contexts assert "discover_service" in config._check_contexts["test_check_2"] web.discover_services("modes-test-host") # Should have discovered nothing so far assert site.read_file("var/check_mk/autochecks/modes-test-host.mk") == "[\n]\n" web.discover_services("modes-test-host") # And now overwrite the setting in the config site.write_file("etc/check_mk/conf.d/test_check_2.mk", "discover_service = True\n") web.discover_services("modes-test-host") # Verify that the discovery worked as expected services = autochecks.parse_autochecks_file("modes-test-host") assert services[0].check_plugin_name == "test_check_2" assert services[0].item is None assert services[0].parameters_unresolved == "{}" assert services[0].service_labels.to_dict() == {}
def test_check_crash_report_read_snmp_info(monkeypatch): Scenario().apply(monkeypatch) config.load_checks( check_api.get_check_api_context, ["%s/uptime" % cmk.utils.paths.checks_dir, "%s/snmp_uptime" % cmk.utils.paths.checks_dir]) cache_path = Path(cmk.utils.paths.data_source_cache_dir, "snmp", "testhost") cache_path.parent.mkdir(parents=True, exist_ok=True) # pylint: disable=no-member with cache_path.open("w", encoding="utf-8") as f: f.write(u"[]\n") try: raise Exception("DING") except Exception: crash = crash_reporting.CheckCrashReport.from_exception_and_context( hostname="testhost", check_plugin_name="snmp_uptime", item=None, is_manual_check=False, params=None, description=u"Uptime", info="X", text=u"Output", ) assert crash.agent_output is None assert crash.snmp_info == b"[]\n"
def test_check_crash_report_read_agent_output(monkeypatch): Scenario().apply(monkeypatch) config.load_checks(check_api.get_check_api_context, [ "%s/uptime" % cmk.utils.paths.checks_dir, "%s/snmp_uptime" % cmk.utils.paths.checks_dir ]) cache_path = Path(cmk.utils.paths.tcp_cache_dir, "testhost") cache_path.parent.mkdir(parents=True, exist_ok=True) with cache_path.open("w", encoding="utf-8") as f: f.write(u"<<<abc>>>\nblablub\n") try: raise Exception("DING") except Exception: crash = crash_reporting.CheckCrashReport.from_exception_and_context( hostname="testhost", check_plugin_name="uptime", check_plugin_kwargs={}, is_manual_check=False, description=u"Uptime", text=u"Output", ) assert isinstance(crash, crash_reporting.CheckCrashReport) assert crash.agent_output == b"<<<abc>>>\nblablub\n" assert crash.snmp_info is None
def load(self, file_names=None): """Load either all check plugins or the given file_names""" import cmk.base.config as config # pylint: disable=import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=import-outside-toplevel import cmk.utils.paths # pylint: disable=import-outside-toplevel if config.all_checks_loaded(): return self # No need to load more if file_names is None: raise RuntimeError( "Loading all checks is not supported. Use the fixture " "\"config_load_all_checks\" instead") if not set(file_names) - CheckManager._CHECK_FILES_LOADED: return self # Everything needed is already loaded # On first call, initialize the basic data structures if not CheckManager._CHECK_FILES_LOADED: config._initialize_data_structures() CheckManager._CHECK_FILES_LOADED.update(set(file_names)) config.load_checks( check_api.get_check_api_context, [os.path.join(cmk.utils.paths.checks_dir, f) for f in file_names]) return self
def load(self, file_names=None): """Load either all check plugins or the given file_names""" import cmk.base.config as config # pylint: disable=import-outside-toplevel import cmk.base.check_api as check_api # pylint: disable=import-outside-toplevel import cmk.utils.paths # pylint: disable=import-outside-toplevel if file_names is None: config.load_all_checks(check_api.get_check_api_context) # loads all checks else: config._initialize_data_structures() config.load_checks(check_api.get_check_api_context, [os.path.join(cmk.utils.paths.checks_dir, f) for f in file_names]) return self
def load(self, file_names=None): """Load either all check plugins or the given file_names""" if sys.version_info[0] < 3: # This has not been ported to Python 3 yet. Prevent mypy in Python 3 mode from following import cmk.base.config as config import cmk.base.check_api as check_api import cmk.utils.paths if file_names is None: config.load_all_checks( check_api.get_check_api_context) # loads all checks else: config._initialize_data_structures() config.load_checks(check_api.get_check_api_context, [ os.path.join(cmk.utils.paths.checks_dir, f) for f in file_names ]) return self
def test_test_check_1(request, test_cfg, site, web): test_check_path = "local/share/check_mk/checks/test_check_1" 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, """ test_check_1_default_levels = 10.0, 20.0 def inventory(info): return [(None, "test_check_1_default_levels")] def check(item, params, info): return 0, "OK - %r" % (test_check_1_default_levels, ) check_info["test_check_1"] = { "check_function" : check, "inventory_function" : inventory, "service_description" : "Testcheck 1", # "default_levels_variable" : "test_check_1_default_levels" } """) site.write_file("var/check_mk/agent_output/modes-test-host", "<<<test_check_1>>>\n1 2\n") config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)]) config.load(with_conf_d=False) # Verify that the default variable is in the check context and # not in the global checks module context. assert "test_check_1_default_levels" not in config.__dict__ assert "test_check_1" in config._check_contexts assert "test_check_1_default_levels" in config._check_contexts[ "test_check_1"] web.discover_services("modes-test-host") # Verify that the discovery worked as expected services = autochecks.parse_autochecks_file("modes-test-host", config.service_description) assert services[0].check_plugin_name == "test_check_1" assert services[0].item is None assert services[0].parameters_unresolved == "test_check_1_default_levels" assert services[0].service_labels.to_dict() == {} # Now execute the check function to verify the variable is available p = site.execute(["cmk", "-nv", "modes-test-host"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "OK - (10.0, 20.0)" in stdout assert stderr == '' assert p.returncode == 0 # And now overwrite the setting in the config site.write_file("etc/check_mk/conf.d/test_check_1.mk", "test_check_1_default_levels = 5.0, 30.1\n") p = site.execute(["cmk", "-nv", "modes-test-host"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "OK - (10.0, 20.0)" not in stdout assert "OK - (5.0, 30.1)" in stdout assert stderr == '' assert p.returncode == 0
def test_check_factory_settings(request, test_cfg, site, web): test_check_path = "local/share/check_mk/checks/test_check_3" def cleanup(): if site.file_exists("etc/check_mk/conf.d/test_check_3.mk"): site.delete_file("etc/check_mk/conf.d/test_check_3.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, """ factory_settings["test_check_3_default_levels"] = { "param1": 123, } def inventory(info): return [(None, {})] def check(item, params, info): return 0, "OK - %r" % (params, ) check_info["test_check_3"] = { "check_function" : check, "inventory_function" : inventory, "service_description" : "Testcheck 3", "group" : "asd", "default_levels_variable" : "test_check_3_default_levels", } """) site.write_file("var/check_mk/agent_output/modes-test-host", "<<<test_check_3>>>\n1 2\n") config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)]) # Verify that the default variable is in the check context and # not in the global checks module context assert "test_check_3_default_levels" not in config.__dict__ assert "test_check_3" in config._check_contexts assert "test_check_3_default_levels" in config._check_contexts[ "test_check_3"] web.discover_services("modes-test-host") # Verify that the discovery worked as expected services = autochecks.parse_autochecks_file("modes-test-host", config.service_description) assert services[0].check_plugin_name == "test_check_3" assert services[0].item is None assert services[0].parameters_unresolved == "{}" assert services[0].service_labels.to_dict() == {} # Now execute the check function to verify the variable is available p = site.execute(["cmk", "-nv", "modes-test-host"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "OK - {'param1': 123}\n" in stdout assert stderr == '' assert p.returncode == 0 # And now overwrite the setting in the config site.write_file( "etc/check_mk/conf.d/test_check_3.mk", """ checkgroup_parameters.setdefault('asd', []) checkgroup_parameters['asd'] = [ ( {'param2': 'xxx'}, [], ALL_HOSTS, {} ), ] + checkgroup_parameters['asd'] """) # And execute the check again to check for the parameters p = site.execute(["cmk", "-nv", "modes-test-host"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "'param1': 123" in stdout assert "'param2': 'xxx'" in stdout assert stderr == '' assert p.returncode == 0
def test_test_check_1(request, site: Site, web): host_name = "check-variables-test-host" create_linux_test_host(request, site, host_name) site.write_text_file(f"var/check_mk/agent_output/{host_name}", "<<<test_check_1>>>\n1 2\n") test_check_path = "local/share/check_mk/checks/test_check_1" 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") site.delete_file(test_check_path) request.addfinalizer(cleanup) site.write_text_file( test_check_path, """ test_check_1_default_levels = 10.0, 20.0 def inventory(info): return [(None, "test_check_1_default_levels")] def check(item, params, info): return 0, "OK - %r" % (test_check_1_default_levels, ) check_info["test_check_1"] = { "check_function" : check, "inventory_function" : inventory, "service_description" : "Testcheck 1", # "default_levels_variable" : "test_check_1_default_levels" } """, ) config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)]) config.load(with_conf_d=False) site.activate_changes_and_wait_for_core_reload() # Verify that the default variable is in the check context and # not in the global checks module context. assert "test_check_1_default_levels" not in config.__dict__ assert "test_check_1" in config._check_contexts assert "test_check_1_default_levels" in config._check_contexts[ "test_check_1"] assert config._check_contexts["test_check_1"][ "test_check_1_default_levels"] == (10.0, 20.0) web.discover_services(host_name) # Replace with RestAPI call, see CMK-9249 # Verify that the discovery worked as expected entries = autochecks.AutochecksStore(HostName(host_name)).read() assert str(entries[0].check_plugin_name) == "test_check_1" assert entries[0].item is None assert entries[0].parameters == (10.0, 20.0) assert entries[0].service_labels == {} # Now execute the check function to verify the variable is available p = site.execute(["cmk", "-nv", host_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "OK - (10.0, 20.0)" in stdout assert stderr == "" assert p.returncode == 0 # And now overwrite the setting in the config site.write_text_file("etc/check_mk/conf.d/test_check_1.mk", "test_check_1_default_levels = 5.0, 30.1\n") p = site.execute(["cmk", "-nv", host_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "OK - (10.0, 20.0)" not in stdout assert "OK - (5.0, 30.1)" in stdout assert stderr == "" assert p.returncode == 0 # rediscover with the setting in the config site.delete_file(f"var/check_mk/autochecks/{host_name}.mk") web.discover_services(host_name) # Replace with RestAPI call, see CMK-9249 entries = autochecks.AutochecksStore(HostName(host_name)).read() assert entries[0].parameters == (5.0, 30.1)
def test_check_factory_settings(request, site: Site, web): host_name = "check-variables-test-host" create_linux_test_host(request, site, host_name) site.write_text_file(f"var/check_mk/agent_output/{host_name}", "<<<test_check_3>>>\n1 2\n") test_check_path = "local/share/check_mk/checks/test_check_3" def cleanup(): if site.file_exists("etc/check_mk/conf.d/test_check_3.mk"): site.delete_file("etc/check_mk/conf.d/test_check_3.mk") site.delete_file(test_check_path) request.addfinalizer(cleanup) site.write_text_file( test_check_path, """ factory_settings["test_check_3_default_levels"] = { "param1": 123, } def inventory(info): return [(None, {})] def check(item, params, info): return 0, "OK - %r" % (params, ) check_info["test_check_3"] = { "check_function" : check, "inventory_function" : inventory, "service_description" : "Testcheck 3", "group" : "asd", "default_levels_variable" : "test_check_3_default_levels", } """, ) config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)]) config.load(with_conf_d=False) site.activate_changes_and_wait_for_core_reload() # Verify that the default variable is in the check context and # not in the global checks module context assert "test_check_3_default_levels" not in config.__dict__ assert "test_check_3" in config._check_contexts assert "test_check_3_default_levels" in config._check_contexts[ "test_check_3"] web.discover_services(host_name) # Replace with RestAPI call, see CMK-9249 # Verify that the discovery worked as expected entries = autochecks.AutochecksStore(HostName(host_name)).read() assert str(entries[0].check_plugin_name) == "test_check_3" assert entries[0].item is None assert entries[0].parameters == {} assert entries[0].service_labels == {} # Now execute the check function to verify the variable is available p = site.execute(["cmk", "-nv", host_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "OK - {'param1': 123}\n" in stdout, stdout assert stderr == "" assert p.returncode == 0 # And now overwrite the setting in the config site.write_text_file( "etc/check_mk/conf.d/test_check_3.mk", """ checkgroup_parameters.setdefault('asd', []) checkgroup_parameters['asd'] = [ ( {'param2': 'xxx'}, [], ALL_HOSTS, {} ), ] + checkgroup_parameters['asd'] """, ) # And execute the check again to check for the parameters p = site.execute(["cmk", "-nv", host_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() assert "'param1': 123" in stdout assert "'param2': 'xxx'" in stdout assert stderr == "" assert p.returncode == 0
def test_test_check_2(request, site, web): host_name = "check-variables-test-host" create_linux_test_host(request, site, host_name) site.write_text_file(f"var/check_mk/agent_output/{host_name}", "<<<test_check_2>>>\n1 2\n") test_check_path = "local/share/check_mk/checks/test_check_2" def cleanup(): if site.file_exists("etc/check_mk/conf.d/test_check_2.mk"): site.delete_file("etc/check_mk/conf.d/test_check_2.mk") site.delete_file(test_check_path) request.addfinalizer(cleanup) site.write_text_file( test_check_path, """ discover_service = False def inventory(info): if discover_service: return [(None, {})] def check(item, params, info): return 0, "OK, discovered!" check_info["test_check_2"] = { "check_function" : check, "inventory_function" : inventory, "service_description" : "Testcheck 2", } """, ) config.load_checks(check_api.get_check_api_context, ["%s/%s" % (site.root, test_check_path)]) config.load(with_conf_d=False) site.activate_changes_and_wait_for_core_reload() # Verify that the default variable is in the check context and # not in the global checks module context assert "discover_service" not in config.__dict__ assert "test_check_2" in config._check_contexts assert "discover_service" in config._check_contexts["test_check_2"] web.discover_services(host_name) # Replace with RestAPI call, see CMK-9249 # Should have discovered nothing so far assert site.read_file( f"var/check_mk/autochecks/{host_name}.mk") == "[\n]\n" web.discover_services(host_name) # Replace with RestAPI call, see CMK-9249 # And now overwrite the setting in the config site.write_text_file("etc/check_mk/conf.d/test_check_2.mk", "discover_service = True\n") web.discover_services(host_name) # Replace with RestAPI call, see CMK-9249 # Verify that the discovery worked as expected entries = autochecks.AutochecksStore(HostName(host_name)).read() assert str(entries[0].check_plugin_name) == "test_check_2" assert entries[0].item is None assert entries[0].parameters == {} assert entries[0].service_labels == {}