
    d                     x    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZ  G d de      Z G d	 d
e      Zy)    N)logger)	HttpError)ustr)restutil)
AGENT_NAMECURRENT_VERSIONc                   $    e Zd ZddZed        Zy)Observationc                     |t        d      |t        d      |d}|d}|| _        || _        || _        || _        y )Nz!Observation name must be providedz#Observation health must be provided )
ValueErrorname
is_healthydescriptionvalue)selfr   r   r   r   s        O/usr/lib/python3/dist-packages/azurelinuxagent/common/protocol/healthservice.py__init__zObservation.__init__   sW    <@AABCC=EK	$&
    c                 r    | j                   d d | j                  | j                  d d | j                  d d dS )N@      )ObservationName	IsHealthyDescriptionValuer   r   r   r   )r   s    r   as_objzObservation.as_obj0   s@      $yy"~++DS1ZZ%	
 	
r   Nr   r   )__name__
__module____qualname__r   propertyr    r   r   r
   r
      s    $ 
 
r   r
   c                   |    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
Zd Zed        Zd Zd Zd Zd Zd ZddZd Zd Zy)HealthServicezhttp://{0}:80/HealthServicereporttargethealthz1.0WALinuxAgentGuestAgentPluginHeartbeatGuestAgentPluginStatusGuestAgentPluginVersionsGuestAgentPluginArtifactInstanceMetadataHeartbeat
   c                     t         j                  j                  |      | _        t         j                  | _        t         j                  | _        t         j                  | _	        t               | _        y )N)r&   ENDPOINTformatendpointAPIapiVERSIONversionOBSERVER_NAMEsourcelistobservations)r   r2   s     r   r   zHealthService.__init__G   sK    %..55h? $$$,,#11 Fr   c                     | j                   | j                  | j                  | j                  D cg c]  }|j                   c}d}t        j                  |      S c c}w )N)ApiVersionSourceObservations)r4   r6   r8   r:   r   jsondumps)r   odatas      r   as_jsonzHealthService.as_jsonN   sO     88||kk/3/@/@A!QXXA	
 zz$ Bs   A
c                 f    | j                  t        j                  |       | j                          y)zd
        Reports a signal for /health
        :param is_healthy: whether the call succeeded
        )r   r   N)_observer&   &HOST_PLUGIN_HEARTBEAT_OBSERVATION_NAME_report)r   r   s     r   report_host_plugin_heartbeatz*HealthService.report_host_plugin_heartbeatX   s)    
 	=OO!+ 	 	-r   c                 h    | j                  t        j                  ||       | j                          y)z
        Reports a signal for /versions
        :param is_healthy: whether the api call succeeded
        :param response: debugging information for failures
        r   r   r   N)rF   r&   %HOST_PLUGIN_VERSIONS_OBSERVATION_NAMErH   r   r   responses      r   report_host_plugin_versionsz)HealthService.report_host_plugin_versionsa   s.     	=NN!+$ 	 	& 	r   c                 j    | j                  t        j                  |||       | j                          y)z
        Reports a signal for /extensionArtifact
        :param is_healthy: whether the api call succeeded
        :param source: specifies the api caller for debugging failures
        :param response: debugging information for failures
        r   N)rF   r&   %HOST_PLUGIN_ARTIFACT_OBSERVATION_NAMErH   )r   r   r8   rN   s       r   %report_host_plugin_extension_artifactz3HealthService.report_host_plugin_extension_artifactl   s1     	=NN!+"($ 	 	& 	r   c                 h    | j                  t        j                  ||       | j                          y)z
        Reports a signal for /status
        :param is_healthy: whether the api call succeeded
        :param response: debugging information for failures
        rK   N)rF   r&   #HOST_PLUGIN_STATUS_OBSERVATION_NAMErH   rM   s      r   report_host_plugin_statusz'HealthService.report_host_plugin_statusy   s.     	=LL!+$ 	 	& 	r   c                 h    | j                  t        j                  ||       | j                          y)z
        Reports a signal for /metadata/instance
        :param is_healthy: whether the api call succeeded and returned valid data
        :param response: debugging information for failures
        rK   N)rF   r&   IMDS_OBSERVATION_NAMErH   rM   s      r   report_imds_statusz HealthService.report_imds_status   s.     	=>>!+$ 	 	& 	r   c                     t        | j                        t        j                  k\  r | j                  d t        j                  dz
  = | j                  j	                  t        ||||             y )N   )r   r   r   r   )lenr:   r&   MAX_OBSERVATIONSappendr
   )r   r   r   r   r   s        r   rF   zHealthService._observe   sb    t  !]%C%CC!!"C=#A#A!#C"CD  $8B389D"F 	Gr   c                    t        j                  d       	 t        j                  | j                  | j
                  ddi       t        j                  d| j                  | j
                         | j                          | j                  d d = y # t        $ r)}t        j                  dt        |             Y d }~Md }~ww xY w# | j                          | j                  d d = w xY w)Nz"HealthService: report observationszContent-Typezapplication/json)headersz0HealthService: Reported observations to {0}: {1}z1HealthService: could not report observations: {0})r   verboser   	http_postr2   rD   r   warnr   _report_failuresr:   )r   es     r   rH   zHealthService._report   s    ;<		%t}}dll^UgDhiNNMt}}^b^j^jk
 !!#!!!$  	VKKKTRSWUU	V !!#!!!$s*   AB 	CB=8C =CC !C&c           
      z   	 t        j                  d       ddlm}m} | j
                  D ]K  }|j                  r |t        t        |j                  dt        j                  |j                               M y # t        $ r7}t        j                  dj                  t        |                   Y d }~y d }~ww xY w)Nz+HealthService: report failures as telemetryr   )	add_eventWALAEventOperationF)r6   op
is_successmessagez-HealthService: could not report failures: {0})r   r`   azurelinuxagent.common.eventrf   rg   r:   r   r   r   HealthObservationr@   rA   r   	Exceptionr1   r   )r   rf   rg   rB   rd   s        r   rc   zHealthService._report_failures   s    	\NNHIR&& <||j&5!3!E!E).&*jj&:	<<  	\NNJQQRVWXRYZ[[	\s   8A: >A: :	B:-B55B:Nr   )r    r!   r"   r0   r3   r5   r7   rG   rT   rL   rQ   rW   r\   r   r#   rD   rI   rO   rR   rU   rX   rF   rH   rc   r$   r   r   r&   r&   :   s}    ,H
CG"M-H**B',F),F)7#    			G%\r   r&   )r@   azurelinuxagent.commonr    azurelinuxagent.common.exceptionr   azurelinuxagent.common.futurer   azurelinuxagent.common.utilsr   azurelinuxagent.common.versionr   r   objectr
   r&   r$   r   r   <module>rt      s5   (  ) 6 . 1 F
& 
:w\F w\r   