
    Vh;[                         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_functionaQ  
---
module: zabbix_item
short_description: Create/delete Zabbix items
description:
   - Create items if they do not exist.
   - Delete existing items if they exist.
author:
    - "Andrew Lathrop (@aplathrop)"
requirements:
    - "python >= 2.6"

options:
    state:
        description:
            - Create or delete item.
        required: false
        type: str
        default: "present"
        choices: [ "present", "absent" ]
    name:
        description:
            - Name of item to create or delete.
        required: true
        type: str
    host_name:
        description:
            - Name of host to add item 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 item to.
            - Required when I(host_name) is not used.
            - Mutually exclusive with I(host_name).
        required: false
        type: str
    params:
        description:
            - Parameters to create/update item with.
            - Required if state is "present".
            - Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/item/object
            - Additionally supported parameters are below
        required: false
        type: dict
        suboptions:
            key:
                description:
                    - Item key.
                    - Alias for "key_" in API docs
                required: false
                type: str
            interval:
                description:
                    - Update interval of the item.
                    - Alias for "delay" in API docs
                required: false
                type: str
            status:
                description:
                    - Status of the item.
                required: false
                type: str
                choices: [ "enabled", "disabled" ]
            enabled:
                description:
                    - Status of the item.
                    - Overrides "status" in API docs
                required: false
                type: bool
            type:
                description:
                    - Type of the item.
                    - Required if state is "present".
                required: false
                type: str
                choices:
                    - zabbix_agent
                    - zabbix_trapper
                    - simple_check
                    - zabbix_internal
                    - zabbix_agent_active
                    - web_item
                    - external_check
                    - database_monitor
                    - ipmi_agent
                    - ssh_agent
                    - telnet_agent
                    - calculated
                    - jmx_agent
                    - snmp_trap
                    - dependent_item
                    - http_agent
                    - snmp_agent
                    - script
            value_type:
                description:
                    - Type of information of the item.
                    - Required if state is "present".
                required: false
                type: str
                choices:
                    - numeric_float
                    - character
                    - log
                    - numeric_unsigned
                    - text
            new_name:
                description:
                    - New name for item
                required: false
                type: str
            master_item:
                description:
                    - item that is the master of the current one
                    - Overrides "master_itemid" in API docs
                required: false
                type: dict
                suboptions:
                    item_name:
                        description:
                          - name of the master item
                        required: true
                        type: str
                    host_name:
                        description:
                          - name of the host the master item belongs to
                          - Required when I(template_name) is not used.
                          - Mutually exclusive with I(template_name).
                        required: false
                    template_name:
                        description:
                          - name of the template the master item belongs to
                          - Required when I(host_name) is not used.
                          - Mutually exclusive with I(host_name).
            preprocessing:
                description:
                    - Item preprocessing options.
                    - Parameters as defined at https://www.zabbix.com/documentation/current/en/manual/api/reference/item/object#item-preprocessing
                    - Additionally supported parameters are below
                required: false
                type: list
                elements: dict
                suboptions:
                    type:
                        description:
                            - The preprocessing option type.
                        required: true
                        type: str
                        choices:
                            - custom_multiplier
                            - right_trim
                            - left_trim
                            - trim
                            - regular_expressions
                            - regex
                            - boolean_to_decimal
                            - octal_to_decimal
                            - hexadecimal_to_decimal
                            - simple_change
                            - change_per_second
                            - xml_xpath
                            - jsonpath
                            - in_range
                            - matches_regular_expression
                            - matches_regex
                            - does_not_match_regular_expression
                            - not_match_regex
                            - check_for_error_in_json
                            - check_for_json_error
                            - check_for_error_in_xml
                            - check_for_xml_error
                            - check_for_error_using_regular_expression
                            - check_for_error_regex
                            - discard_unchanged
                            - discard_unchanged_with_heartbeat
                            - javascript
                            - prometheus_pattern
                            - prometheus_to_json
                            - csv_to_json
                            - replace
                            - check_unsupported
                            - xml_to_json
                            - snmp_walk_value
                            - snmp_walk_to_json
                    error_handler:
                        description:
                            - Action type used in case of preprocessing step failure.
                        required: false
                        type: str
                        choices:
                            - zabbix_server
                            - discard
                            - set_custom_value
                            - set_custom_error_message

extends_documentation_fragment:
- community.zabbix.zabbix
av  

# 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 item on example_host
- name: create ping item
  # 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_item:
    name: agent_ping
    host_name: example_host
    params:
        type: zabbix_agent
        key: agent.ping
        value_type: numeric_unsigned
        interval: 1m
    state: present

# Create ping item on example_template
- name: create ping item
  # 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_item:
    name: agent_ping
    template_name: example_template
    params:
        type: zabbix_agent
        key: agent.ping
        value_type: numeric_unsigned
        interval: 1m
    state: present

- name: Add tags to the existing Zabbix item
  # 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_item:
    name: agent_ping
    template_name: example_template
    params:
        type: zabbix_agent
        key: agent.ping
        value_type: numeric_unsigned
        interval: 1m
        tags:
          - tag: class
            value: application
    state: present

- name: create a dependent item
  # 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_item:
    name: depend_item
    host_name: example_host
    params:
        type: dependent_item
        key: vfs.fs.pused
        value_type: numeric_float
        units: '%'
        master_item:
          item_name: example_item
          host_name: example_host
        preprocessing:
          - type: jsonpath
            params: '$[?(@.fstype == "ext4")]'
            error_handler: zabbix_server
          - type: jsonpath
            params: "$[*].['bytes', 'inodes'].pused.max()"
            error_handler: zabbix_server
    state: present

- name: Delete Zabbix item
  # 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_item:
    name: agent_ping
    template_name: example_template
    state: absent

- name: Rename Zabbix item
  # 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_item:
    name: agent_ping
    template_name: example_template
    params:
      new_name: new_agent_ping
    state: present
)AnsibleModule)
ZabbixBaseNc                      e Zd Zi ddddddddd	d
dddddddddddddddddddddd d!d"d#d$iZdd%ddd&d'Zi d(d%d)dd*dd+d&d,dd-dd.d/d0d
d1d2d3dd4dd5dd6dd7dd8dd9dd:di d;dd<dd=dd>dd?dd@ddAddBd dCd"dDd$dEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTiZdd%dddUZdV ZdW ZdX Z	dY Z
dZ Zd[ Zd\ Zy])^Itemzabbix_agentr   zabbix_trapper   simple_check   zabbix_internal   zabbix_agent_active   web_item	   external_check
   database_monitor   
