Example #1
0
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
Example #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))
Example #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
Example #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
Example #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
Example #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
Example #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
Example #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", [])
Example #9
0
    def generate(self) -> None:
        save_global_settings(self._initial_global_settings())

        # A contact group for all hosts and services
        groups: AllGroupSpecs = {
            "contact": {
                GroupName("all"): {
                    "alias": "Everything"
                }
            },
        }
        save_group_information(groups)

        self._initialize_tag_config()

        # Rules that match the upper host tag definition
        ruleset_config = {
            # Make the tag 'offline' remove hosts from the monitoring
            "only_hosts": [
                {
                    "id": "10843c55-11ea-4eb2-bfbc-bce65cd2ae22",
                    "condition": {
                        "host_tags": {
                            "criticality": {
                                "$ne": "offline"
                            }
                        }
                    },
                    "value": True,
                    "options": {
                        "description":
                        'Do not monitor hosts with the tag "offline"'
                    },
                },
            ],
            # Rule for WAN hosts with adapted PING levels
            "ping_levels": [
                {
                    "id": "0365b634-30bf-40a3-8516-08e86051508e",
                    "condition": {
                        "host_tags": {
                            "networking": "wan",
                        }
                    },
                    "value": {
                        "loss": (80.0, 100.0),
                        "packets": 6,
                        "timeout": 20,
                        "rta": (1500.0, 3000.0),
                    },
                    "options": {
                        "description":
                        "Allow longer round trip times when pinging WAN hosts"
                    },
                },
            ],
            # All hosts should use SNMP v2c if not specially tagged
            "bulkwalk_hosts": [
                {
                    "id": "b92a5406-1d57-4f1d-953d-225b111239e5",
                    "condition": {
                        "host_tags": {
                            "snmp": "snmp",
                            "snmp_ds": {
                                "$ne": "snmp-v1"
                            },
                        },
                    },
                    "value": True,
                    "options": {
                        "description":
                        'Hosts with the tag "snmp-v1" must not use bulkwalk'
                    },
                },
            ],
            # All SNMP managment boards should use SNMP v2c if not specially tagged
            "management_bulkwalk_hosts": [
                {
                    "id": "59d84cde-ee3a-4f8d-8bec-fce35a2b0d15",
                    "condition": {},
                    "value": True,
                    "options": {
                        "description":
                        "All management boards use SNMP v2 (incl. bulk walks) by default"
                    },
                },
            ],
            # Put all hosts and the contact group 'all'
            "host_contactgroups": [
                {
                    "id": "efd67dab-68f8-4d3c-a417-9f7e29ab48d5",
                    "condition": {},
                    "value": "all",
                    "options": {
                        "description":
                        'Put all hosts into the contact group "all"'
                    },
                },
            ],
            # Docker container specific host check commands
            "host_check_commands": [
                {
                    "id": "24da4ccd-0d1b-40e3-af87-0097df8668f2",
                    "condition": {
                        "host_labels": {
                            "cmk/docker_object": "container"
                        }
                    },
                    "value": ("service", "Docker container status"),
                    "options": {
                        "description":
                        'Make all docker container host states base on the "Docker container status" service',
                    },
                },
            ],
            # Enable HW/SW inventory + status data inventory for docker
            # containers, kubernetes objects and Check-MK servers by default to
            # simplify the setup procedure for them
            "active_checks": {
                "cmk_inv": [
                    {
                        "id": "7ba2ac2a-5a49-47ce-bc3c-1630fb191c7f",
                        "condition": {
                            "host_labels": {
                                "cmk/docker_object": "node",
                            }
                        },
                        "value": {
                            "status_data_inventory": True
                        },
                    },
                    {
                        "id": "b4b151f9-c7cc-4127-87a6-9539931fcd73",
                        "condition": {
                            "host_labels": {
                                "cmk/check_mk_server": "yes",
                            }
                        },
                        "value": {
                            "status_data_inventory": True
                        },
                    },
                    {
                        "id": "2527cb37-e9da-4a15-a7d9-80825a7f6661",
                        "condition": {
                            "host_labels": {
                                "cmk/kubernetes": "yes",
                            }
                        },
                        "value": {
                            "status_data_inventory": True
                        },
                    },
                ]
            },
            # Interval for HW/SW-Inventory check
            "extra_service_conf": {
                "check_interval": [
                    {
                        "id": "b3847203-84b3-4f5b-ac67-0f06d4403905",
                        "condition": {
                            "service_description": [{
                                "$regex":
                                "Check_MK HW/SW Inventory$"
                            }]
                        },
                        "value": 1440,
                        "options": {
                            "description":
                            "Restrict HW/SW-Inventory to once a day"
                        },
                    },
                ],
            },
            # Disable unreachable notifications by default
            "extra_host_conf": {
                "notification_options": [
                    {
                        "id": "814bf932-6341-4f96-983d-283525b5416d",
                        "condition": {},
                        "value": "d,r,f,s",
                    },
                ],
            },
            # Periodic service discovery
            "periodic_discovery": [
                {
                    "id": "95a56ffc-f17e-44e7-a162-be656f19bedf",
                    "condition": {},
                    "value": {
                        "severity_unmonitored": 1,
                        "severity_vanished": 0,
                        "severity_new_host_label": 1,
                        "check_interval": 120.0,
                    },
                    "options": {
                        "description":
                        "Perform every two hours a service discovery"
                    },
                },
            ],
            # Include monitoring of checkmk's tmpfs
            "inventory_df_rules": [
                {
                    "id": "b0ee8a51-703c-47e4-aec4-76430281604d",
                    "condition": {
                        "host_labels": {
                            "cmk/check_mk_server": "yes",
                        },
                    },
                    "value": {
                        "ignore_fs_types":
                        ["tmpfs", "nfs", "smbfs", "cifs", "iso9660"],
                        "never_ignore_mountpoints":
                        ["~.*/omd/sites/[^/]+/tmp$"],
                    },
                },
            ],
        }

        rulesets = FolderRulesets(Folder.root_folder())
        rulesets.load()
        rulesets.from_config(Folder.root_folder(), ruleset_config)
        rulesets.save()

        notification_rules = [
            EventRule(
                allow_disable=True,
                contact_all=False,
                contact_all_with_email=False,
                contact_object=True,
                description=
                "Notify all contacts of a host/service via HTML email",
                disabled=False,
                notify_plugin=("mail", {}),
            ),
        ]
        save_notification_rules(notification_rules)