B
    `1                 @   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 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_bya  
module: check
author:
  - Paul Arthur (@flowerysong)
  - Aljaz Kosir (@aljazkosir)
  - Miha Plesko (@miha-plesko)
  - Tadej Borovsak (@tadeboro)
short_description: Manage Sensu checks
description:
  - Create, update or delete Sensu Go check.
  - For more information, refer to the Sensu Go documentation at
    U(https://docs.sensu.io/sensu-go/latest/reference/checks/).
version_added: 1.0.0
extends_documentation_fragment:
  - sensu.sensu_go.requirements
  - sensu.sensu_go.auth
  - sensu.sensu_go.name
  - sensu.sensu_go.namespace
  - sensu.sensu_go.state
  - sensu.sensu_go.labels
  - sensu.sensu_go.annotations
  - sensu.sensu_go.secrets
seealso:
  - module: sensu.sensu_go.check_info
options:
  command:
    description:
      - Check command to run.
      - Required if I(state) is C(present).
    type: str
  subscriptions:
    description:
      - List of subscriptions which receive check requests.
      - Required if I(state) is C(present).
    type: list
    elements: str
  handlers:
    description:
      - List of handlers which receive check results.
    type: list
    elements: str
  interval:
    description:
      - Check request interval.
      - Cannot be used when I(cron) option is used.
    type: int
  cron:
    description:
      - Schedule check requests using crontab syntax.
      - Cannot be used when I(interval) option is used.
    type: str
  publish:
    description:
      - Enables or disables scheduled publication of check requests.
    type: bool
  timeout:
    description:
      - Check execution timeout.
    type: int
  ttl:
    description:
      - Amount of time after which a check result is considered stale.
    type: int
  stdin:
    description:
      - Enables writing of serialized JSON data to the check command's stdin.
      - Only usable with checks written specifically for Sensu Go.
    type: bool
  low_flap_threshold:
    description:
      - Low flap threshold.
    type: int
  high_flap_threshold:
    description:
      - High flap threshold.
    type: int
  runtime_assets:
    description:
      - List of runtime assets required to run the check.
    type: list
    elements: str
  check_hooks:
    description:
      - A mapping of response codes to hooks which will be run by the agent
        when that code is returned.
      - Note that the structure of this parameter is a bit different from the
        one described at
        U(https://docs.sensu.io/sensu-go/latest/reference/checks/#check-hooks-attribute).
      - See check hooks example below for more information on exact mapping
        structure.
    type: dict
  proxy_entity_name:
    description:
      - Entity name to associate this check with instead of the agent it ran on.
    type: str
  proxy_requests:
    description:
      - Allows you to assign the check to run for multiple entities according
        to their entity_attributes.
    type: dict
    suboptions:
      entity_attributes:
        description:
          - List of attribute checks for determining which proxy entities this check should be scheduled against.
        type: list
        elements: str
      splay:
        description:
          - Enables or disables splaying of check request scheduling.
        type: bool
      splay_coverage:
        description:
          - Percentage of the C(interval) over which to splay checks.
        type: int
  output_metric_format:
    description:
      - Enable parsing of metrics in the specified format from this check's
        output.
    type: str
    choices:
      - graphite_plaintext
      - influxdb_line
      - nagios_perfdata
      - opentsdb_line
  output_metric_handlers:
    description:
      - List of handlers which receive check results. I'm not sure why this exists.
    type: list
    elements: str
  round_robin:
    description:
      -  An array of environment variables to use with command execution.
    type: bool
  env_vars:
    description:
      - A mapping of environment variable names and values to use with command execution.
    type: dict
a_  
- name: Check executing command every 30 seconds
  sensu.sensu_go.check:
    name: check
    command: check-cpu.sh -w 75 -c 90
    subscriptions:
      - checks
    interval: 30
    publish: yes

- name: Check executing command with cron scheduler
  sensu.sensu_go.check:
    name: check
    command: check-cpu.sh -w 75 -c 90
    subscriptions:
      - systems
    handlers:
      - slack
    cron: "* * * * *"
    publish: yes

- name: Ad-hoc scheduling
  sensu.sensu_go.check:
    name: check
    command: check-cpu.sh -w 75 -c 90
    subscriptions:
      - systems
    handlers:
      - slack
    interval: 60
    publish: no

- name: Report events under proxy entity name instead of agent entity
  sensu.sensu_go.check:
    name: check
    command: http_check.sh https://sensu.io
    subscriptions:
      - proxy
    handlers:
      - slack
    interval: 60
    proxy_entity_name: sensu-site
    round_robin: yes
    publish: yes

- name: Event that triggers hooks
  sensu.sensu_go.check:
    name: check
    command: http_check.sh https://sensu.io
    subscriptions: [ proxy ]
    # The upstream JSON payload for the hooks below would look like this:
    #
    #   "check_hooks": [
    #     {"0": ["passing-hook", "always-run-this-hook"]},
    #     {"critical": ["failing-hook", "always-run-this-hook"]}
    #   ]
    #
    # Ansible task simplifies this structure into a simple mapping:
    check_hooks:
      "0":
        - passing-hook
        - always-run-this-hook
      critical:
        - failing-hook
        - always-run-this-hook

- name: Remove check
  sensu.sensu_go.check:
    name: my-check
    state: absent
a  
object:
  description: Object representing Sensu check.
  returned: success
  type: dict
  sample:
    metadata:
      name: check_minimum
      namespace: default
    command: collect.sh
    handlers:
      - slack
    interval: 10
    publish: true
    subscriptions:
      - system
)AnsibleModule   )	argumentserrorsutilsc             C   sd   | j }|d }|r8|ddr8|dd kr8| jdd |d dkr`|d	 s`|d
 s`| jdd d S )Nproxy_requestssplayFsplay_coveragezBsplay is true but all of the following are missing: splay_coverage)msgstatepresentintervalcronz0one of the following is required: interval, cron)paramsget	fail_json)moduler   r    r   g/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/sensu/sensu_go/plugins/modules/check.pyvalidate_module_params   s    r   c             C   s$   t | |pg t ||pg kS )N)setr   )currentdesiredkeyr   r   r   do_sets_differ  s    r   c             C   sD   d|krdS |  dpi } |d }d|kr6t| |dpBt| |dS )Nr   Fentity_attributes)r   r   r   	do_differ)r   r   r   r   r   do_proxy_requests_differ  s    r"   c             C   sb   d|krdS t | dpg } tdd |  D } t |d }tdd | D }| |kS )Ncheck_hooksFc             s   s   | ]\}}|t |fV  qd S )N)r   ).0kvr   r   r   	<genexpr>!  s    z(do_check_hooks_differ.<locals>.<genexpr>c             s   s   | ]\}}|t |fV  qd S )N)r   )r$   r%   r&   r   r   r   r'   $  s    )r   Zsingle_item_dicts_to_dictr   dictitems)r   r   r   r   r   do_check_hooks_differ  s    r*   c             C   sx   t | |dddddddd
