
    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Z	 d dl	Z	dZ
	 d dlZdZd dlmZmZ d dlmZ  G d	 d
e      Z	 d dlZdZ G d de      ZeZ G d de      Zy# e$ r dZ
Y Iw xY w# e$ r dZY Ow xY w# e$ r eZdZY 1w xY w)    )annotationsa  
author: Unknown (!UNKNOWN)
name: logentries
type: notification
short_description: Sends events to Logentries
description:
  - This callback plugin will generate JSON objects and send them to Logentries using TCP for auditing/debugging purposes.
requirements:
  - whitelisting in configuration
  - certifi (Python library)
  - flatdict (Python library), if you want to use the O(flatten) option
options:
  api:
    description: URI to the Logentries API.
    type: str
    env:
      - name: LOGENTRIES_API
    default: data.logentries.com
    ini:
      - section: callback_logentries
        key: api
  port:
    description: HTTP port to use when connecting to the API.
    type: int
    env:
      - name: LOGENTRIES_PORT
    default: 80
    ini:
      - section: callback_logentries
        key: port
  tls_port:
    description: Port to use when connecting to the API when TLS is enabled.
    type: int
    env:
      - name: LOGENTRIES_TLS_PORT
    default: 443
    ini:
      - section: callback_logentries
        key: tls_port
  token:
    description: The logentries C(TCP token).
    type: str
    env:
      - name: LOGENTRIES_ANSIBLE_TOKEN
    required: true
    ini:
      - section: callback_logentries
        key: token
  use_tls:
    description:
      - Toggle to decide whether to use TLS to encrypt the communications with the API server.
    env:
      - name: LOGENTRIES_USE_TLS
    default: false
    type: boolean
    ini:
      - section: callback_logentries
        key: use_tls
  flatten:
    description: Flatten complex data structures into a single dictionary with complex keys.
    type: boolean
    default: false
    env:
      - name: LOGENTRIES_FLATTEN
    ini:
      - section: callback_logentries
        key: flatten
a5  
examples: >-
  To enable, add this to your ansible.cfg file in the defaults block

    [defaults]
    callback_whitelist = community.general.logentries

  Either set the environment variables
    export LOGENTRIES_API=data.logentries.com
    export LOGENTRIES_PORT=10000
    export LOGENTRIES_ANSIBLE_TOKEN=dd21fc88-f00a-43ff-b977-e3a4233c53af

  Or in the main Ansible config file
    [callback_logentries]
    api = data.logentries.com
    port = 10000
    tls_port = 20000
    use_tls = true
    token = dd21fc88-f00a-43ff-b977-e3a4233c53af
    flatten = false
NTF)to_bytesto_text)CallbackBasec                  ,    e Zd ZddZd Zd Zd Zd Zy)PlainTextSocketAppenderc                    || _         || _        || _        d| _        d| _        d| _        d| _        || _        d | _        y )Ng?
   zG

It appears the LOGENTRIES_TOKEN parameter you entered is incorrect!

u    )	LE_APILE_PORTLE_TLS_PORT	MIN_DELAY	MAX_DELAYINVALID_TOKENLINE_SEP_display_conn)selfdisplayr   r   r   s        q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/callback/logentries.py__init__z PlainTextSocketAppender.__init__}   sC    &j 
    c                    t        j                   t         j                  t         j                        | _        | j                  j	                  | j
                  | j                  f       y N)socketAF_INETSOCK_STREAMr   connectr   r   r   s    r   open_connectionz'PlainTextSocketAppender.open_connection   s=    ]]6>>63E3EF


DKK67r   c                   | j                          | j                  }	 	 | j                          y # t        $ r(}| j                  j                  d|        Y d }~nd }~ww xY w|dz  }|| j                  kD  r| j                  }|t        j                  d|      z   }	 | j                  j                  d| d       t        j                  |       n# t        $ r  w xY w)Nz!Unable to connect to Logentries:    r   z	sleeping z before retry)close_connectionr   r    	Exceptionr   vvvvr   randomuniformtimesleepKeyboardInterrupt)r   
root_delayewait_fors       r   reopen_connectionz)PlainTextSocketAppender.reopen_connection   s    ^^
L$$& L""%Fqc#JKKL !OJDNN*!^^
!FNN1j$AAH""Yxj#FG

