def default_cfg_fixture(request, site: Site, web) -> None: site.ensure_running() print("Applying default config") create_linux_test_host(request, site, "livestatus-test-host") create_linux_test_host(request, site, "livestatus-test-host.domain") web.discover_services("livestatus-test-host") # Replace with RestAPI call, see CMK-9249 site.activate_changes_and_wait_for_core_reload()
def configure_service_tags_fixture(site: Site, default_cfg): site.openapi.create_host( "modes-test-host", attributes={ "ipaddress": "127.0.0.1", }, ) rule_id = site.openapi.create_rule( ruleset_name="service_tag_rules", value=[("criticality", "prod")], conditions={ "host_name": { "match_on": ["livestatus-test-host"], "operator": "one_of", }, "service_description": { "match_on": ["CPU load$"], "operator": "one_of", }, }, ) site.activate_changes_and_wait_for_core_reload() yield site.openapi.delete_rule(rule_id) site.activate_changes_and_wait_for_core_reload()
def test_active_check_execution(site: Site, web): rule_id = site.openapi.create_rule( ruleset_name="custom_checks", value={ "service_description": "\xc4ctive-Check", "command_line": 'echo "123"', }, ) try: site.activate_changes_and_wait_for_core_reload() site.schedule_check("test-host", "\xc4ctive-Check", 0) result = site.live.query_row( "GET services\nColumns: host_name description state plugin_output has_been_checked\nFilter: host_name = test-host\nFilter: description = \xc4ctive-Check" ) print("Result: %r" % result) assert result[4] == 1 assert result[0] == "test-host" assert result[1] == "\xc4ctive-Check" assert result[2] == 0 assert result[3] == "123" finally: site.openapi.delete_rule(rule_id) site.activate_changes_and_wait_for_core_reload()
def cfg_setup_fixture(request, web, site: Site): hostname = "test-prediction" # Enforce use of the pre-created RRD file from the git. The restart of the core # is needed to make it renew it's internal RRD file cache site.makedirs("var/check_mk/rrd/test-prediction") with open(site.path("var/check_mk/rrd/test-prediction/CPU_load.rrd"), "wb") as f: f.write( Path( repo_path(), "tests", "integration", "cmk", "base", "test-files", "CPU_load.rrd" ).read_bytes() ) site.write_text_file( "var/check_mk/rrd/test-prediction/CPU_load.info", Path( repo_path(), "tests", "integration", "cmk", "base", "test-files", "CPU_load.info" ).read_text(), ) site.restart_core() create_linux_test_host(request, site, "test-prediction") site.write_text_file( "etc/check_mk/conf.d/linux_test_host_%s_cpu_load.mk" % hostname, """ globals().setdefault('custom_checks', []) custom_checks = [ ( {'service_description': u'CPU load', 'has_perfdata': True}, [], ALL_HOSTS, {} ), ] + custom_checks """, ) site.activate_changes_and_wait_for_core_reload() yield # Cleanup site.delete_file("etc/check_mk/conf.d/linux_test_host_%s_cpu_load.mk" % hostname) site.activate_changes_and_wait_for_core_reload() site.delete_dir("var/check_mk/rrd")
def test_active_check_execution(site: Site, web): try: web.set_ruleset( # Replace with RestAPI, see CMK-9251 "custom_checks", { "ruleset": { # Main folder "": [ { "value": { "service_description": "\xc4ctive-Check", "command_line": 'echo "123"', }, "condition": {}, "options": {}, }, ], } }, ) site.activate_changes_and_wait_for_core_reload() site.schedule_check("test-host", "\xc4ctive-Check", 0) result = site.live.query_row( "GET services\nColumns: host_name description state plugin_output has_been_checked\nFilter: host_name = test-host\nFilter: description = \xc4ctive-Check" ) print("Result: %r" % result) assert result[4] == 1 assert result[0] == "test-host" assert result[1] == "\xc4ctive-Check" assert result[2] == 0 assert result[3] == "123" finally: web.set_ruleset( # Replace with RestAPI, see CMK-9251 "custom_checks", { "ruleset": { "": [], # -> folder } }, ) site.activate_changes_and_wait_for_core_reload()
def test_cfg_fixture(site: Site) -> Iterator[None]: print("Applying default config") site.openapi.create_host( "test-host", attributes={ "ipaddress": "127.0.0.1", "tag_agent": "no-agent", }, ) site.activate_changes_and_wait_for_core_reload() yield # # Cleanup code # print("Cleaning up test config") site.openapi.delete_host("test-host")
def test_log_fixture(site: Site, fake_sendmail) -> Iterator[WatchLog]: users = { "hh": { "fullname": "Harry Hirsch", "password": "******", "email": f"{site.id}@localhost", "contactgroups": ["all"], }, } initial_users = site.openapi.get_all_users() assert len(initial_users) == 2 # expect cmkadmin and automation user for name, user_dict in users.items(): site.openapi.create_user(username=name, **user_dict) # type: ignore all_users = site.openapi.get_all_users() assert len(all_users) == len(initial_users) + len(users) site.live.command("[%d] STOP_EXECUTING_HOST_CHECKS" % time.time()) site.live.command("[%d] STOP_EXECUTING_SVC_CHECKS" % time.time()) site.openapi.create_host( "notify-test", attributes={ "ipaddress": "127.0.0.1", }, ) site.activate_changes_and_wait_for_core_reload() with WatchLog(site, default_timeout=20) as l: yield l site.live.command("[%d] START_EXECUTING_HOST_CHECKS" % time.time()) site.live.command("[%d] START_EXECUTING_SVC_CHECKS" % time.time()) site.openapi.delete_host("notify-test") for username in users: site.openapi.delete_user(username) site.activate_changes_and_wait_for_core_reload()
def test_cfg_fixture(site: Site, web): print("Applying default config") site.openapi.create_host( "modes-test-host", attributes={ "ipaddress": "127.0.0.1", }, ) site.openapi.create_host( "modes-test-host2", attributes={ "ipaddress": "127.0.0.1", "tag_criticality": "test", }, ) site.openapi.create_host( "modes-test-host3", attributes={ "ipaddress": "127.0.0.1", "tag_criticality": "test", }, ) site.openapi.create_host( "modes-test-host4", attributes={ "ipaddress": "127.0.0.1", "tag_criticality": "offline", }, ) site.write_text_file( "etc/check_mk/conf.d/modes-test-host.mk", "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ALL_HOSTS))\n", ) site.makedirs("var/check_mk/agent_output/") site.write_text_file("var/check_mk/agent_output/modes-test-host", get_standard_linux_agent_output()) site.write_text_file("var/check_mk/agent_output/modes-test-host2", get_standard_linux_agent_output()) site.write_text_file("var/check_mk/agent_output/modes-test-host3", get_standard_linux_agent_output()) web.discover_services( "modes-test-host") # Replace with RestAPI call, see CMK-9249 web.discover_services( "modes-test-host2") # Replace with RestAPI call, see CMK-9249 web.discover_services( "modes-test-host3") # Replace with RestAPI call, see CMK-9249 try: site.activate_changes_and_wait_for_core_reload() yield None finally: # # Cleanup code # print("Cleaning up test config") site.delete_dir("var/check_mk/agent_output") site.delete_file("etc/check_mk/conf.d/modes-test-host.mk") site.openapi.delete_host("modes-test-host") site.openapi.delete_host("modes-test-host2") site.openapi.delete_host("modes-test-host3") site.openapi.delete_host("modes-test-host4") site.activate_changes_and_wait_for_core_reload()
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 unreachable_enabled_fixture(request, web, site: Site): unreachable_enabled = request.param try: print("Applying test config") site.openapi.create_host( "notify-test-parent", attributes={ "ipaddress": "127.0.0.1", }, ) site.openapi.create_host( "notify-test-child", attributes={ "ipaddress": "127.0.0.1", "parents": ["notify-test-parent"], }, ) if unreachable_enabled: notification_options = "d,u,r,f,s" else: notification_options = "d,r,f,s" # Replace with RestAPI, see CMK-9251 rule_result = web.get_ruleset("extra_host_conf:notification_options") rule_result["ruleset"] = { "": [{ "condition": {}, "options": {}, "value": notification_options }] } # Replace with RestAPI, see CMK-9251 web.set_ruleset("extra_host_conf:notification_options", rule_result) site.activate_changes_and_wait_for_core_reload() site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-parent" % time.time()) site.live.command("[%d] DISABLE_SVC_CHECK;notify-test-parent;PING" % time.time()) site.live.command( "[%d] DISABLE_SVC_CHECK;notify-test-parent;Check_MK Discovery" % time.time()) site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-child" % time.time()) site.live.command("[%d] DISABLE_SVC_CHECK;notify-test-child;PING" % time.time()) site.live.command( "[%d] DISABLE_SVC_CHECK;notify-test-child;Check_MK Discovery" % time.time()) site.live.command("[%d] DISABLE_FLAP_DETECTION" % time.time()) yield unreachable_enabled finally: # # Cleanup code # print("Cleaning up default config") site.live.command("[%d] ENABLE_FLAP_DETECTION" % time.time()) site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-child" % time.time()) site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-parent" % time.time()) site.openapi.delete_host("notify-test-child") site.openapi.delete_host("notify-test-parent") site.activate_changes_and_wait_for_core_reload()
def unreachable_enabled_fixture(request, web, site: Site): unreachable_enabled = request.param rule_id = None try: print("Applying test config") site.openapi.create_host( "notify-test-parent", attributes={ "ipaddress": "127.0.0.1", }, ) site.openapi.create_host( "notify-test-child", attributes={ "ipaddress": "127.0.0.1", "parents": ["notify-test-parent"], }, ) if unreachable_enabled: notification_options = "d,u,r,f,s" else: notification_options = "d,r,f,s" for rule_spec in site.openapi.get_rules("extra_host_conf:notification_options"): site.openapi.delete_rule(rule_spec["id"]) rule_id = site.openapi.create_rule( ruleset_name="extra_host_conf:notification_options", value=notification_options, ) site.activate_changes_and_wait_for_core_reload() site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-parent" % time.time()) site.live.command("[%d] DISABLE_SVC_CHECK;notify-test-parent;PING" % time.time()) site.live.command( "[%d] DISABLE_SVC_CHECK;notify-test-parent;Check_MK Discovery" % time.time() ) site.live.command("[%d] DISABLE_HOST_CHECK;notify-test-child" % time.time()) site.live.command("[%d] DISABLE_SVC_CHECK;notify-test-child;PING" % time.time()) site.live.command( "[%d] DISABLE_SVC_CHECK;notify-test-child;Check_MK Discovery" % time.time() ) site.live.command("[%d] DISABLE_FLAP_DETECTION" % time.time()) yield unreachable_enabled finally: # # Cleanup code # print("Cleaning up default config") site.live.command("[%d] ENABLE_FLAP_DETECTION" % time.time()) site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-child" % time.time()) site.live.command("[%d] ENABLE_HOST_CHECK;notify-test-parent" % time.time()) if rule_id is not None: site.openapi.delete_rule(rule_id) site.openapi.delete_host("notify-test-child") site.openapi.delete_host("notify-test-parent") site.activate_changes_and_wait_for_core_reload()
def test_test_check_1_all_rule(request, site: Site, web): host_name = "disco-params-test-host" create_linux_test_host(request, site, host_name) site.write_text_file("var/check_mk/agent_output/disco-params-test-host", "<<<test_check_2>>>\n1 2\n") test_check_path = "local/lib/check_mk/base/plugins/agent_based/test_check_2.py" 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, """ 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_2", discovery_function=discover, discovery_ruleset_name="discover_test_check_2", discovery_ruleset_type=register.RuleSetType.ALL, discovery_default_parameters={"default": 42}, check_function=check, service_name="Foo %s", ) """, ) site.activate_changes_and_wait_for_core_reload() 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() for entry in entries: if str(entry.check_plugin_name) == "test_check_2": assert entry.item == "[Parameters({'default': 42})]" break else: raise AssertionError('"test_check_2" not discovered') # And now overwrite the setting in the config site.write_text_file( "etc/check_mk/conf.d/test_check_2.mk", "discover_test_check_2 = [{'value': {'levels': (1, 2)}, 'condition': {}}]\n", ) # 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() for entry in entries: if str(entry.check_plugin_name) == "test_check_2": assert entry.item == ("[Parameters({'levels': (1, 2)})," " Parameters({'default': 42})]") break else: raise AssertionError('"test_check_2" not discovered')