
    Vhy]                         d dl mZmZmZ eZd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_functionak  
---
module: zabbix_service
short_description: Create/update/delete Zabbix service
description:
    - Create/update/delete Zabbix service.
author:
    - "Emmanuel Riviere (@emriver)"
    - "Evgeny Yurchenko (@BGmot)"
requirements:
    - "python >= 3.9"
options:
    name:
        description:
            - Name of Zabbix service
        required: true
        type: str
    algorithm:
        description:
            - Status calculation rule. Only applicable if child services exists.
            - " - C(status_to_ok), set status to OK with"
            - " - C(most_crit_if_all_children), most critical if all children have problems"
            - " - C(most_crit_of_child_serv), most critical of child services with"
        required: false
        type: str
        choices: ["status_to_ok", "most_crit_if_all_children", "most_crit_of_child_serv"]
        default: status_to_ok
    state:
        description:
            - "State: present - create/update service; absent - delete service."
        required: false
        choices: [present, absent]
        default: "present"
        type: str
    sortorder:
        description:
            - Position of the service used for sorting.
        required: true
        type: str
    weight:
        description:
            - Service weight.
        required: false
        default: "0"
        type: str
    description:
        description:
            - Description of the service.
        required: false
        type: str
    tags:
        description:
            - Service tags to be created for the service.
        required: false
        type: list
        elements: dict
        suboptions:
            tag:
                description:
                    - Service tag name.
                required: true
                type: str
            value:
                description:
                    - Service tag value.
                required: false
                type: str
    problem_tags:
        description:
            - Problem tags to be created for the service.
        required: false
        type: list
        elements: dict
        suboptions:
            tag:
                description:
                    - Problem tag name.
                required: true
                type: str
            operator:
                description:
                    - Mapping condition operator.
                    - C(equals)
                    - C(like)
                choices: ["equals", "like"]
                required: false
                default: "equals"
                type: str
            value:
                description:
                    - Problem tag value.
                required: false
                default: ""
                type: str
    parents:
        description:
            - Parent services to be linked to the service.
        required: false
        type: list
        elements: str
    children:
        description:
            - Child services to be linked to the service.
        required: false
        type: list
        elements: str
    propagation_rule:
        description:
            - Status propagation value. Must be set together with propagation_rule.
            - C(as_is) propagate service status as is - without any changes
            - C(increase) increase the propagated status by a given propagation_value (by 1 to 5 severities)
            - C(decrease) decrease the propagated status by a given propagation_value (by 1 to 5 severities)
            - C(ignore) ignore this service - the status is not propagated to the parent service at all
            - C(fixed) set fixed service status using a given propagation_value
            - Required with C(propagation_value)
        required: false
        type: str
        default: as_is
    propagation_value:
        description:
            - Status propagation value. Must be set together with propagation_rule.
            - "Possible values when I(propagation_rule=as_is or ignore):"
            - " - C(not_classified)"
            - "Possible values when I(propagation_rule=increase or decrease):"
            - " - C(information)"
            - " - C(warning)"
            - " - C(average)"
            - " - C(high)"
            - " - C(disaster)"
            - "Possible values when I(propagation_rule=fixed):"
            - " - C(ok)"
            - " - C(not_classified)"
            - " - C(information)"
            - " - C(warning)"
            - " - C(average)"
            - " - C(high)"
            - " - C(disaster)"
            - Required with C(propagation_rule)
        required: false
        type: str
    status_rules:
        description:
            - Status rules for the service.
        required: false
        type: list
        elements: dict
        suboptions:
            type:
                description:
                    - Condition for setting (New status) status.
                    - C(at_least_n_child_services_have_status_or_above) if at least (N) child services have (Status) status or above
                    - C(at_least_npct_child_services_have_status_or_above) if at least (N%) of child services have (Status) status or above
                    - C(less_than_n_child_services_have_status_or_below) if less than (N) child services have (Status) status or below
                    - C(less_than_npct_child_services_have_status_or_below) if less than (N%) of child services have (Status) status or below
                    - C(weight_child_services_with_status_or_above_at_least_w) if weight of child services with (Status) status or above is at least (W)
                    - C(weight_child_services_with_status_or_above_at_least_npct) if weight of child services with (Status) status or above is at least (N%)
                    - C(weight_child_services_with_status_or_below_less_w) if weight of child services with (Status) status or below is less than (W)
                    - C(weight_child_services_with_status_or_below_less_npct) if weight of child services with (Status) status or below is less than (N%)
                required: true
                type: str
            limit_value:
                description:
                    - "Limit value: N, N% or W"
                    - "Possible values: 1-100000 for N and W, 1-100 for N%"
                required: true
                type: int
            limit_status:
                description:
                    - Limit status.
                    - C(ok) OK
                    - C(not_classified) Not classified
                    - C(information) Information
                    - C(warning) Warning
                    - C(average) Average
                    - C(high) High
                    - C(disaster) Disaster
                required: true
                type: str
            new_status:
                description:
                    - New status value.
                    - C(not_classified) Not classified
                    - C(information) Information
                    - C(warning) Warning
                    - C(average) Average
                    - C(high) High
                    - C(disaster) Disaster
                required: true
                type: str

