
    VhUK                         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lmc mc mc mc mZ d dlmZ  G d d	e      Zd
 Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: zabbix_maintenance
short_description: Create Zabbix maintenance windows
description:
    - This module will let you create Zabbix maintenance windows.
author: "Alexander Bulimov (@abulimov)"
requirements:
    - "python >= 3.9"
options:
    state:
        description:
            - Create or remove a maintenance window. Maintenance window to remove is identified by name.
        default: present
        choices: [ "present", "absent" ]
        type: str
    host_names:
        description:
            - Hosts to manage maintenance window for.
            - B(Required) option when I(state=present) and I(host_groups) is not used.
        aliases: [ "host_name" ]
        type: list
        elements: str
    host_groups:
        description:
            - Host groups to manage maintenance window for.
            - B(Required) option when I(state=present) and I(host_names) is not used.
        aliases: [ "host_group" ]
        type: list
        elements: str
    append:
        description:
            - Whether to append hosts and host groups to the existing maintenance.
        type: bool
        default: false
    minutes:
        description:
            - Length of maintenance window in minutes.
        default: 10
        type: int
    name:
        description:
            - Unique name of maintenance window.
        required: true
        type: str
    desc:
        description:
            - Short description of maintenance window.
        default: Created by Ansible
        type: str
    collect_data:
        description:
            - Type of maintenance. With data collection, or without.
        type: bool
        default: "yes"
    visible_name:
        description:
            - Type of zabbix host name to use for identifying hosts to include in the maintenance.
            - I(visible_name=yes) to search by visible name,  I(visible_name=no) to search by technical name.
        type: bool
        default: "yes"
    active_since:
        description:
            - Time when the maintenance becomes active.
            - The given value will be rounded down to minutes.
            - Uses `datetime.datetime.now(`) if not specified.
        type: "str"
        default: ""
    active_till:
        description:
            - Time when the maintenance stops being active.
            - The given value will be rounded down to minutes.
            - Gets calculated from I(minutes) if not specified.
        type: "str"
        default: ""
    tags:
        description:
            - List of tags to assign to the hosts in maintenance.
            - Requires I(collect_data=yes).
        type: list
        elements: dict
        suboptions:
            tag:
                description:
                    - Name of the tag.
                type: str
                required: true
            value:
                description:
                    - Value of the tag.
                type: str
                default: ""
            operator:
                description:
                    - Condition operator.
                    - Possible values is
                    - 0 - Equals
                    - 2 - Contains
                type: int
                default: 2

extends_documentation_fragment:
- community.zabbix.zabbix


notes:
    - Useful for setting hosts in maintenance mode before big update,
      and removing maintenance window after update.
    - Module creates maintenance window from now() to now() + minutes,
      so if Zabbix server's time and host's time are not synchronized,
      you will get strange results.
aj  
# 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: Create a named maintenance window for host www1 for 90 minutes
  # 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_maintenance:
    name: Update of www1
    host_name: www1.example.com
    state: present
    minutes: 90

- name: Create a named maintenance window for host www1 and host groups Office and Dev
  # 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_maintenance:
    name: Update of www1
    host_name: www1.example.com
    host_groups:
      - Office
      - Dev
    state: present
    tags:
      - tag: ExampleHostsTag
      - tag: ExampleHostsTag2
        value: ExampleTagValue
      - tag: ExampleHostsTag3
        value: ExampleTagValue
        operator: 0

- name: Create a named maintenance window for hosts www1 and db1, without data collection.
  # 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_maintenance:
    name: update
    host_names:
      - www1.example.com
      - db1.example.com
    state: present
    collect_data: false

- name: Remove maintenance window by name
  # 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_maintenance:
    name: Test1
    state: absent

- name: Create maintenance window by date
  # 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_maintenance:
    name: TestDate
    state: present
    host_names:
      - host.example.org
    active_since: "1979-09-19 09:00"
    active_till: "1979-09-19 17:00"
N)AnsibleModule)
ZabbixBase)LooseVersionc                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	MaintenanceModulec	                    ||z   }	|D 
cg c]  }
d|
i c}
|D cg c]  }d|i c}||t        |      t        |	      |dt        |      t        |      dgd}t        | j                        t        d      k  r||d<   ||d<   |d	= |d
= |||d<   | j                  j                  j                  |       yc c}
w c c}w )Ngroupidhostid0timeperiod_type
start_dateperiod)groupshostsnamemaintenance_typeactive_sinceactive_tilldescriptiontimeperiods6.2groupidshostidsr   r   tagsr   NN)strr   _zbx_api_version_zapimaintenancecreate)self	group_idshost_ids
start_timer   r   r   descr   end_timer   r   
parameterss                w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_maintenance.pycreate_maintenancez$MaintenanceModule.create_maintenance   s    &;DE	7+E7?@Vx(@ 0
Ox=#&!*of+ 

 --.e1DD%.Jz"$,Jy!8$7#!%Jv

%%j1+ F@s
   B8B=c	                    ||z   }	||D 
