
    Vh/4                     t    d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZ d
dZd Zed	k(  r e        yy)    )absolute_importdivisionprint_functiona  
module: sensu_check
short_description: Manage Sensu checks
description:
  - Manage the checks that should be run on a machine by I(Sensu).
  - Most options do not have a default and will not be added to the check definition unless specified.
  - All defaults except O(path), O(state), O(backup) and O(metric) are not managed by this module, they are simply specified
    for your convenience.
deprecated:
  removed_in: 13.0.0
  why: Sensu Core and Sensu Enterprise products have been End of Life since 2019/20.
  alternative: Use Sensu Go and its accompanying collection C(sensu.sensu_go).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: str
    description:
      - The name of the check.
      - This is the key that is used to determine whether a check exists.
    required: true
  state:
    type: str
    description:
      - Whether the check should be present or not.
    choices: ['present', 'absent']
    default: present
  path:
    type: str
    description:
      - Path to the JSON file of the check to be added/removed.
      - Will be created if it does not exist (unless O(state=absent)).
      - The parent folders need to exist when O(state=present), otherwise an error will be thrown.
    default: /etc/sensu/conf.d/checks.json
  backup:
    description:
      - Create a backup file (if yes), including the timestamp information so you can get the original file back if you somehow
        clobbered it incorrectly.
    type: bool
    default: false
  command:
    type: str
    description:
      - Path to the sensu check to run (not required when O(state=absent)).
  handlers:
    type: list
    elements: str
    description:
      - List of handlers to notify when the check fails.
  subscribers:
    type: list
    elements: str
    description:
      - List of subscribers/channels this check should run for.
      - See sensu_subscribers to subscribe a machine to a channel.
  interval:
    type: int
    description:
      - Check interval in seconds.
  timeout:
    type: int
    description:
      - Timeout for the check.
      - If not specified, it defaults to 10.
  ttl:
    type: int
    description:
      - Time to live in seconds until the check is considered stale.
  handle:
    description:
      - Whether the check should be handled or not.
      - Default is V(false).
    type: bool
  subdue_begin:
    type: str
    description:
      - When to disable handling of check failures.
  subdue_end:
    type: str
    description:
      - When to enable handling of check failures.
  dependencies:
    type: list
    elements: str
    description:
      - Other checks this check depends on, if dependencies fail handling of this check will be disabled.
  metric:
    description:
      - Whether the check is a metric.
    type: bool
    default: false
  standalone:
    description:
      - Whether the check should be scheduled by the sensu client or server.
      - This option obviates the need for specifying the O(subscribers) option.
      - Default is V(false).
    type: bool
  publish:
    description:
      - Whether the check should be scheduled at all.
      - You can still issue it using the sensu API.
      - Default is V(false).
    type: bool
  occurrences:
    type: int
    description:
      - Number of event occurrences before the handler should take action.
      - If not specified, defaults to 1.
  refresh:
    type: int
    description:
      - Number of seconds handlers should wait before taking second action.
  aggregate:
    description:
      - Classifies the check as an aggregate check, making it available using the aggregate API.
      - Default is V(false).
    type: bool
  low_flap_threshold:
    type: int
    description:
      - The low threshold for flap detection.
  high_flap_threshold:
    type: int
    description:
      - The high threshold for flap detection.
  custom:
    type: dict
    description:
      - A hash/dictionary of custom parameters for mixing to the configuration.
      - You cannot rewrite other module parameters using this.
  source:
    type: str
    description:
      - The check source, used to create a JIT Sensu client for an external resource (for example a network switch).
author: "Anders Ingemann (@andsens)"
a|  
# Fetch metrics about the CPU load every 60 seconds,
# the sensu server has a handler called 'relay' which forwards stats to graphite
- name: Get cpu metrics
  community.general.sensu_check:
    name: cpu_load
    command: /etc/sensu/plugins/system/cpu-mpstat-metrics.rb
    metric: true
    handlers: relay
    subscribers: common
    interval: 60

# Check whether nginx is running
- name: Check nginx process
  community.general.sensu_check:
    name: nginx_running
    command: /etc/sensu/plugins/processes/check-procs.rb -f /var/run/nginx.pid
    handlers: default
    subscribers: nginx
    interval: 60

# Stop monitoring the disk capacity.
# Note that the check will still show up in the sensu dashboard,
# to remove it completely you need to issue a DELETE request to the sensu api.
- name: Check disk
  community.general.sensu_check:
    name: check_disk_capacity
    state: absent
N)AnsibleModule)	to_nativec                 p
   d}g }d }	 	 t        |d      }t        j                  |      }|r|j                          	 d
vr2|dk(  r|j                  d       ||fS i |d
<   d}|j                  d       |dk(  r ||d
   v r|d
   |= d}|j                  d       |dk(  r||d
   vri }||d
   |<   d}|j                  d       n|d
   |   }g d}|D ]  }| j                  |   P||vs||   | j                  |   k7  s,| j                  |   ||<   d}|j                  dj                  |             b||v sg||= d}|j                  dj                  |              | j                  d   r| j                  d   }t        |j                               t        |g dz         z  }|r-dj                  t!        |            }
