
    Vh4D                         d dl mZmZmZ eZdZdZd dlm	Z	 d dl
mZ d dlmc mc mc mc mZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: zabbix_trigger
short_description: Create/delete Zabbix triggers
description:
   - Create triggers if they do not exist.
   - Delete existing triggers if they exist.
author:
    - "Andrew Lathrop (@aplathrop)"
requirements:
    - "python >= 2.6"

options:
    state:
        description:
            - Create or delete trigger.
        required: false
        type: str
        default: "present"
        choices: [ "present", "absent" ]
    name:
        description:
            - Name of trigger to create or delete.
            - Overrides "description" in API docs.
            - Cannot be changed. If a trigger's name needs to be changed, it needs to deleted and recreated
        required: true
        type: str
    host_name:
        description:
            - Name of host to add trigger to.
            - Required when I(template_name) is not used.
            - Mutually exclusive with I(template_name).
        required: false
        type: str
    template_name:
        description:
            - Name of template to add trigger to.
            - Required when I(host_name) is not used.
            - Mutually exclusive with I(host_name).
        required: false
        type: str
    desc:
        description:
            - Additional description of the trigger.
            - Overrides "comments" in API docs.
        required: false
        type: str
        aliases: [ "description" ]
    dependencies:
        description:
            - list of triggers that this trigger is dependent on
        required: false
        type: list
        elements: dict
        suboptions:
                name:
                    description:
                        - Name of dependent trigger.
                    required: true
                    type: str
                host_name:
                    description:
                        - Name of host containing dependent trigger.
                        - Required when I(template_name) is not used.
                        - Mutually exclusive with I(template_name).
                    required: false
                    type: str
                template_name:
                    description:
                        - Name of template containing dependent trigger.
                        - Required when I(host_name) is not used.
                        - Mutually exclusive with I(host_name).
                    required: false
                    type: str
    params:
        description:
            - Parameters to create/update trigger with.
            - Required if state is "present".
            - Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/trigger/object
            - Additionally supported parameters are below.
        required: false
        type: dict
        suboptions:
            severity:
                description:
                    - Severity of the trigger.
                    - Alias for "priority" in API docs.
                required: false
                type: str
                aliases: [ "priority" ]
                choices:
                    - not_classified
                    - information
                    - warning
                    - average
                    - high
                    - disaster
            status:
                description:
                    - Status of the trigger.
                required: false
                type: str
                choices: [ "enabled", "disabled" ]
            enabled:
                description:
                    - Status of the trigger.
                    - Overrides "status" in API docs.
                required: false
                type: bool
            new_name:
                description:
                    - New name for trigger
                required: false
                type: str
            generate_multiple_events:
                description:
                    - Whether the trigger can generate multiple problem events.
                    - Alias for "type" in API docs.
                required: false
                type: bool
            recovery_mode:
                description:
                    - OK event generation mode.
                    - Overrides "recovery_mode" in API docs.
                required: false
                type: str
                choices:
                    - expression
                    - recovery_expression
                    - none
            correlation_mode:
                description:
                    - OK event closes.
                    - Overrides "correlation_mode" in API docs.
                required: false
                type: str
                choices: [ "all", "tag" ]
            manual_close:
                description:
                    - Allow manual close.
                    - Overrides "manual_close" in API docs.
                required: false
                type: bool

extends_documentation_fragment:
- community.zabbix.zabbix
a  

# If you want to use Username and Password to be authenticated by Zabbix Server
- name: Set credentials to access Zabbix Server API
  ansible.builtin.set_fact:
    ansible_user: Admin
    ansible_httpapi_pass: zabbix

# If you want to use API token to be authenticated by Zabbix Server
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens
- name: Set API token
  ansible.builtin.set_fact:
    ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895

# Create ping trigger on example_host
- name: create ping trigger
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_trigger:
    name: agent_ping
    host_name: example_host
    params:
        severity: high
        expression: 'nodata(/example_host/agent.ping,1m)=1'
        manual_close: True
        enabled: True
    state: present

# Create ping trigger on example_template
- name: create ping trigger
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_trigger:
    name: agent_ping
    host_name: example_template
    params:
        severity: high
        expression: 'nodata(/example_template/agent.ping,1m)=1'
        manual_close: True
        enabled: True
    state: present

