
    Vh7&                         d dl mZmZmZ eZddgddZdZdZdZ	d d	l
mZ d
dlmZmZmZ d dd
ddZd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionz1.1stableinterface	certified)metadata_versionstatussupported_byaK  
module: event
author:
  - Paul Arthur (@flowerysong)
  - Aljaz Kosir (@aljazkosir)
  - Manca Bizjak (@mancabizjak)
  - Tadej Borovsak (@tadeboro)
short_description: Manage Sensu events
description:
  - Send a synthetic event to Sensu.
  - For more information, refer to the Sensu documentation at
    U(https://docs.sensu.io/sensu-go/latest/reference/events/).
version_added: 1.0.0
extends_documentation_fragment:
  - sensu.sensu_go.requirements
  - sensu.sensu_go.auth
  - sensu.sensu_go.namespace
seealso:
  - module: sensu.sensu_go.event_info
notes:
  - Metric events bypass the store and are sent off to the event pipeline and corresponding event
    handlers. Read more about this at
    U(https://docs.sensu.io/sensu-go/latest/reference/events/#metric-only-events).
options:
  timestamp:
    description:
      - UNIX time at which the event occurred.
    type: int
  entity:
    description:
      - Name of the entity associated with this event. It must exist before event creation.
    type: str
    required: true
  check:
    description:
      - Name of the check associated with this event. It must exist before event creation.
    type: str
    required: true
  check_attributes:
    type: dict
    description:
      - Additional check parameters. Find out more at
        U(https://docs.sensu.io/sensu-go/latest/reference/events/#check-attributes).
    suboptions:
      duration:
        description:
          - Command execution time in seconds.
        type: float
      executed:
        description:
          - Time that the check request was executed.
        type: int
      history:
        description:
          - Check status history for the last 21 check executions.
        type: list
        elements: dict
      issued:
        description:
          - Time that the check request was issued in seconds since the Unix epoch.
        type: int
      last_ok:
        description:
          - The last time that the check returned an OK status (0) in seconds since the Unix epoch.
        type: int
      output:
        description:
          - The output from the execution of the check command.
        type: str
      state:
        description:
          - The state of the check.
        choices: [ "passing", "failing", "flapping" ]
        type: str
      status:
        description:
          - Exit status code produced by the check.
        choices: [ "ok", "warning", "critical", "unknown" ]
        type: str
      total_state_change:
        description:
          - The total state change percentage for the check's history.
        type: int
  metric_attributes:
    type: dict
    description:
      - Metric attributes. Find out more at
        U(https://docs.sensu.io/sensu-go/latest/reference/events/#metric-attributes).
    suboptions:
      handlers:
        description:
          - An array of Sensu handlers to use for events created by the check.
            Each array item must be a string.
        type: list
        elements: str
      points:
        description:
          - Metric data points including a name, timestamp, value, and tags.
        type: list
        elements: dict
a  
- name: Create an event
  sensu.sensu_go.event:
    auth:
      url: http://localhost:8080
    entity: awesome_entity
    check: awesome_check
    check_attributes:
      duration: 1.945
      executed: 1522100915
      history:
        - executed: 1552505193
          status: 1
      issued: 1552506034
      last_ok: 1552506033
      output: '10'
      state: 'passing'
      status: 'ok'
      total_state_change: 0
    metric_attributes:
      handlers:
        - handler1
        - handler2
      points:
        - name: "sensu-go-sandbox.curl_timings.time_total"
          tags:
            - name: "response_time_in_ms"
              value: 101
          timestamp: 1552506033
          value: 0.005
        - name: "sensu-go-sandbox.curl_timings.time_namelookup"
          tags:
            - name: "namelookup_time_in_ms"
              value: 57
          timestamp: 1552506033
          value: 0.004
a  
object:
  description: Object representing Sensu event (deprecated).
  returned: success
  type: dict
  sample:
    metadata:
      namespace: default
    check:
      check_hooks: null
      command: check-cpu.sh -w 75 -c 90
      duration: 1.07055808
      env_vars: null
      executed: 1552594757
      handlers: []
      high_flap_threshold: 0
      history:
      - executed: 1552594757
        status: 0
      interval: 60
      metadata:
        name: check-cpu
        namespace: default
      occurrences: 1
      occurrences_watermark: 1
      output: CPU OK - Usage:3.96
      subscriptions:
        - linux
      timeout: 0
      total_state_change: 0
      ttl: 0
    entity:
      deregister: false
      deregistration: {}
      entity_class: agent
      last_seen: 1552594641
      metadata:
        name: sensu-centos
        namespace: default
    timestamp: 1552594758
    id: 3a5948f3-6ffd-4ea2-a41e-334f4a72ca2f
    sequence: 1
)AnsibleModule   )	argumentserrorsutils      okwarningcriticalunknownc                     t        j                  |d|      }| j                  |      }|j                  dk7  r$t	        j
                  dj                  |            |j                  S )Nchecks   z/Check with name '{0}' does not exist on remote.r   build_core_v2_pathgetr	   r   	SyncErrorformatjson)client	namespacecheck