8$$ ! s    0 	A!AA!4C Cc                R    | j                   | j                   j                          y y r   )r   closer   s    r   r#   z(PlainTextSocketAppender.close_connection   s!    ::!JJ "r   c                $   t        |d      }|j                  d| j                        }|dz  }	 	 | j                  j	                  t        |d             	 | j                          y # t        j                  $ r | j                          Y _w xY w)Nsurrogate_or_strict)errors
)
r   replacer   r   sendr   r   errorr.   r#   )r   data	multilines      r   putzPlainTextSocketAppender.put   s     t$9:LLt}}5	T	

;P QR  << &&(s   &A) )#BBN)zdata.logentries.comP   i  )__name__
__module____qualname__r   r    r.   r#   r:    r   r   r   r   |   s    8. r   r   c                      e Zd Zd Zy)TLSSocketAppenderc                n   t        j                   t         j                  t         j                        }t        j                  t        j
                  j                  t        j                               }|j                  |dd      }|j                  | j                  | j                  f       || _        y )N)purposecafileT)sockdo_handshake_on_connectsuppress_ragged_eofs)r   r   r   sslcreate_default_contextPurposeSERVER_AUTHcertifiwherewrap_socketr   r   r   r   )r   rE   contexts      r   r    z!TLSSocketAppender.open_connection   s    ==1C1CDD00//}}*G &&(,%) ' -D LL$++t'7'789DJr   N)r<   r=   r>   r    r?   r   r   rA   rA      s    
	r   rA   c                       e Zd ZdZdZdZdZ fdZd fd	Zd Z	d Z
d	 Zd
 Zd ZddZddZd Zd Zd Zd Z xZS )CallbackModuleg       @notificationzcommunity.general.logentriesTc                   t         t        |           t        s| j                  j                  d       t        s"d| _        | j                  j                  d       t        t        j                               | _        d| _        y )Nz4Unable to import ssl module. Will send over port 80.TzWThe `certifi` python module is not installed.