# Add tags to the existing Zabbix trigger
- name: update ping trigger
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_trigger:
    name: agent_ping
    host_name: example_template
    params:
        severity: high
        expression: 'nodata(/example_template/agent.ping,1m)=1'
        manual_close: True
        enabled: True
        tags:
          - tag: class
            value: application
    state: present

# delete Zabbix trigger
- name: delete ping trigger
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: 'zabbixeu'  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_trigger:
    name: agent_ping
    host_name: example_template
    state: absent

- name: Rename Zabbix trigger
  # set task level variables as we change ansible_connection plugin here
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: false
    ansible_zabbix_url_path: "zabbixeu"  # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu
    ansible_host: zabbix-example-fqdn.org
  community.zabbix.zabbix_trigger:
    name: agent_ping
    template_name: example_template
    params:
      new_name: new_agent_ping
    state: present
)AnsibleModule)
ZabbixBaseNc                   P    e Zd ZdddddddZddddZd	 ZddZd Zd Zd Z	d Z
y
)Triggerr                  )not_classifiedinformationwarningaveragehighdisaster)
expressionrecovery_expressionnonec                     ||}n|}g }	 | j                   j                  j                  d||di      }|S # t        $ r*}| j                  j                  d|z         Y d }~|S d }~ww xY w)Nfilter)descriptionhostFailed to get trigger: %smsg_zapitriggerget	Exception_module	fail_json)selftrigger_name	host_nametemplate_namer   triggerses          s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_trigger.pyget_triggerszTrigger.get_triggers#  s     D D	Hzz))--x_c9d.efH   	HLL""'BQ'F"GG	Hs   *7 	A* A%%A*Nc                 B   ||d<   |||d<   d|v r|d   |d<   |j                  d       d|v r| j                  |d      }||d<   d|v r!|d   rd|d<   nd|d<   |j                  d       d|v r:|d   }|dk(  rd|d<   n*|dk(  rd	|d<   n| j                  j                  d
|z         d|v r|d   }|rd	|d<   nd|d<   d|v r| j                  |d      }||d<   d|v r:|d   }	|	dk(  rd|d<   n*|	dk(  rd	|d<   n| j                  j                  d|	z         d|v r|d   }
|
rd	|d<   nd|d<   |qg |d<   |D ]f  }d }d }d|v r|}n#d|v r|}n| j                  j                  d       | j                  |d   ||      }|D ]  }|d   j                  d|d   i        h y y )Nr   commentsseveritypriorityenabledstatusdisabledr   r
   z.Status must be 'enabled' or 'disabled', got %sr   generate_multiple_eventstyperecovery_modecorrelation_modealltagz+correlation_mode must be all or tag, got %smanual_closedependenciesr(   r)   zFEach dependency must contain either the host_name or the template_namename	triggerid)popPRIORITY_TYPESr$   r%   RECOVERY_MODESr-   append)r&   r=   paramsdescr<   priority_idr3   multiple_event_typerecovery_mode_idr8   r;   
dependencyr(   r)   r*   r!   s                   r,   sanitize_paramszTrigger.sanitize_params/  s_    $}!%F:!'
!3F:JJz"--fZ.@AK!,F:i #,x #-x JJy!vH%F"#$x :%#$x &&+[^d+d&e%/"()C"D"!"v!"vf$#226/3JK&6F?#'%&895(-.)*!U*-.)*&&+X[k+k&lV#!.1L)*~&)*~&#%'F>"* W
 	 $*, *I$
