#NEWLINE# def get_hostname( ) : #NEWLINE# #INDENT# '\n    Get the canonical host name this agent should identify as. This is\n    the authoritative source of the host name for the agent.\n\n    Tries, in order:\n\n      * agent config (datadog.conf, "hostname:")\n      * \'hostname -f\' (on unix)\n      * socket.gethostname()\n    ' #NEWLINE# hostname = None #NEWLINE# config = None #NEWLINE# try : #NEWLINE# #INDENT# config = get_config ( ) #NEWLINE# config_hostname = config . get ( 'hostname' ) #NEWLINE# if ( config_hostname and is_valid_hostname ( config_hostname ) ) : #NEWLINE# #INDENT# return config_hostname #NEWLINE# #UNINDENT# #UNINDENT# except CfgNotFound : #NEWLINE# #INDENT# log . info ( 'No agent or invalid configuration file found' ) #NEWLINE# #UNINDENT# if ( hostname is None ) : #NEWLINE# #INDENT# gce_hostname = GCE . get_hostname ( config ) #NEWLINE# if ( gce_hostname is not None ) : #NEWLINE# #INDENT# if is_valid_hostname ( gce_hostname ) : #NEWLINE# #INDENT# return gce_hostname #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( unix_hostname is None ) : #NEWLINE# #INDENT# def _get_hostname_unix( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# p = subprocess . Popen ( [ '/bin/hostname' , '-f' ] , stdout = subprocess . PIPE ) #NEWLINE# ( out , err ) = p . communicate ( ) #NEWLINE# if ( p . returncode == 0 ) : #NEWLINE# #INDENT# if is_p3k ( ) : #NEWLINE# #INDENT# return out . decode ( 'utf-8' ) . strip ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return out . strip ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except Exception : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# #UNINDENT# os_name = get_os ( ) #NEWLINE# if ( os_name in [ 'mac' , 'freebsd' , 'linux' , 'solaris' ] ) : #NEWLINE# #INDENT# unix_hostname = _get_hostname_unix ( ) #NEWLINE# if ( unix_hostname and is_valid_hostname ( unix_hostname ) ) : #NEWLINE# #INDENT# hostname = unix_hostname #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( ( hostname is not None ) and ( True in [ hostname . lower ( ) . startswith ( p ) for p in [ 'ip-' , 'domu' ] ] ) ) : #NEWLINE# #INDENT# instanceid = EC2 . get_instance_id ( config ) #NEWLINE# if instanceid : #NEWLINE# #INDENT# hostname = instanceid #NEWLINE# #UNINDENT# #UNINDENT# if ( hostname is None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# socket_hostname = socket . gethostname ( ) #NEWLINE# #UNINDENT# except socket . error : #NEWLINE# #INDENT# socket_hostname = None #NEWLINE# #UNINDENT# if ( socket_hostname and is_valid_hostname ( socket_hostname ) ) : #NEWLINE# #INDENT# hostname = socket_hostname #NEWLINE# #UNINDENT# #UNINDENT# if ( hostname is None ) : #NEWLINE# #INDENT# log . critical ( 'Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file' ) #NEWLINE# raise Exception ( 'Unable to reliably determine host name. You can define one in datadog.conf or in your hosts file' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return hostname