
    9Yf8(                         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j"                  d      ZdZ ej(                  d      Zd Zd	 Zd
 Z G d de      Z G d de      Zy)    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                 2   | j                         t        g d      v rt        j                  d| z         yt	        |       t
        kD  r!t        j                  d| dt
        d       yt        j                  |       t        j                  d| z         yy)	N)	localhostzlocalhost.localdomainzlocalhost6.localdomain6zip6-localhostzHostname: %s is localFz
Hostname: z is too long (max length is  z characters)z+Hostname: %s is not complying with RFC 1123T)lowersetlogwarninglenMAX_HOSTNAME_LENVALID_HOSTNAME_RFC_1123_PATTERNmatch)hostnames    U/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/datadog/util/hostname.pyis_valid_hostnamer      s    ~~3	
  	+h67
8}''QY[klm&,,X6>AHLM    c                    d}d}	 | r?t               }|j                  d      }|r"t        |      rt        j	                  d       |S |$t        j                  |      }|t        |      r|S |'d }t               }|dv r |       }|rt        |      r|}|HddD cg c]!  }|j                         j                  |      # c}v rt        j                  |      }	|	r|	}|$	 t        j                         }
|
rt        |
      r|
}|t        j	                  d	       |S # t
        $ r t        j                  d       Y w xY wc c}w # t        j                   $ r d}
Y hw xY w)
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                  F   	 t        j                  ddgt         j                  t         j                        } | j	                         \  }}| j
                  dk(  r9t               r|j                  d      j                         S |j                         S y # t        $ r Y y w xY w)Nz/bin/hostnamez-f)stdoutstderrr   zutf-8)

subprocessPopenPIPEDEVNULLcommunicate
returncoder   decodestrip	Exception)pouterrs      r   _get_hostname_unixz(get_hostname.<locals>._get_hostname_unixU   s    
$$ot%<Z__]g]o]op==?S<<1$x"zz'288::"yy{*	 %
  s   B B B 	B B )macfreebsdlinuxsolarisT)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	   r   
startswithEC2get_instance_idsocketgethostnameerror)hostname_from_configr   configconfig_hostnamegce_hostnamer*   os_nameunix_hostnamer'   
instanceidsocket_hostnames              r   r3   r3   -   s    HFA\F$jj4O#4_#E1
 '&
 ''/# .##	 (<<.0M!2=!A( Qb(cA)9)D)DQ)G(c c((0
!H 	#$002O 0A&HN	

 Oi  A?@A@ )d || 	#"O	#s*   A D &E#E E ?E E E c                  h   	 t        j                         } t        j                  d       	 t        j                  t        j
                  d            j                         t        j                  |        S # t        j                  |        w xY w# t        $ r t        j                         cY S w xY w)Ng      ?z3http://169.254.169.254/latest/meta-data/instance-id)	r7   getdefaulttimeoutsetdefaulttimeoutr   urlopenRequestreadr&   r8   )old_timeouts    r   get_ec2_instance_idrI      s    $..0 	  &	2??7??3k#lmrrt $$[1F$$[1 $!!##$s(   )B 5A7 !B 7BB B10B1c                   <    e Zd ZdZdZdZdZed        Zed        Z	y)r2   z9http://169.254.169.254/computeMetadata/v1/?recursive=true皙?zgoogle cloud platformNc                    t         j                  t         j                  S | d   s0t        j                  d       i t         _        t         j                  S d }	 t	        j
                         }t	        j                  t         j                         	 t        j                         }dg|_        t        j                  |j                  t         j                        j!                         j#                               t         _        	 |d}t	        j                  |       t         j                  S # t        $ r Y w xY w# t        $ r i t         _        Y Ow xY w# t        $ r Y t         j                  S w xY w)Ncollect_instance_metadata<Instance metadata collection is disabled. Not collecting it.)zX-Google-Metadata-RequestTrue   )r2   metadatar   r1   r7   rC   rD   TIMEOUTr&   r   build_opener
addheadersjsonloadsopenURLrG   r%   )agentConfig	socket_toopeners      r   _get_metadatazGCE._get_metadata   s&   <<#<<67HHSTCL<<		002I$$S[[1	))+F!F GF::fkk#''&:&?&?&A&G&G&IJCL
	 	$$Y/ ||#  		  	CL	  	||	s7   7D. A3D= E .	D:9D:=EE	E21E2c                     	 t         j                  |       }|d   d   j                  d      d   S # t        $ r Y y w xY w)Ninstancer   .r   )r2   r\   splitr&   )rY   host_metadatas     r   r3   zGCE.get_hostname   sI    	--k:M ,Z8>>sCAFF 		s   .1 	==)
