def _ip_address_for_dump_host(host_config, family=None): if host_config.is_cluster: try: return ip_lookup.lookup_ip_address(host_config.hostname, family) except: return "" try: return ip_lookup.lookup_ip_address(host_config.hostname, family) except: return core_config.fallback_ip_for(host_config, family)
def _create_nagios_host_spec(cfg, config_cache, hostname, attrs): 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): 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): 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.iteritems(): if host_config.is_cluster and key == "parents": continue host_spec[key] = value return host_spec