
    Vh                        d dl mZ dZdZd dlZd dlZd dlZ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  G d
 de      Z G d de      Zy)    )annotationsa  
name: sumologic
type: notification
short_description: Sends task result events to Sumologic
author: "Ryan Currah (@ryancurrah)"
description:
  - This callback plugin will send task results as JSON formatted events to a Sumologic HTTP collector source.
requirements:
  - Whitelisting this callback plugin
  - 'Create a HTTP collector source in Sumologic and specify a custom timestamp format of V(yyyy-MM-dd HH:mm:ss ZZZZ) and
    a custom timestamp locator of V("timestamp": "(.*\)")'
options:
  url:
    description: URL to the Sumologic HTTP collector source.
    type: str
    env:
      - name: SUMOLOGIC_URL
    ini:
      - section: callback_sumologic
        key: url
a?  
examples: |-
  To enable, add this to your ansible.cfg file in the defaults block
    [defaults]
    callback_whitelist = community.general.sumologic

  Set the environment variable
    export SUMOLOGIC_URL=https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/R8moSv1d8EW9LAUFZJ6dbxCFxwLH6kfCdcBfddlfxCbLuL-BN5twcTpMk__pYy_cDmp==

  Set the ansible.cfg variable in the callback_sumologic block
    [callback_sumologic]
    url = https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/R8moSv1d8EW9LAUFZJ6dbxCFxwLH6kfCdcBfddlfxCbLuL-BN5twcTpMk__pYy_cDmp==
N)basename)open_url)AnsibleJSONEncoder)CallbackBase)nowc                      e Zd Zd Zd Zy)SumologicHTTPCollectorSourcec                .   d| _         d| _        d| _        t        t	        j
                               | _        t        j                         | _	        t        j                  t        j                               | _        t        j                         | _        y )NF )ansible_check_modeansible_playbookansible_versionstruuiduuid4sessionsocketgethostnamehostgethostbyname
ip_addressgetpassgetuseruser)selfs    p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/callback/sumologic.py__init__z%SumologicHTTPCollectorSource.__init__=   se    "' "!4::<(&&(	 ..v/A/A/CDOO%	    c                   |j                   d   j                  d      du rd| _        |j                   d   j                  d      r#|j                   d   j                  d      | _        |j                  j
                  r t        |j                  j
                        }nd }d|j                   v r|j                   d= i }|j                  j                  |d<   | j                  |d<   ||d<   t               j                  d      |d	<   | j                  |d
<   | j                  |d<   | j                  |d<   ||d<   | j                  |d<   | j                  |d<   |j                  j                  |d<   | j                   |d<   ||d<   |j                   |d<   |j"                  |d<   t%        |t'        j(                  |t*        d      d|d   dd       y )Nargs_ansible_check_modeT_ansible_versionr   r   statusz%Y-%m-%d %H:%M:%S +0000	timestampr   r   r   runtimer   r   ansible_hostr   ansible_roleansible_taskansible_result)cls	sort_keyszapplication/json)zContent-typezX-Sumo-HostPOST)dataheadersmethod)_task_fieldsgetr   r   _task_roler   _uuidr   r   strftimer   r   r   _hostnamer   _resultr   jsondumpsr   )r   urlstateresultr&   r(   r.   s          r   
send_eventz'SumologicHTTPCollectorSource.send_eventF   s   v&**+@ATI&*D#v&**+=>##F+//0BC   <<v||112LLV(((##F+||))V,,YXENN+DE[yyV!__\yyV!Y"&"6"6%)%<%<!"%||00^#'#8#8 +^%22^!'D&8DI 2#N3 	
r   N)__name__
__module____qualname__r   r?    r   r   r
   r
   <   s    &)
r   r
   c                  r     e Zd ZdZdZdZdZd fd	Zd Zd fd	Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Z xZS )CallbackModuleg       @notificationzcommunity.general.sumologicTc                h    t         t        |   |       i | _        d | _        t               | _        y )N)display)superrE   r   start_datetimesr<   r
   	sumologic)r   rH   	__class__s     r   r   zCallbackModule.__init__x   s.    nd,W,=!57r   c                z    t               | j                  |j                  j                     z
  j	                         S N)r   rJ   r3   r5   total_seconds)r   r>   s     r   _runtimezCallbackModule._runtime~   s0    E  !3!345
-/	r   c                    t         t        |   |||       | j                  d      | _        | j                  #d| _        | j                  j                  d       y y )N)	task_keysvar_optionsdirectr<   TzSumologic HTTP collector source URL was not provided. The Sumologic HTTP collector source URL can be provided using the `SUMOLOGIC_URL` environment variable or in the ansible.cfg file.)rI   rE   set_options
get_optionr<   disabled_displaywarning)r   rR   rS   rT   rL   s       r   rU   zCallbackModule.set_options   sX    nd/)Q\ek/l??5)88 DMMM!! #= > r   c                L    t        |j                        | j                  _        y rN   )r   
_file_namerK   r   )r   playbooks     r   v2_playbook_on_startz#CallbackModule.v2_playbook_on_start   s    *283F3F*G'r   c                F    t               | j                  |j                  <   y rN   r   rJ   r5   )r   taskis_conditionals      r   v2_playbook_on_task_startz(CallbackModule.v2_playbook_on_task_start       +.5TZZ(r   c                F    t               | j                  |j                  <   y rN   r_   )r   r`   s     r   !v2_playbook_on_handler_task_startz0CallbackModule.v2_playbook_on_handler_task_start   rc   r   c                r    | j                   j                  | j                  d|| j                  |             y )NOKrK   r?   r<   rP   r   r>   kwargss      r   v2_runner_on_okzCallbackModule.v2_runner_on_ok   s-    !!HHMM&!		
r   c                r    | j                   j                  | j                  d|| j                  |             y )NSKIPPEDrh   ri   s      r   v2_runner_on_skippedz#CallbackModule.v2_runner_on_skipped   s-    !!HHMM&!		
r   c                r    | j                   j                  | j                  d|| j                  |             y NFAILEDrh   ri   s      r   v2_runner_on_failedz"CallbackModule.v2_runner_on_failed   -    !!HHMM&!		
r   c                r    | j                   j                  | j                  d|| j                  |             y rp   rh   ri   s      r   runner_on_async_failedz%CallbackModule.runner_on_async_failed   rs   r   c                r    | j                   j                  | j                  d|| j                  |             y )NUNREACHABLErh   ri   s      r   v2_runner_on_unreachablez'CallbackModule.v2_runner_on_unreachable   s-    !!HHMM&!		
r   rN   )NNN)r@   rA   rB   CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMECALLBACK_NEEDS_WHITELISTr   rP   rU   r]   rb   re   rk   rn   rr   ru   rx   __classcell__)rL   s   @r   rE   rE   r   sQ    "M1M#8>H11




r   rE   )
__future__r   DOCUMENTATIONEXAMPLESr:   r   r   r   os.pathr   ansible.module_utils.urlsr   ansible.parsing.ajsonr   ansible.plugins.callbackr   Cansible_collections.community.general.plugins.module_utils.datetimer   objectr
   rE   rC   r   r   <module>r      sR    #,      . 4 1
3
6 3
lN
\ N
r   