def num_split(s): if not s: return () elif s[0].isdigit(): first_num = regex("[^0-9]").split(s)[0] return (int(first_num), ) + num_split(s[len(first_num):]) else: first_word = regex("[0-9]").split(s)[0] return (first_word.lower(), ) + num_split(s[len(first_word):])
def num_split(s): if not s: return () elif s[0].isdigit(): first_num = regex("[^0-9]").split(s)[0] return ( int(first_num), ) + num_split(s[len(first_num):]) else: first_word = regex("[0-9]").split(s)[0] return ( first_word.lower(), ) + num_split(s[len(first_word):])
def event_match_exclude_servicegroups(rule, context, is_regex=False): if is_regex: match_type, excluded_groups = rule.get( "match_exclude_servicegroups_regex", (None, None)) else: excluded_groups = rule.get("match_exclude_servicegroups") if context["WHAT"] != "SERVICE": # excluded_groups do not apply to a host notification return if excluded_groups != None: context_sgn = context.get("SERVICEGROUPNAMES") if context_sgn == None: # No actual groups means no possible negative match return servicegroups = context_sgn.split(",") for group in excluded_groups: if is_regex: r = regex(group) for sg in servicegroups: match_value = match_type == "match_alias" and define_servicegroups[ sg] or sg match_value_inverse = match_type == "match_alias" and sg or define_servicegroups[ sg] if r.search(match_value): return "The service group \"%s\" (%s) is excluded per regex pattern: %s" %\ (match_value, match_value_inverse, group) elif group in servicegroups: return "The service group %s is excluded" % group
def convert_pattern(pattern): def is_infix_string_search(pattern): return pattern.startswith('.*') and not is_regex(pattern[2:]) def is_exact_match(pattern): return pattern[-1] == '$' and not is_regex(pattern[:-1]) def is_prefix_match(pattern): return pattern[-2:] == '.*' and not is_regex(pattern[:-2]) if pattern == '': return False, lambda txt: True # empty patterns match always negate, pattern = parse_negated(pattern) if is_exact_match(pattern): # Exact string match return negate, lambda txt: pattern[:-1] == txt elif is_infix_string_search(pattern): # Using regex to search a substring within text return negate, lambda txt: pattern[2:] in txt elif is_prefix_match(pattern): # prefix match with tailing .* pattern = pattern[:-2] return negate, lambda txt: txt[:len(pattern)] == pattern elif is_regex(pattern): # Non specific regex. Use real prefix regex matching return negate, lambda txt: regex(pattern).match(txt) != None else: # prefix match without any regex chars return negate, lambda txt: txt[:len(pattern)] == pattern
def event_match_exclude_servicegroups(rule, context, is_regex = False): if is_regex: match_type, excluded_groups = rule.get("match_exclude_servicegroups_regex", (None, None)) else: excluded_groups = rule.get("match_exclude_servicegroups") if context["WHAT"] != "SERVICE": # excluded_groups do not apply to a host notification return if excluded_groups != None: context_sgn = context.get("SERVICEGROUPNAMES") if context_sgn == None: # No actual groups means no possible negative match return servicegroups = context_sgn.split(",") for group in excluded_groups: if is_regex: r = regex(group) for sg in servicegroups: match_value = match_type == "match_alias" and define_servicegroups[sg] or sg match_value_inverse = match_type == "match_alias" and sg or define_servicegroups[sg] if r.search(match_value): return "The service group \"%s\" (%s) is excluded per regex pattern: %s" %\ (match_value, match_value_inverse, group) elif group in servicegroups: return "The service group %s is excluded" % group
def event_match_plugin_output(rule, context): if "match_plugin_output" in rule: r = regex(rule["match_plugin_output"]) if context["WHAT"] == "SERVICE": output = context["SERVICEOUTPUT"] else: output = context["HOSTOUTPUT"] if not r.search(output): return "The expression '%s' cannot be found in the plugin output '%s'" % \ (rule["match_plugin_output"], output)
def event_match_servicegroups(rule, context, is_regex=False): if is_regex: match_type, required_groups = rule.get("match_servicegroups_regex", (None, None)) else: required_groups = rule.get("match_servicegroups") if context["WHAT"] != "SERVICE": if required_groups: return "This rule requires membership in a service group, but this is a host notification" else: return if required_groups != None: sgn = context.get("SERVICEGROUPNAMES") if sgn == None: return "No information about service groups is in the context, but service " \ "must be in group %s" % ( " or ".join(required_groups)) if sgn: servicegroups = sgn.split(",") else: return "The service is in no service group, but %s%s is required" % ( (is_regex and "regex " or ""), " or ".join(required_groups)) for group in required_groups: if is_regex: r = regex(group) for sg in servicegroups: match_value = match_type == "match_alias" and define_servicegroups[ sg] or sg if r.search(match_value): return elif group in servicegroups: return if is_regex: if match_type == "match_alias": return "The service is only in the groups %s. None of these patterns match: %s" % ( '"' + '", "'.join( map(lambda x: define_servicegroups[x], servicegroups)) + '"', '"' + '" or "'.join(required_groups)) + '"' else: return "The service is only in the groups %s. None of these patterns match: %s" % ( '"' + '", "'.join(servicegroups) + '"', '"' + '" or "'.join(required_groups)) + '"' else: return "The service is only in the groups %s, but %s is required" % ( sgn, " or ".join(required_groups))
def event_match_servicegroups(rule, context, is_regex = False): if is_regex: match_type, required_groups = rule.get("match_servicegroups_regex", (None, None)) else: required_groups = rule.get("match_servicegroups") if context["WHAT"] != "SERVICE": if required_groups: return "This rule requires membership in a service group, but this is a host notification" else: return if required_groups != None: sgn = context.get("SERVICEGROUPNAMES") if sgn == None: return "No information about service groups is in the context, but service " \ "must be in group %s" % ( " or ".join(required_groups)) if sgn: servicegroups = sgn.split(",") else: return "The service is in no service group, but %s%s is required" % ((is_regex and "regex " or ""), " or ".join(required_groups)) for group in required_groups: if is_regex: r = regex(group) for sg in servicegroups: match_value = match_type == "match_alias" and define_servicegroups[sg] or sg if r.search(match_value): return elif group in servicegroups: return if is_regex: if match_type == "match_alias": return "The service is only in the groups %s. None of these patterns match: %s" % ( '"' + '", "'.join(map(lambda x: define_servicegroups[x], servicegroups)) + '"', '"' + '" or "'.join(required_groups)) + '"' else: return "The service is only in the groups %s. None of these patterns match: %s" % ( '"' + '", "'.join(servicegroups) + '"', '"' + '" or "'.join(required_groups)) + '"' else: return "The service is only in the groups %s, but %s is required" % ( sgn, " or ".join(required_groups))