ipmi_agent   	ssh_agent   telnet_agent   
calculated   	jmx_agent   	snmp_trap   dependent_item   
http_agent   
snmp_agent   script         )numeric_float	characterlognumeric_unsignedtextcustom_multiplier
right_trim	left_trimtrimregular_expressionsregexboolean_to_decimal   octal_to_decimalhexadecimal_to_decimal   simple_changechange_per_second	xml_xpathjsonpathin_rangematches_regular_expressionmatches_regex!does_not_match_regular_expressionnot_match_regexcheck_for_error_in_jsoncheck_for_json_errorcheck_for_error_in_xmlcheck_for_xml_error(check_for_error_using_regular_expressioncheck_for_error_regexdiscard_unchanged discard_unchanged_with_heartbeat
javascriptprometheus_pattern   prometheus_to_json   csv_to_json   replace   check_unsupported   xml_to_json   snmp_walk_value   snmp_walk_to_json   )zabbix_serverdiscardset_custom_valueset_custom_error_messagec                    |*	 | j                   j                  j                  dd|ii      S 	 | j                   j                  j                  dd|ii      S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w# t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)NfilterhostzFailed to get host: %smsgzFailed to get template: %s)_zapirg   get	Exception_module	fail_jsontemplate)self	host_nametemplate_namees       p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_item.pyget_hosts_templateszItem.get_hosts_templates  s     Izz**Hvy6I+JKKMzz**..6=:Q/RSS	  I&&+Ca+G&HHI
  M&&+G!+K&LLMs.   (A (B 	B	 BB		B>B99B>c                     ||}n|}g }	 | j                   j                  j                  d||di      }|S # t        $ r*}| j                  j                  d|z         Y d }~|S d }~ww xY w)Nrf   )namerg   Failed to get item: %srh   rj   itemrk   rl   rm   rn   )rp   	item_namerq   rr   rg   itemsrs   s          rt   	get_itemszItem.get_items  s}     D D	EJJOO''It3T(UVE   	ELL""'?!'C"DD	Es   *7 	A* A%%A*c                 d   ||d<   d|v r|d   |d<   |j                  d       d|v r| j                  |d      }||d<   d|v r| j                  |d      }||d<   d|v r|d   |d<   |j                  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 rd|d   vrd |d   d<   d|d   vrd |d   d<   | j                  |d   d   |d   d   |d   d         }t        |      dk(  r%| j                  j	                  d|d   d   z         |d   d   |d<   |j                  d       d|v r>|d   D ]5  }| j                  |d      }||d<   d|v s| j                  |d      }	|	|d<   7 y y )Nrw   keykey_type
