
    VhS%                     d    d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d Zedk(  r e        yy)	    )absolute_importdivisionprint_functiona  
module: sensu_handler
author: "David Moreau Simard (@dmsimard)"
short_description: Manages Sensu handler configuration
description:
  - Manages Sensu handler configuration.
  - 'For more information, refer to the L(Sensu documentation, https://sensuapp.org/docs/latest/reference/handlers.html).'
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:
  state:
    type: str
    description:
      - Whether the handler should be present or not.
    choices: ['present', 'absent']
    default: present
  name:
    type: str
    description:
      - A unique name for the handler. The name cannot contain special characters or spaces.
    required: true
  type:
    type: str
    description:
      - The handler type.
    choices: ['pipe', 'tcp', 'udp', 'transport', 'set']
  filter:
    type: str
    description:
      - The Sensu event filter (name) to use when filtering events for the handler.
  filters:
    type: list
    elements: str
    description:
      - An array of Sensu event filters (names) to use when filtering events for the handler.
      - Each array item must be a string.
  severities:
    type: list
    elements: str
    description:
      - An array of check result severities the handler will handle.
      - 'NOTE: event resolution bypasses this filtering.'
      - "Example: [ 'warning', 'critical', 'unknown' ]."
  mutator:
    type: str
    description:
      - The Sensu event mutator (name) to use to mutate event data for the handler.
  timeout:
    type: int
    description:
      - The handler execution duration timeout in seconds (hard stop).
      - Only used by pipe and tcp handler types.
    default: 10
  handle_silenced:
    description:
      - If events matching one or more silence entries should be handled.
    type: bool
    default: false
  handle_flapping:
    description:
      - If events in the flapping state should be handled.
    type: bool
    default: false
  command:
    type: str
    description:
      - The handler command to be executed.
      - The event data is passed to the process using STDIN.
      - 'NOTE: the O(command) attribute is only required for Pipe handlers (that is, handlers configured with O(type=pipe)).'
  socket:
    type: dict
    description:
      - The socket definition scope, used to configure the TCP/UDP handler socket.
      - 'NOTE: the O(socket) attribute is only required for TCP/UDP handlers (that is, handlers configured with O(type=tcp)
        or O(type=udp)).'
  pipe:
    type: dict
    description:
      - The pipe definition scope, used to configure the Sensu transport pipe.
      - 'NOTE: the O(pipe) attribute is only required for Transport handlers (that is, handlers configured with O(type=transport)).'
  handlers:
    type: list
    elements: str
    description:
      - An array of Sensu event handlers (names) to use for events using the handler set.
      - 'NOTE: the O(handlers) attribute is only required for handler sets (that is, handlers configured with O(type=set)).'
a  
# Configure a handler that sends event data as STDIN (pipe)
- name: Configure IRC Sensu handler
  community.general.sensu_handler:
    name: "irc_handler"
    type: "pipe"
    command: "/usr/local/bin/notify-irc.sh"
    severities:
      - "ok"
      - "critical"
      - "warning"
      - "unknown"
    timeout: 15
  notify:
    - Restart sensu-client
    - Restart sensu-server

# Delete a handler
- name: Delete IRC Sensu handler
  community.general.sensu_handler:
    name: "irc_handler"
    state: "absent"

# Example of a TCP handler
- name: Configure TCP Sensu handler
  community.general.sensu_handler:
    name: "tcp_handler"
    type: "tcp"
    timeout: 30
    socket:
      host: "10.0.1.99"
      port: 4444
  register: handler
  notify:
    - Restart sensu-client
    - Restart sensu-server

- name: Secure Sensu handler configuration file
  ansible.builtin.file:
    path: "{{ handler['file'] }}"
    owner: "sensu"
    group: "sensu"
    mode: "0600"
a  
config:
  description: Effective handler configuration, when state is present.
  returned: success
  type: dict
  sample: {'name': 'irc', 'type': 'pipe', 'command': '/usr/local/bin/notify-irc.sh'}