extends_documentation_fragment:
- community.zabbix.zabbix

ai	  
---
# 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

# Creates a new Zabbix service
- name: Create Zabbix service monitoring Apache2 in DCs in Toronto area
  # set task level variables
  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_service:
    name: "apache2 service Toronto"
    description: Apache2 services in Toronto area
    sortorder: 0
    propagation_rule: increase
    propagation_value: warning
    weight: 1
    state: present
    tags:
      - tag: zabbix_service
        value: apache2
      - tag: area
        value: Toronto
    problem_tags:
      - tag: service_name
        value: httpd
      - tag: area
        operator: like
        value: toronto
    status_rules:
      - type: at_least_n_child_services_have_status_or_above
        limit_value: 4242
        limit_status: ok
        new_status: average

- name: Create Zabbix service monitoring all Apache2 services
  # 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_service:
    name: apache2 service
    description: Apache2 services
    tags:
      - tag: zabbix_service
        value: apache2
      - tag: area
        value: global
    children:
      - "apache2 service Toronto"
z
---
)AnsibleModule)
ZabbixBaseNc                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)Servicec                     g }| j                   j                  j                  dd|ii      }|D ]  }|j                  |d           |S )Nfiltername	serviceid)_zapiservicegetappend)selfservice_nameservice_idsservicesr   s        s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_service.pyget_service_idszService.get_service_ids"  sS    ::%%))8fl5K*LM 	5Gw{34	5    c                     | j                   j                  r| j                   j                  d       | j                  j                  j                  |       y NTchanged)_module
check_mode	exit_jsonr   r   delete)r   r   s     r   delete_servicezService.delete_service)  s<    <<""LL""4"0

!!+.r   c           
      d    | j                   j                  j                  dd|idddddd      }|S )Nextendr   )outputr   selectParents
