o
    i8(                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZ d dlmZmZmZ edZdZedZdd	 Zd
d Zdd ZG dd deZG dd deZdS )    N)DictOptional)url_libis_p3k	iteritems)
get_configget_osCfgNotFoundzk^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$   zdatadog.apic                 C   sl   |   tg dv rtd|   dS t| tkr$td| tf  dS t| d u r4td|   dS dS )N)	localhostzlocalhost.localdomainzlocalhost6.localdomain6zip6-localhostzHostname: %s is localFz7Hostname: %s is too long (max length is  %s characters)z+Hostname: %s is not complying with RFC 1123T)lowersetlogwarninglenMAX_HOSTNAME_LENVALID_HOSTNAME_RFC_1123_PATTERNmatchhostname r   I/home/ubuntu/.local/lib/python3.10/site-packages/datadog/util/hostname.pyis_valid_hostname   s   r   c           	         s8  d d}z| rt  }|d}|rt|rtd |W S W n ty,   td Y nw  du r@t|}|dur@t|r@|S  du rZdd }t	 }|dv rZ| }|rZt|rZ|  durrd fd	d
dD v rrt
|}|rr|  du rzt }W n tjy   d}Y nw |rt|r|  du rtd  S )a  
    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()
    Nr   zHostname lookup from agent configuration will be deprecated in an upcoming version of datadogpy. Set hostname_from_config to False to get rid of this warningz,No agent or invalid configuration file foundc                  S   sj   z*t jddgt jt jd} |  \}}| jdkr(t r#|d W S | W S W d S  t	y4   Y d S w )Nz/bin/hostnamez-f)stdoutstderrr   zutf-8)

subprocessPopenPIPEDEVNULLcommunicate
returncoder   decodestrip	Exception)pouterrr   r   r   _get_hostname_unixU   s   

z(get_hostname.<locals>._get_hostname_unix)macfreebsdlinuxsolarisTc                    s   g | ]	}   |qS r   )r   
startswith).0r$   r   r   r   
<listcomp>i   s    z get_hostname.<locals>.<listcomp>)zip-domuzUnable to reliably determine host name. You can define one in your `hosts` file, or in `datadog.conf` file if you have Datadog Agent installed.)r   getr   r   r   r	   infoGCEget_hostnamer   EC2get_instance_idsocketgethostnameerror)	hostname_from_configconfigconfig_hostnamegce_hostnamer'   os_nameunix_hostname
instanceidsocket_hostnamer   r   r   r3   -   sX   


r3   c               	   C   s^   z!t  } t d zttd W t |  W S t |  w  ty.   t   Y S w )Ng      ?z3http://169.254.169.254/latest/meta-data/instance-id)	r6   getdefaulttimeoutsetdefaulttimeoutr   urlopenRequestreadr#   r7   )old_timeoutr   r   r   get_ec2_instance_id   s   
rG   c                   @   s4   e Zd ZdZdZdZdZedd Zedd Z	dS )	r2   z9http://169.254.169.254/computeMetadata/v1/?recursive=true皙?zgoogle cloud platformNc                 C   s   t jd urt jS | d std i t _t jS d }zt }tt j W n	 ty.   Y nw zt	
 }dg|_t|t j  t _W n tyS   i t _Y nw z|d u r[d}t| W t jS  tyn   Y t jS w )Ncollect_instance_metadata<Instance metadata collection is disabled. Not collecting it.)zX-Google-Metadata-RequestTrue   )r2   metadatar   r1   r6   rA   rB   TIMEOUTr#   r   build_opener
addheadersjsonloadsopenURLrE   r"   )agentConfig	socket_toopenerr   r   r   _get_metadata   s:   

 
zGCE._get_metadatac                 C   s8   zt | }|d d dd W S  ty   Y d S w )Ninstancer   .r   )r2   rX   splitr#   )rU   host_metadatar   r   r   r3      s   
zGCE.get_hostname)
__name__
__module____qualname__rT   rN   SOURCE_TYPE_NAMErM   staticmethodrX   r3   r   r   r   r   r2      s    
 r2   c                   @   s@   e Zd ZdZdZdZi Zedd Zedd Z	edd	 Z
d
S )r4   zRetrieve EC2 metadataz'http://169.254.169.254/latest/meta-datarH   c                 C   s0  | d st d g S d }zt }ttj W n	 ty"   Y nw zKt	tj
d   }tt	tj
d d t|   }ddlm} ||d |d |d	 d
}|tjd gd }dd t|jD }W n ty~   t d g }Y nw z|d u rd}t| W |S  ty   Y |S w )NrI   rJ   z/iam/security-credentials/r   )EC2ConnectionAccessKeyIdSecretAccessKeyToken)aws_access_key_idaws_secret_access_keysecurity_tokeninstance-idc                 S   s   g | ]
\}}d ||f qS )z%s:%sr   )r-   tag_key	tag_valuer   r   r   r.      s    z EC2.get_tags.<locals>.<listcomp>z"Problem retrieving custom EC2 tagsrL   )r   r1   r6   rA   rB   r4   rN   r#   r   rC   rT   rE   r"   rQ   rR   strboto.ec2.connectionrc   get_only_instancesrM   r   tags	exception)rU   rV   iam_role
iam_paramsrc   
connectioninstance_objectEC2_tagsr   r   r   get_tags   sH   
"
zEC2.get_tagsc              	   C   s   | d st d i S d}zt }ttj W n	 ty"   Y nw dD ]8}z-t	tj
d t|   }t|tjtjfv rHt|dksNJ d| |tj|< W q% ty]   Y q%w z|du red}t| W tjS  tyx   Y tjS w )	zmUse the ec2 http service to introspect the instance. This adds latency         if not running on EC2
        rI   rJ   N)
rj   r   zlocal-hostnamezpublic-hostnamezami-idz
local-ipv4zpublic-keyszpublic-ipv4zreservation-idzsecurity-groupsrb   r   z%s is not a stringrL   )r   r1   r6   rA   rB   r4   rN   r#   r   rC   rT   rm   rE   r"   typetypes
StringTypeUnicodeTyper   rM   )rU   rV   kvr   r   r   get_metadata   s6   
 ,zEC2.get_metadatac                 C   s*   z
t | dd W S  ty   Y d S w )Nrj   )r4   r~   r0   r#   )rU   r   r   r   r5   ,  s
   zEC2.get_instance_idN)r]   r^   r_   __doc__rT   rN   rM   ra   rw   r~   r5   r   r   r   r   r4      s    
)
9r4   )rQ   loggingrer6   r   ry   typingr   r   datadog.util.compatr   r   r   datadog.util.configr   r   r	   compiler   r   	getLoggerr   r   r3   rG   objectr2   r4   r   r   r   r   <module>   s&   
S0