file:
  description: Path to the handler configuration file.
  returned: success
  type: str
  sample: "/etc/sensu/conf.d/handlers/irc.json"
name:
  description: Name of the handler.
  returned: success
  type: str
  sample: "irc"
N)AnsibleModulec                     t        dt        t        dddgd      t        dd      t        dg d      t        d	      t        d
d      t        d
d      t        d	      t        dd      t        dd      t        dd      t        d	      t        d	      t        d	      t        d
d            dddggdddggdddggdddggdddggdddggg      } | j                  d   }| j                  d   }dj                  |      }|dk(  rt        j
                  j                  |      rn| j                  r&d j                  |!      }| j                  |d"       n`	 t	        j                  |       d#j                  |!      }| j                  |d"       n$d'j                  |!      }| j                  |&       d|i ii}g d(}|D ]9  }|| j                  v s| j                  |   "| j                  |   |d   |   |<   ; d }		 t        j                  t        |d)            }	|	 |	|k(  r| j                  d*|d   |   ||+       | j                  sot        j
                  j                  t        j
                  j%                  |            s3	 t	        j&                  t        j
                  j%                  |             | j                  r| j                  d-d|d   |   ||.       	 t        |d/      5 }
|
j)                  t        j*                  |d01             | j                  d2d|d   |   ||.       d d d        y # t        $ r9}d$}| j                  |j                  |t        |      %      &       Y d }~d }~ww xY w# t         t"        f$ r Y uw xY w# t        $ rS}| j                  d,j                  t        j
                  j%                  |      t        |            &       Y d }~3d }~ww xY w# 1 sw Y   y xY w# t        t         f$ r5}| j                  d3j                  |t        |            &       Y d }~y d }~ww xY w)4NTstrpresentabsent)typechoicesdefault)r   required)pipetcpudp	transportset)r   r   )r   list)r   elementsint
   )r   r   boolFdict)statenamer   filterfilters
severitiesmutatortimeouthandle_silencedhandle_flappingcommandsocketr   handlersr   r   r   r#   r   r$   r   r   r   r%   )supports_check_modeargument_specrequired_ifr   z#/etc/sensu/conf.d/handlers/{0}.jsonz{path} would have been deleted)path)msgchangedz{path} deleted successfullyz3Exception when trying to delete {path}: {exception})r)   	exception)r*   z{path} already does not exist)r   r   r   r   r   r    r!   r"   r#   r$   r   r%   rz+Handler configuration is already up to date)r*   configfiler   zUnable to create {0}: {1}z-Handler configuration would have been updated)r*   r+   r.   r/   r   w   )indentzHandler configuration updatedzUnable to write file {0}: {1})r   r   paramsformatosr)   exists
check_mode	exit_jsonremoveOSError	fail_jsonr   jsonloadopenIOError
ValueErrordirnamemakedirswritedumps)moduler   r   r)   r*   er.   argsargcurrent_confighandlers              s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sensu_handler.pymainrL      s]    EIx+@)T5405*TUU#fu5%8e$eR0 fe< fe<e$V$6"v6
" i&*Vi[)UXJ'UXJ'[6(+UZL)
%F8 MM'"E== D077=D77>>$  6==4=H  S$ 7EIIdO7>>D>IC$$d$; 288d8CC% 4*%F D  ?&--FMM#$6$B,2MM#,>F:t$S)?
 N4c?3
 !n&>J &z 24 8"" 	 	$ RWW^^BGGOOD4I%J	MKK-.
 L!% &z 24 8""	 	 	$
M$_ 	(MM$**VA67!@%)$*:$6t$<"&"&	  (	( 	(e  EOC$$JJDCFJC % E EE. Z    	M!<!C!CBGGOOTXDYDGF"L M M	M	( 	( W M<CCDDGFL 	M 	MMsz   $:M N 2N& 3P ?APP 	N.NNN#"N#&	P/AO==PP
P P Q +QQ__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr<   r5   ansible.module_utils.basicr   rL   __name__     rK   <module>rW      sR    A @_B+Z
$  	 4fMR zF rV   