selectTagsselectProblemTagsselectChildrenselectStatusRules)r   r   r   )r   r   r   s      r   dump_serviceszService.dump_services.  sC    ::%%))X+WbIcv~9AX`t|@H+J K r   c           
      V   dddd}||   }|||||d}|r||d<   ng |d<   g |d<   |rDddd}|D ]:  }|d	   dd
d}d|v r||d      |d<   d|v r|d   |d<   |d   j                  |       < |rNg }| j                  j                  j                  dd|ii      }|D ]  }|j                  d|d   i        ||d<   ng |d<   |	rNg }| j                  j                  j                  dd|	ii      }|D ]  }|j                  d|d   i        ||d<   ng |d<   g |d<   |r|D ]  }i }d|v r:ddddddddd}|d   |vr| j                  j                  d       ||d      |d<   n| j                  j                  d       d|v r|d   }|d   dv r<t        |      d k  st        |      d!kD  r[| j                  j                  d"|z         n;t        |      d k  st        |      d#kD  r| j                  j                  d$|z         t        |      |d<   n| j                  j                  d%       d&|v r9d'dddddd(d)}|d&   |vr| j                  j                  d*       ||d&      |d&<   n| j                  j                  d+       d,|v r8ddddddd-}|d,   |vr| j                  j                  d.       ||d,      |d,<   n| j                  j                  d/       |d   j                  |        d|d0<   |
rO|| j                  j                  d1       dddddd2}|
|vr| j                  j                  d3       n||
   |d0<   d|d4<   |rR|
| j                  j                  d5       d'ddddddd)}||vr| j                  j                  d6       |S ||   |d4<   |S )7N012status_to_okmost_crit_if_all_childrenmost_crit_of_child_serv)r   	algorithm	sortorderdescriptionweighttagsproblem_tags)equalsliketag r;   operatorvaluer>   r?   r   r   r   parentschildrenstatus_rulestype34567).at_least_n_child_services_have_status_or_above1at_least_npct_child_services_have_status_or_above/less_than_n_child_services_have_status_or_below2less_than_npct_child_services_have_status_or_below5weight_child_services_with_status_or_above_at_least_w8weight_child_services_with_status_or_above_at_least_npct1weight_child_services_with_status_or_below_less_w4weight_child_services_with_status_or_below_less_npctz0Wrong value for 'type' parameter in status rule.)msgz.'type' is mandatory paremeter for status rule.limit_value)r,   r.   rE   rG      i zF'limit_value' for N and W must be between 1 and 100000 but provided %sd   z?'limit_value' for N%% must be between 1 and 100 but provided %sz5'limit_value' is mandatory paremeter for status rule.limit_statusz-1   )oknot_classifiedinformationwarningaveragehighdisasterz8Wrong value for 'limit_status' parameter in status rule.z6'limit_status' is mandatory paremeter for status rule.
new_status)rX   rY   rZ   r[   r\   r]   z6Wrong value for 'new_status' parameter in status rule.z4'new_status' is mandatory paremeter for status rule.propagation_rulezPIf 'propagation_rule' is provided then 'propagation_value' must be provided too.)as_isincreasedecreaseignorefixedz-Wrong value for 'propagation_rule' parameter.propagation_valuezPIf 'propagation_value' is provided then 'propagation_rule' must be provided too.z.Wrong value for 'propagation_value' parameter.)r   r   r   r   r   	fail_jsonintstr) r   r   r4   r6   r3   r5   r7   r8   r@   rA   r_   re   rB   
algorithmsrequestp_operatorsp_tagptp_service_ids
p_services	p_servicec_service_ids
c_services	c_services_rulestatus_rulesr_type_maplv	sr_ls_map	sr_ns_mappr_mappv_maps                                    r   generate_service_configzService.generate_service_config5  s   &)hkl
y)	 ""&
 "GFO GFO"$%(#6K% 3"5\sRH&%0z1B%CBzNe#"'.BwK'..r23 M++//FG;L0MNJ' L	$$k9[3I%JKL!.GI!#GIM++//FH;M0NOJ' L	$$k9[3I%JKL"/GJ"$GJ"$& /< V#UXX[VYY\\__bX[[^#`K f~[8..3e.f*5fVn*EK'LL**/_*` F*.B"6*.BBr7Q;#b'F*: LL227  CE  8E2  Fr7Q;#b'C- LL227x{}7}2~14RK.LL**/f*g!V+'+sSVcf,/!!MIn-Y>..3m.n2;F><R2SK/LL**/g*h6)36sWZ,/#!OIl+9<..3k.l09&:N0OK-LL**/e*f'..{;_/<b '*"# (&&+}&~"PS^abFv-&&+Z&[.45E.F*+'*#$'&&+}&~ CX[!$csDF .&&+[&\  066G/H+,r   c                     | j                   j                  r| j                   j                  d       | j                  j                  j                  | j                  |||||||||	|
||             y r   )r   r   r   r   r   creater|   )r   r   r4   r6   r3   r5   r7   r8   r@   rA   r_   re   rB   s                r   create_servicezService.create_service  sq    <<""LL""4"0

