示例#1
0
文件: events.py 项目: tribe29/checkmk
def _event_match_servicegroups(rule: EventRule, context: EventContext,
                               is_regex: bool) -> Optional[str]:
    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"
            )
        return None

    if required_groups is not None:
        sgn = context.get("SERVICEGROUPNAMES")
        if sgn is 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 = (config.define_servicegroups[sg]
                                   if match_type == "match_alias" else sg)
                    if r.search(match_value):
                        return None
            elif group in servicegroups:
                return None

        if is_regex:
            if match_type == "match_alias":
                return (
                    "The service is only in the groups %s. None of these patterns match: %s"
                    % (
                        '"' + '", "'.join(config.define_servicegroups[x]
                                          for x in servicegroups) + '"',
                        '"' + '" or "'.join(required_groups),
                    ) + '"')

            return (
                "The service is only in the groups %s. None of these patterns match: %s"
                % ('"' + '", "'.join(servicegroups) + '"',
                   '"' + '" or "'.join(required_groups)) + '"')

        return "The service is only in the groups %s, but %s is required" % (
            sgn,
            " or ".join(required_groups),
        )
    return None
示例#2
0
def event_match_contactgroups(rule: EventRule,
                              context: EventContext) -> Optional[str]:
    required_groups = rule.get("match_contactgroups")
    if required_groups is None:
        return None

    if context["WHAT"] == "SERVICE":
        cgn = context.get("SERVICECONTACTGROUPNAMES")
    else:
        cgn = context.get("HOSTCONTACTGROUPNAMES")

    if cgn is None:
        return None

    if not cgn:
        return "The object is in no group, but %s is required" % (
            " or ".join(required_groups))

    contactgroups = cgn.split(",")
    for group in required_groups:
        if group in contactgroups:
            return None

    return "The object is only in the groups %s, but %s is required" % (
        cgn, " or ".join(required_groups))
示例#3
0
def event_match_hosttags(rule: EventRule, context: EventContext) -> Optional[str]:
    required = rule.get("match_hosttags")
    if required:
        tags = context.get("HOSTTAGS", "").split()
        if not config.hosttags_match_taglist(tags, required):
            return "The host's tags %s do not match the required tags %s" % ("|".join(tags),
                                                                             "|".join(required))
    return None
示例#4
0
def event_match_exclude_services(rule: EventRule, context: EventContext) -> Optional[str]:
    if context["WHAT"] != "SERVICE":
        return None
    excludelist = rule.get("match_exclude_services", [])
    service = context["SERVICEDESC"]
    if config.in_extraconf_servicelist(excludelist, service):
        return ("The service's description '%s' matches the list of excluded services" %
                context["SERVICEDESC"])
    return None
示例#5
0
def _event_match_exclude_servicegroups(
    rule: EventRule, context: EventContext, is_regex: bool
) -> Optional[str]:
    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 None

    if excluded_groups is not None:
        context_sgn = context.get("SERVICEGROUPNAMES")
        if not context_sgn:
            # No actual groups means no possible negative match
            return None

        servicegroups = context_sgn.split(",")

        for group in excluded_groups:
            if is_regex:
                r = regex(group)
                for sg in servicegroups:
                    if config.define_servicegroups is None:
                        continue
                    match_value = (
                        config.define_servicegroups[sg] if match_type == "match_alias" else sg
                    )
                    match_value_inverse = (
                        sg if match_type == "match_alias" else config.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
    return None
示例#6
0
def event_match_hostgroups(rule: EventRule, context: EventContext) -> Optional[str]:
    required_groups = rule.get("match_hostgroups")
    if required_groups is not None:
        hgn = context.get("HOSTGROUPNAMES")
        if hgn is None:
            return ("No information about host groups is in the context, but host "
                    "must be in group %s" % (" or ".join(required_groups)))
        if hgn:
            hostgroups = hgn.split(",")
        else:
            return "The host is in no group, but %s is required" % (" or ".join(required_groups))

        for group in required_groups:
            if group in hostgroups:
                return None

        return "The host is only in the groups %s, but %s is required" % (
            hgn, " or ".join(required_groups))
    return None
示例#7
0
def event_match_exclude_hosts(rule: EventRule,
                              context: EventContext) -> Optional[str]:
    if context["HOSTNAME"] in rule.get("match_exclude_hosts", []):
        return "The host's name '%s' is on the list of excluded hosts" % context[
            "HOSTNAME"]
    return None
示例#8
0
def _used_in_notification_rule(name: str, rule: EventRule) -> bool:
    return name in rule.get("contact_groups", []) or name in rule.get(
        "match_contactgroups", [])