B
    `7&                 @   s   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d Zdd Zdd Zdd Zdd Zdd Zdd Zedkre  dS )    )absolute_importdivisionprint_functionz1.1ZstableinterfaceZ	certified)metadata_versionstatusZ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             C   s8   t |d|}| |}|jdkr2td||jS )NZ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resp r   g/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/sensu/sensu_go/plugins/modules/event.py	get_check   s
    

r   c             C   s8   t |d|}| |}|jdkr2td||jS )Nentitiesr   z0Entity with name '{0}' does not exist on remote.)r   r   r   r   r
   r   r   r   )r   r   entityZentity_pathr   r   r   r   
get_entity   s
    

r"   c             C   s$   |sd S t j|f|  | d< d S )NZmetrics)r	   get_spec_payloadkeys)payloadmetric_attributesr   r   r   &_update_payload_with_metric_attributes   s    r'   c             C   sF   |sd S |d r t |d  |d< tj|f|  }| d | d S )Nr   r   )
STATUS_MAPr	   r#   r$   update)r%   check_attributesZfiltered_attributesr   r   r   %_update_payload_with_check_attributes   s    r+   c             C   sn   t |d}t|d d|d< t| |d |d |d< t| |d |d |d< t||d  t||d  |S )	N	timestampr   )r   metadatar!   r   r*   r&   )r	   r#   dictr"   r   r+   r'   )r   paramsr%   r   r   r   _build_api_payload   s    r0   c             C   s   |st | || d|fS )NT)r   put)r   pathr%   
check_moder   r   r   
send_event  s    r4   c              C   sV  t dttddtddtddtddtdttddtddtd	dd
tddtddt tdddgdtddddgdtddd	dtdttd	dd
td	dd
dddd} t| jd }t| jd d| jd | jd }y2t|| j}t	|||| j
\}}| j||d W n6 tjk
rP } z| jt|d W d d }~X Y nX d S )NTauthr   int)type)requiredr.   floatlist)r7   elementsZpassingZfailingZflapping)choicesr   r   r   r   )	durationZexecutedhistoryZissuedZlast_okoutputstater   Ztotal_state_change)r7   optionsstr)handlersZpoints)r,   r!   r   r*   r&   )Zsupports_check_modeZargument_speceventsr!   r   )changedobject)msg)r   r.   r	   Zget_specZget_sensu_clientr/   r   r   r0   r4   r3   Z	exit_jsonr
   ErrorZ	fail_jsonrB   )moduler   r2   r%   rE   eventer   r   r   main
  s\    
rL   __main__N)
__future__r   r   r   r7   __metaclass__ZANSIBLE_METADATAZDOCUMENTATIONZEXAMPLESZRETURNZansible.module_utils.basicr   Zmodule_utilsr	   r
   r   r(   r   r"   r'   r+   r0   r4   rL   __name__r   r   r   r   <module>   s,   g&,D