def load_custom_attrs_from_mk_file(lock): filename = os.path.join(watolib.multisite_dir(), "custom_attrs.mk") vars_ = store.load_mk_file(filename, { 'wato_user_attrs': [], 'wato_host_attrs': [], }, lock=lock) attrs = {} for what in ["user", "host"]: attributes = vars_.get("wato_%s_attrs" % what, []) if what == "host": attributes = transform_pre_16_host_topics(attributes) attrs[what] = attributes return attrs
def _load_profiling_setting() -> Union[bool, Literal["enable_by_var"]]: """Load the profiling global setting from the GUI config This is a small hack to get access to the current configuration without the need to load the whole GUI config. The problem is: The profiling setting is needed before the GUI config is loaded regularly. This is needed, because we want to be able to profile our whole WSGI app, including the config loading logic. We only process the WATO written global settings file to get the WATO settings. Which should be enough for the most cases. """ settings = store.load_mk_file(cmk.utils.paths.default_config_dir + "/multisite.d/wato/global.mk", default={}) return settings.get("profile", False)
def test_openapi_create_rule(logged_in_admin_wsgi_app, new_rule): wsgi_app = logged_in_admin_wsgi_app base = "/NO_SITE/check_mk/api/1.0" values, new_resp = new_rule resp = wsgi_app.get( base + f"/objects/ruleset/{values['ruleset']}", headers={"Accept": "application/json"}, status=200, ) assert resp.json["extensions"]["number_of_rules"] == 1 # Also fetch the newly created rule and check if it's actually persisted. resp = wsgi_app.get( base + f"/objects/rule/{new_resp.json['id']}", headers={"Accept": "application/json"}, status=200, ) ext = resp.json["extensions"] assert ext["ruleset"] == values["ruleset"] assert ext["folder"] == values["folder"] assert ext["properties"] == values["properties"] assert ext["conditions"].items() >= values["conditions"].items() # Check that the format on disk is as expected. rules_mk = os.path.join(paths.omd_root, "etc", "check_mk", "conf.d", "wato", "rules.mk") environ = load_mk_file(rules_mk, default={}) stored_condition = environ[values["ruleset"]][0]["condition"] expected_condition = { "host_tags": { "criticality": "prod", "networking": { "$ne": "wan" } }, "host_labels": { "os": "windows" }, } assert stored_condition == expected_condition
def load(self, site_specific=False, custom_site_path=None): cleanup_settings = store.load_mk_file(self.diskspace_config, default={}) if not cleanup_settings: return {} # Convert old config (min_free_bytes and min_file_age) were independent options if "min_free_bytes" in cleanup_settings: cleanup_settings["min_free_bytes"] = (cleanup_settings["min_free_bytes"], cleanup_settings.pop("min_file_age", 2592000)) # 1 month if cleanup_settings.get("cleanup_abandoned_host_files", False) is None: del cleanup_settings["cleanup_abandoned_host_files"] if cleanup_settings.get("max_file_age", False) is None: del cleanup_settings["max_file_age"] return { "diskspace_cleanup": cleanup_settings, }
def _load_folder_rulesets(self, folder, only_varname=None): path = folder.rules_file_path() config_dict = { "ALL_HOSTS": ALL_HOSTS, "ALL_SERVICES": [""], "NEGATE": NEGATE, "FOLDER_PATH": folder.path(), } # Prepare empty rulesets so that rules.mk has something to # append to. We need to initialize all variables here, even # when only loading with only_varname. for varname in rulespec_registry.keys(): if ':' in varname: dictname, _subkey = varname.split(":") config_dict[dictname] = {} else: config_dict[varname] = [] self.from_config(folder, store.load_mk_file(path, config_dict), only_varname)
def test_save_mk_file(tmp_path, path_type): path = path_type(tmp_path.joinpath("lala")) store.save_mk_file(path, "x = 1") assert store.load_mk_file(path, default={}) == {"x": 1}
def test_load_mk_file(tmp_path, path_type): locked_file = tmp_path / "test" locked_file.write_bytes("# encoding: utf-8\nabc = 'äbc'\n") config = store.load_mk_file(path_type(locked_file), default={}) assert config["abc"] == "äbc"
def test_save_to_mk_file(tmp_path, path_type): path = path_type(tmp_path / "huhu") store.save_to_mk_file(path, "x", {"a": 1}) assert store.load_mk_file(path, default={"x": {"a": 2, "y": 1}}) == {"x": {"a": 1, "y": 1}}