
    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)    )annotationsa5  
name: splunk
type: notification
short_description: Sends task result events to Splunk HTTP Event Collector
author: "Stuart Hirst (!UNKNOWN) <support@convergingdata.com>"
description:
  - This callback plugin will send task results as JSON formatted events to a Splunk HTTP collector.
  - The companion Splunk Monitoring & Diagnostics App is available here U(https://splunkbase.splunk.com/app/4023/).
  - Credit to "Ryan Currah (@ryancurrah)" for original source upon which this is based.
requirements:
  - Whitelisting this callback plugin
  - 'Create a HTTP Event Collector in Splunk'
  - 'Define the URL and token in C(ansible.cfg)'
options:
  url:
    description: URL to the Splunk HTTP collector source.
    type: str
    env:
      - name: SPLUNK_URL
    ini:
      - section: callback_splunk
        key: url
  authtoken:
    description: Token to authenticate the connection to the Splunk HTTP collector.
    type: str
    env:
      - name: SPLUNK_AUTHTOKEN
    ini:
      - section: callback_splunk
        key: authtoken
  validate_certs:
    description: Whether to validate certificates for connections to HEC. It is not recommended to set to V(false) except
      when you are sure that nobody can intercept the connection between this plugin and HEC, as setting it to V(false) allows
      man-in-the-middle attacks!
    env:
      - name: SPLUNK_VALIDATE_CERTS
    ini:
      - section: callback_splunk
        key: validate_certs
    type: bool
    default: true
    version_added: '1.0.0'
  include_milliseconds:
    description: Whether to include milliseconds as part of the generated timestamp field in the event sent to the Splunk
      HTTP collector.
    env:
      - name: SPLUNK_INCLUDE_MILLISECONDS
    ini:
      - section: callback_splunk
        key: include_milliseconds
    type: bool
    default: false
    version_added: 2.0.0
  batch:
    description:
      - Correlation ID which can be set across multiple playbook executions.
    env:
      - name: SPLUNK_BATCH
    ini:
      - section: callback_splunk
        key: batch
    type: str
    version_added: 3.3.0
a%  
examples: >-
  To enable, add this to your ansible.cfg file in the defaults block
    [defaults]
    callback_whitelist = community.general.splunk
  Set the environment variable
    export SPLUNK_URL=http://mysplunkinstance.datapaas.io:8088/services/collector/event
    export SPLUNK_AUTHTOKEN=f23blad6-5965-4537-bf69-5b5a545blabla88
  Set the ansible.cfg variable in the callback_splunk block
    [callback_splunk]
    url = http://mysplunkinstance.datapaas.io:8088/services/collector/event
    authtoken = f23blad6-5965-4537-bf69-5b5a545blabla88
N)basename)open_url)AnsibleJSONEncoder)CallbackBase)nowc                      e Zd Zd Zd Zy)SplunkHTTPCollectorSourcec                .   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    m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/callback/splunk.py__init__z"SplunkHTTPCollectorSource.__init__h   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<   ||
d<   |rd	}nd
}t               j                  |      |
d<   | j                  |
d<   | j                  |
d<   | j                  |
d<   ||
d<   | j                  |
d<   | j                  |
d<   |j                  j                  |
d<   | j                   |
d<   |	|
d<   |j                   |
d<   |j"                  |
d<   t%        j&                  d|
it(        d      }t+        ||dd| dd|       y )Nargs_ansible_check_modeT_ansible_versionr   r   batchstatusz%Y-%m-%d %H:%M:%S.%f +0000z%Y-%m-%d %H:%M:%S +0000	timestampr   r   r   runtimer   r   ansible_hostr   ansible_roleansible_taskansible_resultevent)cls	sort_keyszapplication/jsonzSplunk )zContent-typeAuthorizationPOST)headersmethodvalidate_certs)_task_fieldsgetr   r   _task_roler   _uuidr   r   strftimer   r   r   _hostnamer   _resultjsondumpsr   r   )r   url	authtokenr3   include_millisecondsr$   stateresultr'   r)   datatime_formatjsondatas                r   
send_eventz$SplunkHTTPCollectorSource.send_eventq   s   v&**+@ATI&*D#v&**+=>##F+//0BC   <<v||112LLV(((##F+||))V,,Y!DMX6K3KENN;7[yyV!__\yyV!Y"&"6"6%)%<%<!"%||00^#'#8#8 +^%22^!' ::wo3EQUV 2#*9+!6 )		
r   N)__name__
__module____qualname__r   rG    r   r   r
   r
   g   s    &5
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.splunkTc                    t         t        |   |       i | _        d | _        d | _        d | _        d | _        d | _        t               | _
        y )N)display)superrM   r   start_datetimesr?   r@   r3   rA   r$   r
   splunk)r   rP   	__class__s     r   r   zCallbackModule.__init__   sL    nd,W,=!"$(!