cg c]  }
d|
i c}
|D cg c]  }d|i c}|t        |      t        |	      |dt        |      t        |      dgd}t        | j                        t        d      k  r||d<   ||d<   |d	= |d
= |||d<   | j                  j                  j                  |       yc c}
w c c}w )Nr   r   r   r   )maintenanceidr   r   r   r   r   r   r   r   r   r   r   r   r   r   )r    r   r!   r"   r#   update)r%   maintenance_idr&   r'   r(   r   r   r)   r   r*   r   r   r+   s                r,   update_maintenancez$MaintenanceModule.update_maintenance  s    &+;DE	7+E7?@Vx(@ 0
Ox=#&!*of+ 

 --.e1DD%.Jz"$,Jy!8$7#!%Jv

%%j1) F@s
   B8B=c                    d|idddd}t        | j                        t        d      k  r|d   |d<   |d= | j                  j                  j	                  |      }|D ]  }d|v r|d   D cg c]  }|d   	 c}ng |d	<   t        | j                        t        d      k  rd
|v r|d
   D cg c]  }|d   	 c}ng |d	<   d|v r|d   D cg c]  }|d   	 c}ng |d<   d|d fc S  yc c}w c c}w c c}w )Nr   extend)filterselectHostGroupsselectHosts
selectTagsr   r6   selectGroups
hostgroupsr   r   r   r   r   r   r   r   )r   r!   r"   r#   get)r%   r   r+   maintenancesr#   grouphosts          r,   get_maintenancez!MaintenanceModule.get_maintenance&  sI   tn (#"	

 --.e1DD)34F)GJ~&-.zz--11*=' 	(KHTXcHc+l*C'EEuY'7 'Eik 
#D112\%5HHHPT_H_k(.C+E5+; +Eeg J' CJ[BXW)=&?d8n &?^` 	"k4''	( 'E+E&?s   +C'+C,
C1c                 P    | j                   j                  j                  |g       y)Nr   )r"   r#   delete)r%   r1   s     r,   delete_maintenancez$MaintenanceModule.delete_maintenance>  s     

%%~&67    c                     g }|D ]O  }| j                   j                  j                  dd|id      }|s
dd d|z  fc S |j                  |d   d          Q d|d fS )Nr4   r   outputr5      zGroup id for group %s not foundr   r   )r"   	hostgroupr;   append)r%   host_groupsr&   r=   results        r,   get_group_idszMaintenanceModule.get_group_idsB  s    	  	3EZZ))--& F $ AE IIIVAYy12	3  )T!!rC   c                     g }|D ]O  }| j                   j                  j                  d||id      }|s
dd d|z  fc S |j                  |d   d          Q d|d fS )Nr4   rE   rG   zHost id for host %s not foundr   r   )r"   r>   r;   rI   )r%   
host_nameszabbix_hostr'   r>   rK   s         r,   get_host_idszMaintenanceModule.get_host_idsV  s~     	1DZZ__((& $TF $ ?$ FFFOOF1Ih/0	1  (D  rC   c	                 |   t        |      t        |d         k7  ryt        |      t        |d         k7  ryt        |      |d   k7  ryt        t        |            |d   k7  ryt        t        ||z               |d   k7  ryt        |      |d   k7  ry|'d|v r"t        |d	 
      t        |d   d 
      k7  ryy y y )Nr   Tr   r   r   r   r   r   c                     | d   S Ntag ks    r,   <lambda>z:MaintenanceModule.check_maint_properties.<locals>.<lambda>y  s
    !E( rC   )keyc                     | d   S rS   rU   rV   s    r,   rX   z:MaintenanceModule.check_maint_properties.<locals>.<lambda>y  s    abchai rC   )sortedr    int)	r%   r#   r&   r'   r   r(   r   r)   r   s	            r,   check_maint_propertiesz(MaintenanceModule.check_maint_propertiesj  s    ){:'> ??(vk)&<== K0B$CCs:;~#>>s:&'(K,FFt9M22+ 5d 23vk&>QWi7jj k !6rC   N)