Disabling the Logentries callback plugin.r
   )superrQ   r   HAS_SSLr   warningHAS_CERTIFIdisabledstruuiduuid4le_jobidtimeout)r   	__class__s    r   r   zCallbackModule.__init__   sb     	nd,. MM!!"XY DMMM!!"|}DJJL) r   c                   t         t        |   |||       	 | j                  d      | _        | j                  d      | _        | j                  d      | _        | j                  d      | _        | j                  d      | _        	 | j                  d	      | _        | j                  r(t        s"d| _        | j                  j                  d       | j                          y # t        $ r/}| j                  j                  d|        d| _        Y d }~d }~ww xY w# t        $ r,}| j                  j                  d
       d| _        Y d }~d }~ww xY w)N)	task_keysvar_optionsdirectapiporttls_portuse_tlsflattenz/Missing option for Logentries callback plugin: Ttokenz[Logentries token was not provided, this is required for this callback to operate, disablingzwYou have chosen to flatten and the `flatdict` python module is not installed.
Disabling the Logentries callback plugin.)rT   rQ   set_options
get_optionapi_urlapi_portapi_tls_portrf   rg   KeyErrorr   rV   rX   rh   HAS_FLATDICT_initialize_connections)r   r`   ra   rb   r,   r^   s        r   ri   zCallbackModule.set_options   s$   nd/)Q\ek/l	!??51DL OOF3DM $
 ;D??95DL??95DL
	!1DJ
 << DMMM!!  #]  ^$$&  	!MM!!$STUSV"WX DMM	!  	!MM!!"  A DMM	!s0   A.C# D #	D,%DD	E'"EEc                   | j                   s| j                  rh| j                  j                  d| j                   d| j
                   d       t        | j                  | j                  | j
                        | _        nf| j                  j                  d| j                   d| j                          t        | j                  | j                  | j                        | _        | j                  j                          y y )NzConnecting to :z	 with TLS)r   r   r   )r   r   r   )rX   rf   r   r%   rk   rm   rA   	_appenderrl   r   r.   r   s    r   rp   z&CallbackModule._initialize_connections  s    }}||""^DLL>4CTCTBUU^#_`!24==QUQ]Q]kok|k|!}""^DLL>4==/#RS!8W[WcWcmqmzmz!{NN,,. r   c                    | j                   r6t        j                  |      }| j                  | j	                  |             y | j                  | j	                  |             y r   )rg   flatdictFlatDictemit_dump_results)r   recordresultss      r   emit_formattedzCallbackModule.emit_formatted  sG    <<''/GIId((12IId((01r   c                    |j                  d      }| j                   d| }| j                  j                  |       | j                  j                  d       y )Nr4    zSent event to logentries)rstriprh   rs   r:   r   r%   )r   ry   msgs      r   rw   zCallbackModule.emit  sI    mmD!AcU#356r   c                "    | j                   ||dS )N)r\   hostnamerz   )r\   )r   hostress      r   	_set_infozCallbackModule._set_info   s     MMtLLr   c                T    | j                  ||      }d|d<   | j                  |       y )NOKstatusr   r{   r   r   r   rz   s       r   runner_on_okzCallbackModule.runner_on_ok#  s*    ..s+ G$r   c                T    | j                  ||      }d|d<   | j                  |       y )NFAILEDr   r   )r   r   r   ignore_errorsrz   s        r   runner_on_failedzCallbackModule.runner_on_failed(  s*    ..s+$G$r   c                Z    | j                  ||      }|d= d|d<   | j                  |       y )Nrz   SKIPPEDr   r   )r   r   itemrz   s       r   runner_on_skippedz CallbackModule.runner_on_skipped-  s2    ..t,I%G$r   c                T    | j                  ||      }d|d<   | j                  |       y )NUNREACHABLEr   r   r   s       r   runner_on_unreachablez$CallbackModule.runner_on_unreachable3  s*    ..s+)G$r   c                ^    | j                  ||      }||d<   d|d<   | j                  |       y )NjidASYNC_FAILEDr   r   )r   r   r   r   rz   s        r   runner_on_async_failedz%CallbackModule.runner_on_async_failed8  s3    ..s+*G$r   c                    i }| j                   |d<   t        j                         |d<   |j                  r|j                  |d<   |j                  |d<   | j                  |       y )Nr\   
started_byplayhosts)r\   osgetloginnamer   r{   )r   r   rz   s      r   v2_playbook_on_play_startz(CallbackModule.v2_playbook_on_play_start>  sU    "mm
 "99"iiGFO::G$r   c                8    | j                   j                          y)z close connection N)rs   r#   )r   statss     r   playbook_on_statsz CallbackModule.playbook_on_statsG  s    '')r   )NNN)Fr   )r<   r=   r>   CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMECALLBACK_NEEDS_WHITELISTr   ri   rp   r{   rw   r   r   r   r   r   r   r   r   __classcell__)r^   s   @r   rQ   rQ      s[    "M2M#$'6	/27M%
%
%%
%%*r   rQ   )
__future__r   DOCUMENTATIONEXAMPLESr   r   r&   r(   rZ   rL   rW   ImportErrorru   ro   +ansible.module_utils.common.text.convertersr   r   ansible.plugins.callbackr   objectr   rH   rU   rA   SocketAppenderrQ   r?   r   r   <module>r      s    #CJ, 
    KL J 1> f > B'G3  'Nt*\ t*S  K  L\  ,NGs4   A& A3 B  &A0/A03A=<A= 	BB