value_typeintervaldelayenabledstatusdisabledr   r-   z.Status must be 'enabled' or 'disabled', got %srh   master_itemrq   rr   r{   z,No items with the name %s exist to depend onitemidmaster_itemidpreprocessingerror_handler)	pop
ITEM_TYPESVALUE_TYPESrm   rn   r}   lenPREPROCESSING_TYPESPREPROCESSING_ERROR_HANDLERS)
rp   rw   paramsitem_type_intvalue_type_intr   master_itemsparampreprocess_type_interror_handler_ints
             rt   sanitize_paramszItem.sanitize_params  s]   vF?#E]F6NJJuV OOF6N;M*F6N6!!--f\.BCN#1F< $Z0F7OJJz"i #,x #-x JJy!vH%F"#$x :%#$x &&+[^d+d&eF"&"7759}%k2f]&;;9=}%o6>>&*?*LfUbNcdoNprx  zG  sH  IX  sY  ZL< A%&&+Y\bcp\qr}\~+~&&21oh&?F?#JJ}%f$0 ?&*&>&>uV}&M# 3f"e+(,(I(I%P_J`(a%->E/*? %    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 item: %srh   )rm   
check_mode	exit_jsonrj   rz   createrl   rn   rp   r   resultsrs   s       rt   add_itemzItem.add_item      <<""LL""4"0	Hjjoo,,V4G   	HLL""'BQ'F"GG	H   %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)NTr   zFailed to update item: %srh   )rm   r   r   rj   rz   updaterl   rn   r   s       rt   update_itemzItem.update_item  r   r   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itemidsz%sr   r   rx   rh   ry   )rp   old_itemnew_itemrs   s       rt   check_item_changedzItem.check_item_changed  s    	Ezz**Ithx>P7P+QRSTUH 8##  	ELL""'?!'C"DD8##	E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)NTr   zFailed to delete item: %srh   )rm   r   r   rj   rz   deleterl   rn   )rp   item_idr   rs   s       rt   delete_itemzItem.delete_item
  s    <<""LL""4"0	Hjjoo,,W5G   	HLL""'BQ'F"GG	Hr   N)__name__
__module____qualname__r   r   r   r   ru   r}   r   r   r   r   r    r   rt   r	   r	   h  s    .!  "A  !  $Q  (	 
 a  #B  %b    r  !"    r  r  #B     ! " B# J& %& !'(	K"4. "4'"4&"4 "1"4 1!	"4
 #A"4 0"4 .q"4 4Q"4 +A"4 /"4 '"4 &r"4 &r"4 8"4 +B"4  ?!"4" -b#"4$ 5b%"4& 22'"4( 4R)"4* 1"+"4, Fr-"4. 3B/"40 /1"42 >r3"44 (5"46 07"48 09"4: )";"4< %b="4> /?"4@ )"A"4B -bC"4D /E"4H 67/089@A$C 

M
,?\$r   r	   c                     t        j                         } | j                  t        t        dd      t        dd      t        dd      t        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   }t        |      }|dk(  rw|j                  |||      }t        |      dk(  r|j                  dd       y g }	|D ]  }
|	j                  |
d           |j                  |	      }|j                  d|       y |dk(  r|j                  ||       |j                  |||      }d|v rA|j                  |d   ||      }t        |      dkD  r|j                  dd|d   d   gig       g }t        |      dk(  rd|v r|j                  d|z         |j                  ||      }|D ]N  }d|v r	|d   |d<   nd|v r	|d   |d<   n|j                  d       |j                  |j                  |             P |j                  d|       y d}|D ]]  }
|
d   |d<   d|v r|d   |d<   |j!                  d       |j                  |j#                  |             |j%                  |
      }|s\d}_ |j                  ||       y y )NstrT)r   requiredFdictpresentabsent)r   defaultchoices)rw   rq   rr   r   staterq   rr   r   r   )argument_specrequired_one_ofmutually_exclusiverequired_ifsupports_check_moderw   r   zNo item to delete.)r   resultr   new_namer   z$Cannot rename item:  %s is not foundhostid
templateidzhost/template did not return idrh   )zabbix_utilszabbix_common_argument_specr   r   r   r   r	   r}   r   r   appendr   r   rn   ru   r   r   r   r   )r   modulerw   rq   rr   r   r   rz   r|   
delete_idsir   new_name_itemhosts_templateshost_templater   changed_items                    rt   mainr     s9    <<>Mut,EE26%0y9h:OP  #/*
 /*
 i(,
 !F == Dk*IMM/2M]]8$FMM'"E<DtY>u:?U3GHJ /!!!H+./&&z2GT':	)	T6*tY> NN6*+=y-XM=!A%  ]STEUV^E_D`8a7b cu:?V#  !G$!NO"66y-PO!0 6},'4X'>F8$!]2'4\'BF8$$$)J$Kt}}V456 T':G ##$X;x '%+J%7F6NJJz*t//78#66q9"G# WW=A 
r   __main__)
__future__r   r   r   r   __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helpersr   r	   r   r   r   r   rt   <module>r      s`    A @HTL\ 5 U X X Xi: iXJ>Z zF r   