| j                  |
	       |j#                         D ]i  \  }}||d
   |   v r2|d
   |   |   |k(  sGd}|j                  dj                  |             n#d}|j                  dj                  |             |||<   k ||j                         z  }t        |d
   |   j                               t        |g dz         z
  D ](  }d}|j                  dj                  |             ||= * | j                  d   r$d|vs|d   dk7  rd|d<   d}|j                  d       | j                  d   sd|v r|d= d}|j                  d       | j                  d   S| j                  d    D| j                  d   | j                  d    d!}d"|vs|d"   |k7  r3||d"<   d}|j                  d#       nd"|v r|d"= d}|j                  d$       |rl| j$                  s`|r| j'                  |       	 	 t        |d%      }|j)                  t        j*                  |d&      d'z          |r|j                          ||fS ||fS # t        $ r}}	|	j                  dk(  r5|dk(  r-|j                  d       ||fcY d }	~	|r|j                          S S i }n.| j                  t        |	      t        j                                Y d }	~	bd }	~	wt        $ r( dj                  |      }
| j                  |
	       Y w xY w# |r|j                          w w xY w# t        $ r9}	| j                  t        |	      t        j                                Y d }	~	'd }	~	ww xY w# |r|j                          w w xY w)(NFr   absentz(file did not exist and state is `absent')msg	exceptionz{path} contains invalid JSON)pathr   checksz4`checks' section did not exist and state is `absent'Tz`checks' section did not existz'check was present and state is `absent'presentz'check was absent and state is `present')commandhandlerssubscribersintervaltimeoutttlhandledependencies
standalonepublishoccurrencesrefresh	aggregatelow_flap_thresholdhigh_flap_thresholdsourcez&`{opt}' did not exist or was different)optz`{opt}' was removedcustom)typesubduesubdue_begin
subdue_endz^You can't overwriting standard module parameters via "custom". You are trying overwrite: {opt}z `custom param {opt}' was changedz`custom param {opt}' was addedz `custom param {opt}' was deletedmetricr$   z&`type' was not defined or not `metric'z`type' was definedr&   r'   )beginendr%   z'`subdue' did not exist or was differentz`subdue' was removedw)indent
)openjsonloadIOErrorerrnoappendclose	fail_jsonr   	traceback
format_exc
ValueErrorformatparamssetkeyslistitems
check_modebackup_localwritedumps)moduler   namestatebackupchangedreasonsstreamconfiger   checksimple_optsr"   custom_paramsoverwrited_fieldskvr%   s                      q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sensu_check.pysensu_checkrS      s   GGF	&$_FYYv&F LLNvHNNSTG##x896(##x &GNNEF	vh''E%*F8T"GNNEF8$T*E"  
	KC}}S!-e#uSzV]]35G'G!'s!3E#J"GNN#L#S#SX[#S#\]%<c
"GNN#9#@#@S#@#IJ
	K ==""MM(3M #M$6$6$8 9CN~@~<  w~~  DH  IZ  D[~  \  S )%++- 1x(..!(+D1!49"&'J'Q'QVW'Q'XY"GNN#D#K#KPQ#K#RSa =--//K vh'-2245KJz<z8{{ 	CGNN>EE#ENOc
	
 =="U"eFmx&? (fIJ}}X&6U?fGNN01==(4|9T9`%}}^<#]]<8F u$h6(A"(hIJ5 (O67v((%	UdCTZZq9D@A G7GA  	Uww!|H$NN#NO"G++ LLN    Yq\Y=Q=Q=S T 	&077T7BC%	& LLN \  U  Yq\Y=Q=Q=S TTU  sq   !P <5S 	S (R<S =S 1RS 0S <S ?S  S S	T$.TT  TT   T5c            	      r   i ddddddddddd	d	d
gddddddddidddddddddddidddidddidddidddidddidddddddddddidddiddiddiddiddiddidd iddid!} ddgg}t        | |d"      }|j                  d   d
k7  r4|j                  d   %|j                  d#d$j                  dg      z  %       |j                  d   }|j                  d   }|j                  d   }|j                  d   }t	        |||||      \  }}|j                  ||d&||'       y )(NrD   strT)r$   requiredr   z/etc/sensu/conf.d/checks.json)r$   defaultrE   r   r   )r$   rW   choicesrF   boolFr   r$   r   r=   )r$   elementsr   r   intr   r   r   r&   r'   r   r(   r   r   dict)r   r   r   r   r    r#   r!   )argument_specrequired_togethersupports_check_modezmissing required arguments: %s,r   OK)r   rG   r   rD   rH   )r   r:   r5   joinrS   	exit_json)	arg_specr^   rC   r   rD   rE   rF   rG   rH   s	            rR   mainre   K  s-   D9 3RS%I9V^J_` 6e< FE?	
 V? EB VUO FE?  66*  vuo U C 6e< vv.  FF+!" !'"E?$f-'-uo(.!6*!5//H4 ),78->/35F }}W)fmmI.F.N=)@UUV== D== DMM'"E]]8$F"64ufEGW
$TgV    __main__)r   F)
__future__r   r   r   r$   __metaclass__DOCUMENTATIONEXAMPLESr/   r6   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   rS   re   __name__ rf   rR   <module>rp      sS    A @L\<   4 AIX+W\ zF rf   