def _find_usages_in_time_specific_parameters( self, tpname: str) -> List[TimeperiodUsage]: used_in: List[TimeperiodUsage] = [] rulesets = AllRulesets() rulesets.load() for ruleset in rulesets.get_rulesets().values(): vs = ruleset.valuespec() if not isinstance(vs, cmk.gui.plugins.wato.utils.TimeperiodValuespec): continue for rule_folder, rule_index, rule in ruleset.get_rules(): if not vs.is_active(rule.value): continue for index, (rule_tp_name, _value) in enumerate(rule.value["tp_values"]): if rule_tp_name != tpname: continue edit_url = folder_preserving_link([ ("mode", "edit_rule"), ("back_mode", "timeperiods"), ("varname", ruleset.name), ("rulenr", rule_index), ("rule_folder", rule_folder.path()), ]) used_in.append( (_("Time specific check parameter #%d") % (index + 1), edit_url)) return used_in
def _find_usages_of_group_in_rules(name, varnames): used_in = [] rulesets = AllRulesets() rulesets.load() for varname in varnames: ruleset = rulesets.get(varname) for _folder, _rulenr, rule in ruleset.get_rules(): if rule.value == name: used_in.append(("%s: %s" % (_("Ruleset"), ruleset.title()), folder_preserving_link([("mode", "edit_ruleset"), ("varname", varname)]))) return used_in
def _validate_deletion(self, ident, entry): rulesets = AllRulesets() rulesets.load() matched_rulesets = SearchedRulesets(rulesets, { "rule_predefined_condition": ident }).get_rulesets() if matched_rulesets: raise MKUserError( "_delete", _("You can not delete this %s because it is <a href=\"%s\">in use</a>.") % (self._mode_type.name_singular(), self._search_url(ident)))
def _find_usages_in_host_and_service_rules( self, tpname: str) -> List[TimeperiodUsage]: used_in: List[TimeperiodUsage] = [] rulesets = AllRulesets() rulesets.load() for varname, ruleset in rulesets.get_rulesets().items(): if not isinstance(ruleset.valuespec(), watolib.timeperiods.TimeperiodSelection): continue for _folder, _rulenr, rule in ruleset.get_rules(): if rule.value == tpname: used_in.append(( "%s: %s" % (_("Ruleset"), ruleset.title()), folder_preserving_link([("mode", "edit_ruleset"), ("varname", varname)]), )) break return used_in
def _save_service_enable_disable_rules(self, services, value): if not services: return rulesets = AllRulesets() rulesets.load() try: ruleset = rulesets.get("ignored_services") except KeyError: ruleset = Ruleset( "ignored_services", ruleset_matcher.get_tag_to_group_map(active_config.tags)) modified_folders = [] service_patterns: HostOrServiceConditions = [ service_description_to_condition(s) for s in services ] modified_folders += self._remove_from_rule_of_host(ruleset, service_patterns, value=not value) # Check whether or not the service still needs a host specific setting after removing # the host specific setting above and remove all services from the service list # that are fine without an additional change. for service in list(services): value_without_host_rule = ruleset.analyse_ruleset( self._host.name(), service, service)[0] if (not value and value_without_host_rule in [None, False]) or value == value_without_host_rule: services.remove(service) service_patterns = [ service_description_to_condition(s) for s in services ] modified_folders += self._update_rule_of_host(ruleset, service_patterns, value=value) for folder in modified_folders: rulesets.save_folder(folder)
def page(self): all_rulesets = AllRulesets() all_rulesets.load() for_host: bool = not self._service # Object type specific detail information if for_host: self._show_host_info() else: self._show_service_info(all_rulesets) last_maingroup = None for groupname in sorted( rulespec_group_registry.get_host_rulespec_group_names( for_host)): maingroup = groupname.split("/")[0] for rulespec in sorted(rulespec_registry.get_by_group(groupname), key=lambda x: x.title or ""): if (rulespec.item_type == "service") == (not self._service): continue # This rule is not for hosts/services # Open form for that group here, if we know that we have at least one rule if last_maingroup != maingroup: last_maingroup = maingroup rulegroup = get_rulegroup(maingroup) forms.header( rulegroup.title, isopen=maingroup == "monconf", narrow=True, css="rulesettings", ) html.help(rulegroup.help) self._output_analysed_ruleset(all_rulesets, rulespec, svc_desc_or_item=self._service, svc_desc=self._service) forms.end()
def delete_rule(param): """Delete a rule""" user.need_permission("wato.edit") user.need_permission("wato.rulesets") rule_id = param["rule_id"] rule: Rule all_rulesets = AllRulesets() all_rulesets.load() found = False for ruleset in all_rulesets.get_rulesets().values(): for _folder, _index, rule in ruleset.get_rules(): if rule.id == rule_id: ruleset.delete_rule(rule) all_rulesets.save() found = True if found: return http.Response(status=204) return problem( status=404, title="Rule not found.", detail=f"The rule with ID {rule_id!r} could not be found.", )
def _get_rule_by_id(rule_uuid: str, all_rulesets=None) -> RuleEntry: if all_rulesets is None: all_rulesets = AllRulesets() all_rulesets.load() for ruleset in all_rulesets.get_rulesets().values(): folder: CREFolder index: int rule: Rule for folder, index, rule in ruleset.get_rules(): if rule.id == rule_uuid: return RuleEntry( index_nr=index, rule=rule, folder=folder, ruleset=ruleset, all_rulesets=all_rulesets, ) raise ProblemException( status=400, title="Unknown rule.", detail=f"Rule with UUID {rule_uuid} was not found.", )
def list_rules(param): """List rules""" user.need_permission("wato.rulesets") all_rulesets = AllRulesets() all_rulesets.load() ruleset_name = param["ruleset_name"] try: ruleset = all_rulesets.get(ruleset_name.replace("-", ":")) except KeyError: return problem( status=400, title="Unknown ruleset.", detail=f"The ruleset of name {ruleset_name!r} is not known.", ) result = [] for folder, index, rule in ruleset.get_rules(): result.append( _serialize_rule( RuleEntry( rule=rule, ruleset=rule.ruleset, folder=folder, index_nr=index, all_rulesets=all_rulesets, ))) return serve_json( constructors.collection_object( domain_type="rule", value=result, extensions={ "found_rules": len(result), }, ))