
    Vh4                         d dl mZmZmZ eZdZdZdZd dl	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_functionab  
---
module: zabbix_housekeeping

short_description: Update Zabbix housekeeping

description:
   - This module allows you to modify Zabbix housekeeping setting.

author:
    - ONODERA Masaru(@masa-orca)

requirements:
    - "python >= 3.9"

version_added: 1.6.0

options:
    hk_events_mode:
        description:
            - Internal housekeeping for events and alerts will be enabled if C(true).
        required: false
        type: bool
    hk_events_trigger:
        description:
            - Storage period of trigger data (e.g. 365d).
        required: false
        type: str
    hk_events_service:
        description:
            - Storage period of service data (e.g. 365d).
        required: false
        type: str
    hk_events_internal:
        description:
            - Storage period of internal data (e.g. 365d).
        required: false
        type: str
    hk_events_discovery:
        description:
            - Storage period of network discovery (e.g. 365d).
        required: false
        type: str
    hk_events_autoreg:
        description:
            - Storage period of autoregistration data (e.g. 365d).
        required: false
        type: str
    hk_services_mode:
        description:
            - Internal housekeeping for services will be enabled if C(true).
        required: false
        type: bool
    hk_services:
        description:
            - Storage period of services data (e.g. 365d).
        required: false
        type: str
    hk_audit_mode:
        description:
            - Internal housekeeping for audit will be enabled if C(true).
        required: false
        type: bool
    hk_audit:
        description:
            - Storage period of audit data (e.g. 365d).
        required: false
        type: str
    hk_sessions_mode:
        description:
            - Internal housekeeping for sessions will be enabled if C(true).
        required: false
        type: bool
    hk_sessions:
        description:
            - Storage period of sessions data (e.g. 365d).
        required: false
        type: str
    hk_history_mode:
        description:
            - Internal housekeeping for history will be enabled if C(true).
        required: false
        type: bool
    hk_history_global:
        description:
            - Overriding history period of each items will be enabled if C(true).
        required: false
        type: bool
    hk_history:
        description:
            - Storage priod of history data (e.g. 365d).
        required: false
        type: str
    hk_trends_mode:
        description:
            - Internal housekeeping for trends will be enabled if C(true).
        required: false
        type: bool
    hk_trends_global:
        description:
            - Overriding trend period of each items will be enabled if C(true).
        required: false
        type: bool
    hk_trends:
        description:
            - Storage priod of trends data (e.g. 365d).
        required: false
        type: str
    compression_status:
        description:
            - TimescaleDB compression for history and trends will be enabled if C(true).
        required: false
        type: bool
    compress_older:
        description:
            - Compress history and trends records older than this period if I(compression_status=true).
        required: false
        type: str

extends_documentation_fragment:
    - community.zabbix.zabbix
aP  
# 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

- name: Update housekeeping all parameter
  # 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_housekeeping:
    login_user: Admin
    login_password: secret
    hk_events_mode: yes
    hk_events_trigger: 365d
    hk_events_service: 365d
    hk_events_internal: 365d
    hk_events_discovery: 365d
    hk_events_autoreg: 365d
    hk_services_mode: yes
    hk_services: 365d
    hk_audit_mode: yes
    hk_audit: 365d
    hk_sessions_mode: yes
    hk_sessions: 365d
    hk_history_mode: yes
    hk_history_global: yes
    hk_history: 365d
    hk_trends_mode: yes
    hk_trends_global: yes
    hk_trends: 365d
    compression_status: off
    compress_older: 7d
z
msg:
    description: The result of the operation
    returned: success
    type: str
    sample: "Successfully update housekeeping setting"