2$.MLL**/w*x,,Z-?MZ' WG>*11;@T2UVWW $    c                 "   | j                   j                  r| j                   j                  d       	 | j                  j                  j                  |      }|S # t        $ r*}| j                   j                  d|z         Y d }~S d }~ww xY w)NTchangedzFailed to create trigger: %sr   )r$   
check_mode	exit_jsonr    r!   creater#   r%   r&   rC   resultsr+   s       r,   add_triggerzTrigger.add_triggerm      <<""LL""4"0	Kjj((//7G   	KLL""'E'I"JJ	K   %A 	B$B		Bc                 "   | j                   j                  r| j                   j                  d       	 | j                  j                  j                  |      }|S # t        $ r*}| j                   j                  d|z         Y d }~S d }~ww xY w)NTrL   zFailed to update trigger: %sr   )r$   rN   rO   r    r!   updater#   r%   rQ   s       r,   update_triggerzTrigger.update_triggerv  rT   rU   c                     	 | j                   j                  j                  dd|d   z  i      d   }||k7  S # t        $ r-}| j                  j                  d|z         Y d }~|k7  S d }~ww xY w)N
triggeridsz%sr>   r   r   r   r   )r&   old_triggernew_triggerr+   s       r,   check_trigger_changedzTrigger.check_trigger_changed  s    	H**,,00,{S^G_@_1`abcdK k))  	HLL""'BQ'F"GGk))	Hs   07 	A- A((A-c                 "   | j                   j                  r| j                   j                  d       	 | j                  j                  j                  |      }|S # t        $ r*}| j                   j                  d|z         Y d }~S d }~ww xY w)NTrL   zFailed to delete trigger: %sr   )r$   rN   rO   r    r!   deleter#   r%   )r&   
trigger_idrR   r+   s       r,   delete_triggerzTrigger.delete_trigger  s    <<""LL""4"0	Kjj((//
;G   	KLL""'E'I"JJ	KrU   )NN)__name__
__module____qualname__r@   rA   r-   rI   rS   rX   r]   ra    rJ   r,   r	   r	     sM    ()%&!"!""#%N %&-.!N
<W|*rJ   r	   c                     t        j                         } | j                  t        t        dd      t        dd      t        dd      t        dd      t        dddg      t        ddd	      t        dd
d
dg                   t	        | ddggddggdd
dgggd      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }t        |      }	|dk(  rw|	j                  |||      }
t        |
      dk(  r|j                  dd       y g }|
D ]  }|j                  |d           |	j                  |      }|j                  d|       y |d
k(  rB|	j                  ||||       |	j                  |||      }
d|v rA|	j                  |d   ||      }t        |      dkD  r|j                  dd|d   d   gig       t        |
      dk(  r=d|v r|j                  d|z         |	j                  |      }|j                  d|       y g }d}|
D ]n  }|d   |d<   |j                  d       d|v r|d   |d<   |j                  d       |j                  |	j!                  |             |	j#                  |      }|smd}p |j                  ||       y y )NstrT)r6   requiredFdictr   )r6   rh   aliaseslist)r6   elementsrh   presentabsent)r6   defaultchoices)r=   r(   r)   rC   rD   r<   stater(   r)   rq   rC   )argument_specrequired_one_ofmutually_exclusiverequired_ifsupports_check_moder=   rD   r<   r   zNo trigger to delete.)rM   resultr>   new_namerZ   z'Cannot rename trigger:  %s is not found)zabbix_utilszabbix_common_argument_specrW   ri   r   rC   r	   r-   lenrO   rB   ra   rI   r%   rS   r?   rX   r]   )rr   moduler=   r(   r)   rC   rD   r<   rq   r!   r*   
delete_idstrR   new_name_triggerrM   changed_triggers                    r,   mainr     s#    <<>Mut,EE26%0uu}oFvGy9h:OP  #/*
 /*
 i(,
 !F == Dk*IMM/2M]]8$F== D==0LMM'"EfoG''iGx=AU3JKJ 2!!!K.12,,Z8GT':	)	fdLA''iG&33F:4F	S`a#$q(  HXYZH[\gHhGi8j7k lx=AV#  !JT!QR))&1GT':GG 	#&'n{#

=)',2:,>F=)JJz*w55f=>")"?"?"B""G	# WW=3 
rJ   __main__)
__future__r   r   r   r6   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   >ansible_collections.community.zabbix.plugins.module_utils.baser   Aansible_collections.community.zabbix.plugins.module_utils.helpers	communityzabbixpluginsmodule_utilshelpersry   r	   r   rb   re   rJ   r,   <module>r      s`    A @Rhpd 5 U X X Xwj wtG>T zF rJ   