예제 #1
0
def test_service_depends_on(monkeypatch):
    ts = Scenario().add_host("test-host")
    ts.set_ruleset("service_dependencies", [
        ("dep1", [], config.ALL_HOSTS, ["svc1"], {}),
        ("dep2-%s", [], config.ALL_HOSTS, ["svc1-(.*)"], {}),
        ("dep-disabled", [], config.ALL_HOSTS, ["svc1"], {
            "disabled": True
        }),
    ])
    ts.apply(monkeypatch)

    assert config.service_depends_on("test-host", "svc2") == []
    assert config.service_depends_on("test-host", "svc1") == ["dep1"]
    assert config.service_depends_on("test-host", "svc1-abc") == ["dep1", "dep2-abc"]
예제 #2
0
파일: check_table.py 프로젝트: m4c3/checkMK
    def _handle_entry(self, checkname, item, params):
        # type: (str, Text, Any) -> CheckTable
        check_table = {}  # type: CheckTable
        hostname = self._host_config.hostname

        if not self._is_checkname_valid(checkname):
            return {}

        descr = config.service_description(hostname, checkname, item)
        if self.skip_ignored and config.service_ignored(
                hostname, checkname, descr):
            return {}

        if not self._host_config.part_of_clusters:
            svc_is_mine = True
        else:
            svc_is_mine = hostname == self._config_cache.host_of_clustered_service(
                hostname,
                descr,
                part_of_clusters=self._host_config.part_of_clusters)

        if self.filter_mode is None and not svc_is_mine:
            return {}

        elif self.filter_mode == "only_clustered" and svc_is_mine:
            return {}

        deps = config.service_depends_on(hostname, descr)
        check_table[(checkname, item)] = (params, descr, deps)

        return check_table
예제 #3
0
    def handle_entry(entry):
        num_elements = len(entry)
        if num_elements == 3:  # from autochecks
            hostlist = hostname
            checkname, item, params = entry
            tags = []
        elif num_elements == 4:
            hostlist, checkname, item, params = entry
            tags = []
        elif num_elements == 5:
            tags, hostlist, checkname, item, params = entry
            if not isinstance(tags, list):
                raise MKGeneralException(
                    "Invalid entry '%r' in check table. First entry must be list of host tags." %
                    (entry,))

        else:
            raise MKGeneralException(
                "Invalid entry '%r' in check table. It has %d entries, but must have 4 or 5." %
                (entry, len(entry)))

        # hostlist list might be:
        # 1. a plain hostname (string)
        # 2. a list of hostnames (list of strings)
        # Hostnames may be tagged. Tags are removed.
        # In autochecks there are always single untagged hostnames. We optimize for that.
        if isinstance(hostlist, str):
            if hostlist != hostname:
                return  # optimize most common case: hostname mismatch
            hostlist = [hostlist]
        elif isinstance(hostlist[0], str):
            pass  # regular case: list of hostnames
        elif hostlist != []:
            raise MKGeneralException("Invalid entry '%r' in check table. Must be single hostname "
                                     "or list of hostnames" % hostlist)

        if not is_checkname_valid(checkname):
            return

        if config.hosttags_match_taglist(hosttags, tags) and \
               config.in_extraconf_hostlist(hostlist, hostname):
            descr = config.service_description(hostname, checkname, item)
            if skip_ignored and config.service_ignored(hostname, checkname, descr):
                return

            if not host_config.part_of_clusters:
                svc_is_mine = True
            else:
                svc_is_mine = hostname == config_cache.host_of_clustered_service(
                    hostname, descr, part_of_clusters=host_config.part_of_clusters)

            if filter_mode is None and not svc_is_mine:
                return

            elif filter_mode == "only_clustered" and svc_is_mine:
                return

            deps = config.service_depends_on(hostname, descr)
            check_table[(checkname, item)] = (params, descr, deps)
예제 #4
0
    def get_dependencies(hostname, servicedesc):
        result = ""
        for dep in config.service_depends_on(hostname, servicedesc):
            result += _format_nagios_object(
                "servicedependency", {
                    "use": config.service_dependency_template,
                    "host_name": hostname,
                    "service_description": dep,
                    "dependent_host_name": hostname,
                    "dependent_service_description": servicedesc,
                })

        return result
예제 #5
0
def _get_sorted_check_table(hostname,
                            remove_duplicates=False,
                            filter_mode=None,
                            skip_ignored=True):
    # type: (str, bool, Optional[str], bool) -> List[Service]
    # Convert from dictionary into simple tuple list. Then sort it according to
    # the service dependencies.
    # TODO: Use the Service objects from get_check_table once it returns these objects
    is_cmc = config.is_cmc()
    unsorted = [
        (service, [] if is_cmc else config.service_depends_on(
            hostname, service.description))
        for service in get_check_table(hostname,
                                       remove_duplicates=remove_duplicates,
                                       filter_mode=filter_mode,
                                       skip_ignored=skip_ignored).values()
    ]

    unsorted.sort(key=lambda x: x[0].description)

    ordered = []  # type: List[Service]
    while len(unsorted) > 0:
        unsorted_descrs = {entry[0].description for entry in unsorted}
        left = []
        at_least_one_hit = False
        for check in unsorted:
            deps_fulfilled = True
            for dep in check[1]:  # dependencies
                if dep in unsorted_descrs:
                    deps_fulfilled = False
                    break
            if deps_fulfilled:
                ordered.append(check[0])
                at_least_one_hit = True
            else:
                left.append(check)
        if len(left) == 0:
            break
        if not at_least_one_hit:
            raise MKGeneralException(
                "Cyclic service dependency of host %s. Problematic are: %s" %
                (hostname, ",".join(unsorted_descrs)))
        unsorted = left
    return ordered
예제 #6
0
def test_service_depends_on_unknown_host():
    assert config.service_depends_on("test-host", "svc") == []