!!$">">tYPV"+[$gW_aq  tE  GS#T 	Ur   c                    | j                  ||||||||	|
|||      }| j                  |      d   }t        |d         dkD  r'g }|d   D ]  }|j                  d|d   i        ||d<   t        |d         dkD  r'g }|d   D ]  }|j                  d|d   i        ||d<   i }t	        j
                  t	        j                  |||            }|i k(  r | j                  j                  dd|z         | j                  j                  r| j                  j                  d	       ||d<   | j                  j                  j                  |       | j                  j                  dd
|z         y )Nr   r@   r   rA   FzService %s up to dater   rQ   Tr   zService %s updated)r|   r*   lenr   zabbix_utilshelper_cleanup_datahelper_compare_dictionariesr   r   r   r   r   update)r   
service_idr   r4   r6   r3   r5   r7   r8   r@   rA   r_   re   rB   generated_configlive_confignew_parentsparentnew_childrenchildchange_parameters
differences                         r   update_servicezService.update_service  s   77iQZ8CT<Y`bjl|  P  R^_((4Q7{9%&*K%i0 G""K1D#EFG%0K	"{:&'!+L$Z0 G##[%2D$EFG&2K
#!55l6^6^_oq|  P  7Q  R
LL""56MPT6T"U<<""LL""4"0(2%

!!"23t1E1LMr   N)	__name__
__module____qualname__r   r!   r*   r|   r   r    r   r   r	   r	   !  s$    /
wrUNr   r	   c                     t        j                         } | j                  t        t        dd      t        ddg d      t        dd      t        ddd	      t        d
d
dg      t        dd      t        dddt        t        dd      t        dd                  t        dddt        t        dd      t        ddddgd      t        ddd                  t        ddd      t        ddd      t        ddd	      t        dd      t        dddt        t        dd      t        dd      t        dd      t        d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)   }t        |      }|j                  |      }|dk(  r@|s|j                  dd*|z  +       |j                  |       |j                  dd,|z  -       y |d
k(  rV|s3|j                  |||||||	|
||||       |j                  dd.|z  +       y |j                  |d/   |||||||	|
||||       y y )0Nrh   T)rC   requiredr0   Fr/   )defaultr   choicesr,   )r   rC   r   presentabsent)r   r   listdict)r;   r?   )rC   r   elementsoptionsr9   r:   )rC   r   r   r   r<   )rC   r   r   r=   )rC   r   r   r`   rg   )rC   rR   rU   r^   )r   r3   r4   r6   stater5   r7   r8   r@   rA   r_   re   rB   )argument_specsupports_check_moder   r3   r4   r6   r   r5   r7   r8   r@   rA   r_   re   rB   z Service not found, no change: %sr   z"Successfully deleted service(s) %s)r   resultzService %s createdr   )r   zabbix_common_argument_specr   r   r   paramsr	   r   r   r!   r   r   )r   moduler   r3   r4   r6   r   r5   r7   r8   r@   rA   r_   re   rB   r   r   s                    r   mainr     sX    <<>Mut,~  HP  QED1Cee<9y(.CDee4! "		
 ! "  % "	
2 &55A6EEBgEEJEE:! !! "!  !	
gJ JV # F
 == Dk*Ik*I]]8$FMM'"E--.K== D==0LmmI&G}}Z(H}}%78&9:==0L foG))$/K U0RUY0YZ{+.RUY.YZ	)	""4FI{#'wJZ\mo{}T/Cd/JK "";q>4F#,k4wX`br  uF  HTU 
r   __main__)
__future__r   r   r   rC   __metaclass__DOCUMENTATIONEXAMPLESRETURN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   r   <module>r      sj    A @AFFP

 5 U X X XsNj sNltUn zF r   