__name__
__module____qualname__r-   r2   r?   rB   rL   rP   r]   rU   rC   r,   r
   r
      s%    660"(!(rC   r
   c                  	   t        j                         } | j                  t        t        dddddg      t        ddd dgd      t        d	dd
      t        ddd dgd      t        ddd      t        dd      t        ddd      t        ddd      t        ddd      t        ddd      t        ddd      t        dddt        t        dd      t        d	d      t        dd                               t	        | d      }t        |      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d    }
|j                  d!   }|j                  d"   }|j                  d#   }|j                  d$   }|
rd%}nd&}||j                  d'(       |rd}nd)}d}|dk(  r`|s|s|j                  d*(       |dk7  rt        j                  j                  |      n-t        j                  j                         j                  d%+      }t        t        j                  |j                                     }|dk7  r9t        t        j                  j                  |      |z
  j!                               nd,t        |      z  }|r0|j#                  |      \  }}}|d%k7  r|j                  d-|z  (       ng }|r1|j%                  ||      \  }}}|d%k7  r|j                  d.|z  (       ng }|j'                  |      \  }}}|d%k7  r|j                  d/|d0|(       |r|r4t)        t+        ||d1   z               }t)        t+        ||d2   z               }|j-                  |||||||	|      rK|j.                  rd}n<|j1                  |d3   ||||||	|      \  }}}|d%k(  rd}n|j                  d4|z  (       |sH|j.                  rd}n9|j3                  |||||||	|      \  }}}|d%k(  rd}n|j                  d5|z  (       |dk(  rx|j'                  |      \  }}}|d%k7  r|j                  d/|d0|(       |rD|j.                  rd}n5|j5                  |d3         \  }}}|d%k(  rd}n|j                  d6|z  (       |j7                  |7       y )8Nr    Fpresentabsent)typerequireddefaultchoiceslist	host_name)rd   re   rf   aliaseselementsr\   
   )rd   re   rf   
host_groupboolT)rd   re   zCreated by Ansible dict   )rd   rf   )rT   operatorvalue)rd   rk   re   options)staterN   minutesrJ   rI   r   r)   collect_datavisible_namer   r   r   )argument_specsupports_check_moderN   rJ   rI   ru   rv   r   r)   rw   rx   r   r   r   r   rG   z@Tags cannot be provided for maintenance without data collection.)msgr>   zRAt least one host_name or host_group must be defined for each created maintenance.)second<   zFailed to get group_ids: %szFailed to get host_ids: %szFailed to check maintenance z existence: r   r   r/   z Failed to update maintenance: %sz Failed to create maintenance: %sz Failed to remove maintenance: %s)changed)zabbix_utilszabbix_common_argument_specr0   rp   r   r
   params	fail_jsondatetimefromisoformatnowreplacer\   timemktime	timetupletotal_secondsrL   rP   r?   rh   setr]   
check_moder2   r-   rB   	exit_json)ry   modulemaintrN   rJ   rI   ru   rv   r   r)   rw   rx   r   r   r   r   rO   r~   r   r(   r   rcr&   errorr'   r#   datas                              r,   mainr   }  s\    <<>My%x02Ve ${meM%%<fu!%~O%?ut,uu6JKvtDvtDuubAeeR@ed35!4r2		
 4 # F
 f%E|,J--.K]]8$FMM'"EmmI&G== D== D==0L==0L==0L--.K== D!cdG	+h  j @Lr?Qh--l;W_WhWhWlWlWnWvWv~Wv  XAS]]_56
^imo^oh''55kBSHWWYZuwz}  F  {G  vG%*%8%8%E"REQw  %BU%J KI$)$6$6z;$O!R5Qw  %AE%I JH#(#8#8#> [%7FJER  T  YZ1H%H!IJ	H{9/E$E FG++KHN^,6dL$$"G(-(@(@#O4i:Wgioquw{)}%RuQw"&(( BU J ) L   $)$<$<x5EvtUY[_%a!T57"G$$>F % H #(#8#8#> [%7FJER  T   $)$<$<0%2!T57"G$$>F % H W%rC   __main__)
__future__r   r   r   rd   __metaclass__DOCUMENTATIONEXAMPLESr   r   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   #ansible.module_utils.compat.versionr   r
   r   r^   rU   rC   r,   <module>r      si    A @obgR   4 U X X X <K
 K\K&\ zF rC   