check_pathresps        h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/sensu/sensu_go/plugins/modules/event.py	get_checkr&      sX    )))XuEJ::j!D{{cPWWX]^__99    c                     t        j                  |d|      }| j                  |      }|j                  dk7  r$t	        j
                  dj                  |            |j                  S )Nentitiesr   z0Entity with name '{0}' does not exist on remote.r   )r    r!   entityentity_pathr$   s        r%   
get_entityr,      sX    **9j&IK::k"D{{cQXXY_`aa99r'   c                 X    |sy t        j                  |g|j                          | d<   y )Nmetrics)r   get_spec_payloadkeys)payloadmetric_attributess     r%   &_update_payload_with_metric_attributesr3      s-    "334EaHYH^H^H`aGIr'   c                     |sy |d   rt         |d      |d<   t        j                  |g|j                          }| d   j	                  |       y )Nr	   r"   )
STATUS_MAPr   r/   r0   update)r1   check_attributesfiltered_attributess      r%   %_update_payload_with_check_attributesr9      s[    !%/0@0J%K"#445E`HXH]H]H_`G/0r'   c                     t        j                  |d      }t        |d         |d<   t        | |d   |d         |d<   t	        | |d   |d         |d<   t        ||d          t        ||d          |S )	N	timestampr!   )r!   metadatar*   r"   r7   r2   )r   r/   dictr,   r&   r9   r3   )r    paramsr1   s      r%   _build_api_payloadr?      s    ((=G%GJ #66++>x@PQGH )<fWoNGG)'6:L3MN*7F;N4OPNr'   c                 <    |st        j                  | ||       d|fS )NT)r   put)r    pathr1   
check_modes       r%   
send_eventrD     s    		&$(=r'   c                     t        dt        t        j                  dd      t        d      t        d      t        d      t        dt        t        d      t        d      t        d	d
      t        d      t        d      t               t        g d      t        g d      t        d      	            t        dt        t        d	d
      t        d	d
                              } t        j                  | j
                  d         }t        j                  | j
                  d   d| j
                  d   | j
                  d         }	 t        || j
                        }t        |||| j                        \  }}| j                  ||       y # t        j                  $ r%}| j                  t        |             Y d }~y d }~ww xY w)NTauthr!   int)type)requiredr=   floatlist)rH   elements)passingfailingflapping)choicesr   )	durationexecutedhistoryissuedlast_okoutputstater	   total_state_change)rH   optionsstr)handlerspoints)r;   r*   r"   r7   r2   )supports_check_modeargument_speceventsr*   r"   )changedobject)msg)r   r=   r   get_specget_sensu_clientr>   r   r   r?   rD   rC   	exit_jsonr   Error	fail_jsonrZ   )moduler    rB   r1   r`   eventes          r%   mainrk   
  s    v{3&&%!!$ "" !#f  " !"  6 B   H (,"(/< #!#!&  #!'	G0
3Fj ''f(=>F##k"HfmmH.EgD
%$VV]];#FD'6;L;LM7<< %SV$$%s   AF G/GG__main__N)
__future__r   r   r   rH   __metaclass__ANSIBLE_METADATADOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utilsr   r   r   r5   r&   r,   r3   r9   r?   rD   rk   __name__ r'   r%   <module>rw      s    A @  ! dL$L*
X 5 3 3 	
b1
A%H zF r'   