Exemple #1
0
    def _translate_host_macros(self, cmd: str) -> str:
        attrs = core_config.get_host_attributes(self.hostname, self._config_cache)
        if self._host_config.is_cluster:
            parents_list = core_config.get_cluster_nodes_for_config(self._config_cache,
                                                                    self._host_config)
            attrs.setdefault("alias", "cluster of %s" % ", ".join(parents_list))
            attrs.update(
                core_config.get_cluster_attributes(self._config_cache, self._host_config,
                                                   parents_list))

        macros = core_config.get_host_macros_from_attributes(self.hostname, attrs)
        return ensure_str(core_config.replace_macros(cmd, macros))
Exemple #2
0
    def _translate_host_macros(cmd: str, host_config: config.HostConfig) -> str:
        config_cache = config.get_config_cache()
        attrs = core_config.get_host_attributes(host_config.hostname, config_cache)
        if host_config.is_cluster:
            parents_list = core_config.get_cluster_nodes_for_config(
                config_cache,
                host_config,
            )
            attrs.setdefault("alias", "cluster of %s" % ", ".join(parents_list))
            attrs.update(
                core_config.get_cluster_attributes(
                    config_cache,
                    host_config,
                    parents_list,
                ))

        macros = core_config.get_host_macros_from_attributes(host_config.hostname, attrs)
        return ensure_str(core_config.replace_macros(cmd, macros))
Exemple #3
0
def _create_nagios_host_spec(cfg, config_cache, hostname, attrs):
    # type: (NagiosConfig, ConfigCache, HostName, ObjectAttributes) -> ObjectSpec
    host_config = config_cache.get_host_config(hostname)

    ip = attrs["address"]

    if host_config.is_cluster:
        nodes = core_config.get_cluster_nodes_for_config(
            config_cache, host_config)
        attrs.update(
            core_config.get_cluster_attributes(config_cache, host_config,
                                               nodes))

    #   _
    #  / |
    #  | |
    #  | |
    #  |_|    1. normal, physical hosts

    host_spec = {
        "host_name": hostname,
        "use": config.cluster_template
        if host_config.is_cluster else config.host_template,
        "address": ip if ip else core_config.fallback_ip_for(host_config),
        "alias": attrs["alias"],
    }

    # Add custom macros
    for key, value in attrs.items():
        if key[0] == '_':
            host_spec[key] = value

    def host_check_via_service_status(service):
        # type: (ServiceName) -> CoreCommand
        command = "check-mk-host-custom-%d" % (
            len(cfg.hostcheck_commands_to_define) + 1)
        cfg.hostcheck_commands_to_define.append(
            (command,
             'echo "$SERVICEOUTPUT:%s:%s$" && exit $SERVICESTATEID:%s:%s$' %
             (host_config.hostname,
              service.replace('$HOSTNAME$',
                              host_config.hostname), host_config.hostname,
              service.replace('$HOSTNAME$', host_config.hostname))))
        return command

    def host_check_via_custom_check(command_name, command):
        # type: (CoreCommandName, CoreCommand) -> CoreCommand
        cfg.custom_commands_to_define.add(command_name)
        return command

    # Host check command might differ from default
    command = core_config.host_check_command(
        config_cache,  #
        host_config,
        ip,
        host_config.is_cluster,
        "ping",
        host_check_via_service_status,
        host_check_via_custom_check)
    if command:
        host_spec["check_command"] = command

    hostgroups = host_config.hostgroups
    if config.define_hostgroups or hostgroups == [config.default_host_group]:
        cfg.hostgroups_to_define.update(hostgroups)
    host_spec["hostgroups"] = ",".join(hostgroups)

    # Contact groups
    contactgroups = host_config.contactgroups
    if contactgroups:
        host_spec["contact_groups"] = ",".join(contactgroups)
        cfg.contactgroups_to_define.update(contactgroups)

    if not host_config.is_cluster:
        # Parents for non-clusters

        # Get parents explicitly defined for host/folder via extra_host_conf["parents"]. Only honor
        # the ruleset "parents" in case no explicit parents are set
        if not attrs.get("parents", []):
            parents_list = host_config.parents
            if parents_list:
                host_spec["parents"] = ",".join(parents_list)

    elif host_config.is_cluster:
        # Special handling of clusters
        host_spec["parents"] = ",".join(nodes)

    # Custom configuration last -> user may override all other values
    # TODO: Find a generic mechanism for CMC and Nagios
    for key, value in host_config.extra_host_attributes.items():
        if host_config.is_cluster and key == "parents":
            continue
        host_spec[key] = value

    return host_spec