def _set_agent_config_hostname(self, agentConfig): # Try to fetch instance Id from EC2 if not hostname has been set # in the config file. # DEPRECATED if agentConfig.get('hostname') is None and agentConfig.get('use_ec2_instance_id'): instanceId = EC2.get_instance_id(agentConfig) if instanceId is not None: log.info("Running on EC2, instanceId: %s" % instanceId) agentConfig['hostname'] = instanceId else: log.info('Not running on EC2, using hostname to identify this server') return agentConfig
def get_hostname(config=None): """ Get the canonical host name this agent should identify as. This is the authoritative source of the host name for the agent. Tries, in order: * agent config (datadog.conf, "hostname:") * 'hostname -f' (on unix) * socket.gethostname() """ hostname = None # first, try the config if config is None: from config import get_config config = get_config(parse_args=True) config_hostname = config.get('hostname') if config_hostname and is_valid_hostname(config_hostname): return config_hostname # Try to get GCE instance name gce_hostname = GCE.get_hostname(config) if gce_hostname is not None: if is_valid_hostname(gce_hostname): return gce_hostname # Try to get the docker hostname if Platform.is_containerized(): # First we try from the Docker API docker_util = DockerUtil() docker_hostname = docker_util.get_hostname(use_default_gw=False) if docker_hostname is not None and is_valid_hostname(docker_hostname): hostname = docker_hostname elif Platform.is_k8s(): # Let's try from the kubelet kube_util = KubeUtil() _, kube_hostname = kube_util.get_node_info() if kube_hostname is not None and is_valid_hostname(kube_hostname): hostname = kube_hostname # then move on to os-specific detection if hostname is None: if Platform.is_unix() or Platform.is_solaris(): unix_hostname = _get_hostname_unix() if unix_hostname and is_valid_hostname(unix_hostname): hostname = unix_hostname # if we have an ec2 default hostname, see if there's an instance-id available if (Platform.is_ecs_instance()) or (hostname is not None and EC2.is_default(hostname)): instanceid = EC2.get_instance_id(config) if instanceid: hostname = instanceid # fall back on socket.gethostname(), socket.getfqdn() is too unreliable if hostname is None: try: socket_hostname = socket.gethostname() except socket.error: socket_hostname = None if socket_hostname and is_valid_hostname(socket_hostname): hostname = socket_hostname if hostname is None: log.critical('Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file') raise Exception('Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file') return hostname
def get_hostname(config=None): """ Get the canonical host name this agent should identify as. This is the authoritative source of the host name for the agent. Tries, in order: * agent config (datadog.conf, "hostname:") * 'hostname -f' (on unix) * socket.gethostname() """ hostname = None # first, try the config if config is None: from config import get_config config = get_config(parse_args=True) config_hostname = config.get('hostname') if config_hostname and is_valid_hostname(config_hostname): return config_hostname # Try to get GCE instance name gce_hostname = GCE.get_hostname(config) if gce_hostname is not None: if is_valid_hostname(gce_hostname): return gce_hostname # Try to get the docker hostname if Platform.is_containerized(): # First we try from the Docker API docker_util = DockerUtil() docker_hostname = docker_util.get_hostname(use_default_gw=False) if docker_hostname is not None and is_valid_hostname(docker_hostname): hostname = docker_hostname elif Platform.is_k8s(): # Let's try from the kubelet try: kube_util = KubeUtil() except Exception as ex: log.error("Couldn't instantiate the kubernetes client, " "getting the k8s hostname won't work. Error: %s" % str(ex)) else: _, kube_hostname = kube_util.get_node_info() if kube_hostname is not None and is_valid_hostname( kube_hostname): hostname = kube_hostname # then move on to os-specific detection if hostname is None: if Platform.is_unix() or Platform.is_solaris(): unix_hostname = _get_hostname_unix() if unix_hostname and is_valid_hostname(unix_hostname): hostname = unix_hostname # if we don't have a hostname, or we have an ec2 default hostname, # see if there's an instance-id available if not Platform.is_windows() and (hostname is None or Platform.is_ecs_instance() or EC2.is_default(hostname)): instanceid = EC2.get_instance_id(config) if instanceid: hostname = instanceid # fall back on socket.gethostname(), socket.getfqdn() is too unreliable if hostname is None: try: socket_hostname = socket.gethostname() except socket.error: socket_hostname = None if socket_hostname and is_valid_hostname(socket_hostname): hostname = socket_hostname if hostname is None: log.critical( 'Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file' ) raise Exception( 'Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file' ) return hostname