/1r   c                z    t               | j                  |j                  j                     z
  j	                         S N)r   rR   r6   r8   total_seconds)r   rC   s     r   _runtimezCallbackModule._runtime   s0    E  !3!345
-/	r   c                   t         t        |   |||       | j                  d      | _        | j                  "d| _        | j                  j                  d       | j                  d      | _        | j                  "d| _        | j                  j                  d       | j                  d      | _	        | j                  d      | _
        | j                  d	      | _        y )
N)	task_keysvar_optionsdirectr?   TzSplunk HTTP collector source URL was not provided. The Splunk HTTP collector source URL can be provided using the `SPLUNK_URL` environment variable or in the ansible.cfg file.r@   zSplunk HTTP collector requires an authenticationtoken. The Splunk HTTP collector authentication token can be provided using the `SPLUNK_AUTHTOKEN` environment variable or in the ansible.cfg file.r3   rA   r$   )rQ   rM   set_options
get_optionr?   disabled_displaywarningr@   r3   rA   r$   )r   rZ   r[   r\   rT   s       r   r]   zCallbackModule.set_options   s    nd/)<G7= 	0 	? ??5)88 DMMM!! #= > 5>>! DMMM!! #= > #oo.>?$(OO4J$K!__W-
r   c                L    t        |j                        | j                  _        y rV   )r   
_file_namerS   r   )r   playbooks     r   v2_playbook_on_startz#CallbackModule.v2_playbook_on_start   s    '/0C0C'D$r   c                F    t               | j                  |j                  <   y rV   r   rR   r8   )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 rV   rg   )r   rh   s     r   !v2_playbook_on_handler_task_startz0CallbackModule.v2_playbook_on_handler_task_start   rk   r   c                    | j                   j                  | j                  | j                  | j                  | j
                  | j                  d|| j                  |             y )NOKrS   rG   r?   r@   r3   rA   r$   rX   r   rC   kwargss      r   v2_runner_on_okzCallbackModule.v2_runner_on_ok   sM    HHNN%%JJMM&!		
r   c                    | j                   j                  | j                  | j                  | j                  | j
                  | j                  d|| j                  |             y )NSKIPPEDrp   rq   s      r   v2_runner_on_skippedz#CallbackModule.v2_runner_on_skipped   sM    HHNN%%JJMM&!		
r   c                    | j                   j                  | j                  | j                  | j                  | j
                  | j                  d|| j                  |             y NFAILEDrp   rq   s      r   v2_runner_on_failedz"CallbackModule.v2_runner_on_failed   M    HHNN%%JJMM&!		
r   c                    | j                   j                  | j                  | j                  | j                  | j
                  | j                  d|| j                  |             y rx   rp   rq   s      r   runner_on_async_failedz%CallbackModule.runner_on_async_failed  r{   r   c                    | j                   j                  | j                  | j                  | j                  | j
                  | j                  d|| j                  |             y )NUNREACHABLErp   rq   s      r   v2_runner_on_unreachablez'CallbackModule.v2_runner_on_unreachable  sM    HHNN%%JJMM&!		
r   rV   )NNN)rH   rI   rJ   CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMECALLBACK_NEEDS_WHITELISTr   rX   r]   re   rj   rm   rs   rv   rz   r}   r   __classcell__)rT   s   @r   rM   rM      sQ    "M.M#2.>E11









r   rM   )
__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
   rM   rK   r   r   <module>r      sS    #?B      . 4 1
?
 ?
Dx
\ x
r   