pvt | |pvt| |pvt| |dpvt| |dpvt| |dpvt| |pvt| |dpvt| |dS )	Nr   subscriptionshandlersruntime_assetsr#   output_metric_handlersenv_varssecrets)r   r!   Zdo_secrets_differr"   r   r*   )r   r   r   r   r   r!   )  s    


r!   c             C   s   t | ddddddddd	d
ddddddd}| d rLt | d ddd|d< | d rft| d |d< | d rt| d |d< |S )Ncommandr   r,   high_flap_thresholdr   low_flap_thresholdoutput_metric_formatr.   proxy_entity_namepublishround_robinr-   r0   stdinr+   timeoutttlr   r    r   r   r#   r/   )r	   Zget_mutation_payloadZget_spec_payloadr   Zdict_to_single_item_dictsZdict_to_key_value_strings)r   payloadr   r   r   build_api_payload;  s8    r<   c        	      C   s  ddddgfg} dg}t d| |ttdddd	d
ddt tdddtdddtddt tddtddtddtddtddtddtddtdddtddt tdttdddtddtddddtddddgdtdddtdddd}t| t|jd }t|jd d|jd }t	|j}y2t
|jd ||||jt\}}|j||d W n6 tjk
r } z|jt|d W d d }~X Y nX d S ) Nr   r   r+   r1   )r   r   Tauthnamelabelsannotations	namespacer0   liststr)typeelementsint)rD   boolr(   )r    r   r   )rD   optionsZnagios_perfdataZgraphite_plaintextZinfluxdb_lineZopentsdb_line)choices)r1   r+   r,   r   r   r6   r9   r:   r8   r/   r3   r2   r-   r#   r5   r   r4   r.   r7   )Zsupports_check_moderequired_ifmutually_exclusiveZargument_specZchecks)changedobject)r   )r   r(   r	   Zget_specr   Zget_sensu_clientr   r   Zbuild_core_v2_pathr<   syncZ
check_moder!   Z	exit_jsonr
   Errorr   rC   )	rJ   rK   r   clientpathr;   rL   checker   r   r   main`  s    

rT   __main__N)
__future__r   r   r   rD   __metaclass__ZANSIBLE_METADATAZDOCUMENTATIONZEXAMPLESZRETURNZansible.module_utils.basicr   Zmodule_utilsr	   r
   r   r   r   r"   r*   r!   r<   rT   __name__r   r   r   r   <module>   s&    H%`