N)AnsibleModule)
ZabbixBasec                       e Zd Zd Zd Zd Zy)Housekeepingc                     	 | j                   j                  j                  ddi      S # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)Noutputextendz&Failed to get housekeeping setting: %smsg)_zapihousekeepingget	Exception_module	fail_json)selfes     x/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_housekeeping.pyget_housekeepingzHousekeeping.get_housekeeping   sY    	U::**..(/CDD 	ULL""'ORS'S"TT	Us   &) 	AAAc                 t    t        j                  d|      }|s | j                  j                  d|z         y y )Nz^[0-9]+[smhdw]$z1Invalid value for %s! Please set value like 365d.r   )rematchr   r   )r   key_namevaluematch_results       r   check_time_parameterz!Housekeeping.check_time_parameter   s7    xx 159LL""'Z]e'e"f     c                    	 i }t        |t              rt        t        |            |d<   |r| j	                  d|       ||d<   |r| j	                  d|       ||d<   |r| j	                  d|       ||d<   |r| j	                  d|       ||d<   |r| j	                  d|       ||d<   t        |t              rt        t        |            |d<   |	r| j	                  d|	       |	|d<   t        |
t              rt        t        |
            |d	<   |r| j	                  d
|       ||d
<   t        |t              rt        t        |            |d<   |r| j	                  d|       ||d<   t        |t              rt        t        |            |d<   t        |t              rt        t        |            |d<   |r| j	                  d|       ||d<   t        |t              rt        t        |            |d<   t        |t              rt        t        |            |d<   |r| j	                  d|       ||d<   t        |t              rt        t        |            |d<   |r| j	                  d|       ||d<   |j                         }|j                  |       ||k7  ru| j                  j                  r| j                  j                  d       | j                  j                  j                  |       | j                  j                  dd       y | j                  j                  dd       y # t        $ r)}| j                  j                  d|z         Y d }~y d }~ww xY w)Nhk_events_modehk_events_triggerhk_events_servicehk_events_internalhk_events_discoveryhk_events_autoreghk_services_modehk_serviceshk_audit_modehk_audithk_sessions_modehk_sessionshk_history_modehk_history_global
hk_historyhk_trends_modehk_trends_global	hk_trendscompression_statuscompress_olderT)changedz(Successfully update housekeeping setting)r6   resultFz*Housekeeping setting is already up to datez4Failed to update housekeeping setting, Exception: %sr   )
isinstanceboolstrintr   copyupdater   
check_mode	exit_jsonr   r   r   r   )r   current_housekeepingr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   paramsfuture_housekeepingr   s                            r   update_housekeepingz Housekeeping.update_housekeeping   s?   .V	cF.$/+.s>/B+C'( ))*=?PQ.?*+ ))*=?PQ.?*+!))*>@RS/A+,"))*?ATU0C,- ))*=?PQ.?*+*D1-05E1F-G)*))-E(3}%-.*-c-.@*A'))*h?%-z"*D1-05E1F-G)*))-E(3}%/40,/O0D,E()+T2.1#6G2H.I*+)),
C'1|$.$/+.s>/B+C'(*D1-05E1F-G)*))+yA&/{#,d3/237I3J/K+,))*:NK+9'("6";";"=&&v."&::<<**LL**4*8

''..v6&&t<f&g&&u=i&j 	cLL""']`a'a"bb	cs   LL/ L/ /	M!8MM!N)__name__
__module____qualname__r   r   rC    r    r   r	   r	      s    Ugmcr    r	   c                  V   t        j                         } | j                  t        di 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t        d      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t        d      dt        d      dt        d             t	        | d      }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d	   }|j
                  d
   }|j
                  d   }	|j
                  d   }
|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }|j
                  d   }t        |      }|j                         }|j                  |||||||||	|
|||||||||||       y )Nr"   r9   )typer#   r:   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   T)argument_specsupports_check_moderG   )	zabbix_utilszabbix_common_argument_specr=   dictr   rA   r	   r   rC   )rJ   moduler"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r   r@   s                           r   mainrP   E  s    <<>M (E* E*  U+	
 !e, E* 6* e$ ' 5! 6* e$ &) F+ U#  (!" 6*#$ E"%&  V,'( ') , # F
 ]]#34N&9:&9:';< --(=>&9:}}%78--.KMM/2M}}Z(H}}%78--.Kmm$56O&9:|,J]]#34N}}%78k*I';<]]#34N'L'88:$$+r    __main__)
__future__r   r   r   rI   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   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helpersrL   r	   rP   rD   rG   r    r   <module>r_      sk    A @yv.`
 
 4 U X X X|c: |c~K\ zF r    