def wsgi(): LOG.info("Loading check_mk module...") watch = StopWatch() checks.load() config.load() count = len(checks.check_info) LOG.info("%d checks loaded in %s." % (count, watch.elapsed())) app = flask.Flask(__name__) @app.route("/check/<hostname>") @input_output_fixup def check(hostname): return checking.do_check(hostname, None, None) @app.route("/detail/<hostname>") @input_output_fixup def detail(hostname): try: cmk.debug.enable() cmk.log.set_verbosity(verbosity=2) return checking.do_check(hostname, None, None) finally: cmk.log.set_verbosity(verbosity=0) cmk.debug.disable() @app.route("/inventory/<hostname>") @input_output_fixup def inventory(hostname): return discovery.do_inv_check(hostname, None) return app
def reload_config(): # Needs to be done together, even when the checks are not directly needed import cmk_base.check_api as check_api config.load_all_checks(check_api.get_check_api_context) config.load() config_cache = config.get_config_cache() config_cache.initialize() return config_cache
def execute(self, cmd, args): 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_checks(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" % cmk.utils.make_utf8("%s" % e)) if cmk.utils.debug.enabled(): raise return 1 except Exception as e: if cmk.utils.debug.enabled(): raise console.error("%s\n" % cmk.utils.make_utf8("%s" % e)) return 2 finally: profiling.output_profile() if cmk.utils.debug.enabled(): console.output(pprint.pformat(result) + "\n") else: console.output("%r\n" % (result, )) return 0
import six from omdlib.main import SiteContext, site_name import cmk_base.autochecks import cmk_base.cee.rrd import cmk_base.config as config import cmk_base.check_api as check_api import cmk.utils import cmk.utils.store as store import cmk.utils.debug cmk.utils.debug.enable() config.load_all_checks(check_api.get_check_api_context) config.load() logger = logging.getLogger("RRD INFO Metric Migration") logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) CHECKS_USING_DF_INCLUDE = [ "3par_capacity", "3par_cpgs", "3par_cpgs.usage", "3par_system", "3par_volumes", "ceph_df", "datapower_fs", "db2_logsizes", "dell_compellent_folder", "df", "df_netapp", "df_netapp32", "df_netscaler", "df_zos", "emc_datadomain_fs", "emc_isilon_ifs", "emc_isilon_quota", "emcvnx_raidgroups.capacity", "emcvnx_raidgroups.capacity_contiguous", "esx_vsphere_counters.ramdisk", "esx_vsphere_datastores", "fast_lta_silent_cubes.capacity", "fast_lta_volumes", "hitachi_hnas_span", "hitachi_hnas_volume", "hp_msa_volume.df", "hr_fs", "ibm_svc_mdiskgrp", "k8s_stats.fs", "libelle_business_shadow.archive_dir", "lvm_vgs", "mgmt_hr_fs", "netapp_api_aggr", "netapp_api_luns", "netapp_api_qtree_quota", "netapp_api_volumes", "nimble_volumes", "oracle_asm_diskgroup", "prism_storage_pools",
def test_cfg(web, site): print("Applying default config") web.add_host("ds-test-host1", attributes={ "ipaddress": "127.0.0.1", }) web.add_host("ds-test-host2", attributes={ "ipaddress": "127.0.0.1", }) web.add_host("ds-test-node1", attributes={ "ipaddress": "127.0.0.1", }) web.add_host("ds-test-node2", attributes={ "ipaddress": "127.0.0.1", }) web.add_host( "ds-test-cluster1", attributes={ "ipaddress": "127.0.0.1", }, cluster_nodes=[ "ds-test-node1", "ds-test-node2", ], ) site.write_file( "etc/check_mk/conf.d/ds-test-host.mk", "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ALL_HOSTS))\n" ) site.makedirs("var/check_mk/agent_output/") for h in [ "ds-test-host1", "ds-test-host2", "ds-test-node1", "ds-test-node2" ]: site.write_file( "var/check_mk/agent_output/%s" % h, file( "%s/tests/integration/cmk_base/test-files/linux-agent-output" % repo_path()).read()) web.activate_changes() import cmk.utils.debug cmk.utils.debug.enable() # Needs to be done together, even when the checks are not directly needed import cmk_base.check_api as check_api config.load_all_checks(check_api.get_check_api_context) config.load() yield None # # Cleanup code # print("Cleaning up test config") cmk.utils.debug.disable() site.delete_dir("var/check_mk/agent_output") site.delete_file("etc/check_mk/conf.d/ds-test-host.mk") web.delete_host("ds-test-host1") web.delete_host("ds-test-host2") web.delete_host("ds-test-node1") web.delete_host("ds-test-node2") web.delete_host("ds-test-cluster1") web.activate_changes()
def _load_config(): global _config_loaded if not _config_loaded: config.load(validate_hosts=False) _config_loaded = True
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") 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_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() == {}