__name__
__module____qualname__rX   rR   SOURCE_TYPE_NAMErQ   staticmethodr\   r3    r   r   r2   r2      s>    
ECG.H @  r   r2   c                   L    e Zd ZdZdZdZi Zed        Zed        Z	ed        Z
y)r5   zRetrieve EC2 metadataz'http://169.254.169.254/latest/meta-datarK   c                    | d   st         j                  d       g S d }	 t        j                         }t        j                  t
        j                         	 t        j                  t
        j                  dz         j                         j                         }t        j                  t        j                  t
        j                  dz   dz   t        |      z         j                         j                               }ddlm}  ||d   |d   |d	   
      }|j%                  t
        j&                  d   g      d   }t)        |j*                        D cg c]  \  }}|d| }	}}	 |d}t        j                  |       |	S # t        $ r Y ;w xY wc c}}w # t        $ r t         j-                  d       g }	Y Tw xY w# t        $ r Y |	S w xY w)NrM   rN   z/iam/security-credentials/r   )EC2ConnectionAccessKeyIdSecretAccessKeyToken)aws_access_key_idaws_secret_access_keysecurity_tokeninstance-id:z"Problem retrieving custom EC2 tagsrP   )r   r1   r7   rC   rD   r5   rR   r&   r   rE   rX   rG   r%   rU   rV   strboto.ec2.connectionrk   get_only_instancesrQ   r   tags	exception)
rY   rZ   iam_role
iam_paramsrk   
connectioninstance_objecttag_key	tag_valueEC2_tagss
             r   get_tagszEC2.get_tags   s   67HHSTI		002I$$S[[1	sww1L'LMRRTZZ\H*E E KcRZm [\aaciikJ :&",]";&01B&C)'2J
 );;S\\-=X<YZ[\]OR[\k\p\pRqr<NGYGY7rHr	 	$$Y/ =  		" s 	MM>?H	  		sH   7F C>F F'F +G 	FFF  F?>F?	GGc                    | d   st         j                  d       i S d}	 t        j                         }t        j                  t
        j                         dD ]  }	 t        j                  t
        j                  dz   t        |      z         j                         j                         }t        |      t        j                   t        j"                  fv rt%        |      dkD  s
J d|z         |t
        j&                  |<    	 |d}t        j                  |       t
        j&                  S # t        $ r Y w xY w# t        $ r Y w xY w# t        $ r Y t
        j&                  S w xY w)	zmUse the ec2 http service to introspect the instance. This adds latency         if not running on EC2
        rM   rN   N)
rr   r   zlocal-hostnamezpublic-hostnamezami-idz
local-ipv4zpublic-keyszpublic-ipv4zreservation-idzsecurity-groupsrj   r   z%s is not a stringrP   )r   r1   r7   rC   rD   r5   rR   r&   r   rE   rX   rt   rG   r%   typetypes
StringTypeUnicodeTyper   rQ   )rY   rZ   kvs       r   get_metadatazEC2.get_metadata   sF   & 67HHSTI		002I$$S[[1
 	AOOCGGcMCF$:;@@BHHJAw5#3#3U5F5F"GGCPQFUVJpXlopXppV"#Q	&	 	$$Y/ ||;  		&    	||	s6   7D- B$D<E -	D98D9<	EE	E&%E&c                 l    	 t         j                  |       j                  dd       S # t        $ r Y y w xY w)Nrr   )r5   r   r0   r&   )rY   s    r   r6   zEC2.get_instance_id,  s7    	##K044]DII 		s   $' 	33N)rb   rc   rd   __doc__rX   rR   rQ   rf   r   r   r6   rg   r   r   r5   r5      sP    
3CGH' 'R 7 7r  r   r5   )rU   loggingrer7   r   r   typingr   r   datadog.util.compatr   r   r   datadog.util.configr   r	   r
   compiler   r   	getLoggerr   r   r3   rI   objectr2   r5   rg   r   r   <module>r      s   
   	    ! ; : ? ?","**r#   g&(Pf$"-& -`p& pr   