
    Vh1                         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 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_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                     | j                   }|d   }|r5|j                  dd      r#|j                  d      | j                  d       |d   dk(  r|d	   s|d
   s| j                  d       y y y y )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   s      h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/sensu/sensu_go/plugins/modules/check.pyvalidate_module_paramsr      s    ]]F,-N>--gu=/08abg)#VJ-?6&>OP DR-?#    c                 |    t        | j                  |      xs g       t        |j                  |      xs g       k7  S N)setr   )currentdesiredkeys      r   do_sets_differr'     s3    w{{3%2&#gkk#.>.D"*EEEr    c                     d|vry| j                  d      xs i } |d   }d|v xr t        | |d      xs t        j                  | |d      S )Nr   Fentity_attributes)r   r'   r   	do_differr$   r%   s     r   do_proxy_requests_differr,     sf    w&kk*+1rG&'G  7* B7G-@A	? 	*=>r    c                    d|vryt        j                  | j                  d      xs g       } t        d | j	                         D              } t        j                  |d         }t        d |j	                         D              }| |k7  S )Ncheck_hooksFc              3   <   K   | ]  \  }}|t        |      f  y wr"   r#   .0kvs      r   	<genexpr>z(do_check_hooks_differ.<locals>.<genexpr>!       ;41aAs1v;;   c              3   <   K   | ]  \  }}|t        |      f  y wr"   r0   r1   s      r   r5   z(do_check_hooks_differ.<locals>.<genexpr>$  r6   r7   )r   single_item_dicts_to_dictr   dictitemsr+   s     r   do_check_hooks_differr<     sw    G#--gkk-.H.NBOG;7==?;;G--gm.DEG;7==?;;Ggr    c                 <   t        j                  | |dddddddd
      xs} t        j                  | |      xse t        | |      xsW t	        | |d      xsH t	        | |d      xs9 t	        | |d      xs* t        | |      xs t	        | |d      xs t	        | |d      S )	Nr   subscriptionshandlersruntime_assetsr.   output_metric_handlersenv_varssecrets)r   r*   do_secrets_differr,   r'   r<   r+   s     r   r*   r*   )  s    W.m-E		
 	5
 	1	5 	!'2	5 	w9	5 	w4	5 	w)9:	5 	gw/	5 	w)AB	5 	w4r    c                    t        j                  | ddddddddd	d
ddddddd      }| d   rt        j                  | d   ddd      |d<   | d   rt        j                  | d         |d<   | d   rt        j
                  | d         |d<   |S )Ncommandr   r?   high_flap_thresholdr   low_flap_thresholdoutput_metric_formatrA   proxy_entity_namepublishround_robinr@   rC   stdinr>   timeoutttlr   r)   r   r   r.   rB   )r   get_mutation_payloadget_spec_payloadr   dict_to_single_item_dictsdict_to_key_value_strings)r   payloads     r   build_api_payloadrU   ;  s    ,, %G* $-$>$>#$*:%
 !
 m!&!@!@AV!Wj#==fZ>PQ
Nr    c                     ddddgfg} dg}t        d| |t        t        j                  dddd	d
dd      fi dt               dt        dd      dt        dd      dt        d      dt               d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               d!t        dt        t        dd      t        d      t        d      "      #      d$t        g d%&      d't        dd      d(t        d      )      }t	        |       t        j
                  |j                  d         }t        j                  |j                  d   d*|j                  d         }t        |j                        }	 t        j                  |j                  d   ||||j                  t              \  }}|j                  ||+       y # t        j                  $ r%}|j!                  t#        |      ,       Y d }~y d }~ww xY w)-Nr   r   r>   rF   )r   r   Tauthnamelabelsannotations	namespacerC   liststr)typeelementsr?   r   int)r^   r   rK   boolrN   rO   rM   rB   r:   rH   rG   r@   r.   rJ   r   )r)   r   r   )r^   optionsrI   )nagios_perfdatagraphite_plaintextinfluxdb_lineopentsdb_line)choicesrA   rL   )supports_check_moderequired_ifmutually_exclusiveargument_specchecks)changedobjectr   )r   r:   r   get_specr   get_sensu_clientr   r   build_core_v2_pathrU   sync
check_moder*   	exit_jsonr   Errorr   r]   )	ri   rj   r   clientpathrT   rm   checkes	            r   mainrz   `  s   	)oy9:K // -=+C

 FC
 eC
 eC
 C
 C
  !C
& 'C
, -C
2 3C
8 9C
>  $ ?C
D !%!EC
J  eKC
P QC
V #fWC
X  &*#e' # $("$
YC
t "&c"uC
z $(e${C
@ AC
	HFR 6"''f(=>F##k"HfmmF.CD  .G%MM'"FD'6;L;L
 	7<< %SV$$%s   AH I2II__main__N)
__future__r   r   r   r^   __metaclass__ANSIBLE_METADATADOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   module_utilsr   r   r   r   r'   r,   r<   r*   rU   rz   __name__ r    r   <module>r      s    A @  ! IVFP
$ 5 3 3	QF 
$"J]%@ zF r    