
    Vh$%                         d dl mZmZmZ e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Z d dlmZ d dlmZ d d	lmZ  G d
 de      Zy)    )absolute_importdivisionprint_functiona  
---
name: zabbix
author:
  - Markus Fischbacher (@rockaut)
  - Evgeny Yurchenko (@BGmot)
short_description: HttpApi Plugin for Zabbix
description:
  - This HttpApi plugin provides methods to connect to Zabbix over their HTTP(S)-based api.
version_added: 1.8.0
options:
  zabbix_auth_key:
    type: str
    description:
      - Specifies API authentication key
    env:
      - name: ANSIBLE_ZABBIX_AUTH_KEY
    vars:
      - name: ansible_zabbix_auth_key
  zabbix_url_path:
    type: str
    description:
      - Specifies path portion in Zabbix WebUI URL, e.g. for https://myzabbixfarm.com/zabbixeu zabbix_url_path=zabbixeu.
      - "If Zabbix WebUI is running at the root, i.e. https://myzabbixfarm.com/, then assign empty string to this variable C(zabbix_url_path: '')."
    default: zabbix
    env:
      - name: ANSIBLE_ZABBIX_URL_PATH
    vars:
      - name: ansible_zabbix_url_path
  http_login_user:
    type: str
    description:
      - The http user to access zabbix url with Basic Auth
    vars:
      - name: http_login_user
  http_login_password:
    type: str
    description:
      - The http password to access zabbix url with Basic Auth
    vars:
      - name: http_login_password
N)uuid4)to_text)AnsibleConnectionFailure)HttpApiBase)StrictVersion)ConnectionErrorc                   v     e Zd ZdZdZ fdZd Zd Zd Zd Z	d Z
dd	Zd
 Zd Zd Zedd       Zd Z xZS )HttpApiNz/zabbixc                 J    t         |   |       ddi| j                  _        y )NzContent-Typezapplication/json-rpc)super__init__
connection_auth)selfr   	__class__s     k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/plugins/httpapi/zabbix.pyr   zHttpApi.__init__E   s"    $!/1G H    c                      y)zDAs this is an http rpc call there is no elevation available
        N )r   become_contexts     r   
set_becomezHttpApi.set_becomeI   s     	r   c                      y Nr   )r   responseresponse_texts      r   update_authzHttpApi.update_authN   s    r   c                 &   | j                  d      x}r|| _        y t        | j                               t        d      k\  r| j	                  d||      }n| j	                  d||      }| j                  |      \  }}|dk(  r|dk7  r|| _        y y y )	Nzabbix_auth_keyz6.0
user.login)usernamepassword)userr$   data    )
get_optionauthr
   api_versionpayload_buildersend_request)r   r#   r$   auth_keypayloadcoder   s          r   loginzHttpApi.loginQ   s    '89989 DI
 ))+,e0DD**<(U]*^G**<hQY*ZG***8h3;8r> DI *;r   c                     | j                   r6| j                  d      s$| j                  d      }| j                  |       y y y )Nr!   zuser.logoutr&   )r+   r*   r-   r.   )r   r0   s     r   logoutzHttpApi.logoutc   s>    99T__->?**=9G7+ @9r   c                    | j                  d      }t        |t              r|dk(  rd| _        n
d|z   | _        t	        | j
                  d      so| j                  | j                  d            \  }}|dk(  r5t        |      dk7  r'|| j
                  _	        | j
                  j                  S t        d	|d
|      | j
                  j                  S )Nzabbix_url_pathr)   /zbx_api_versionapiinfo.versionr&   r(   r   z5Could not get API version from Zabbix. Got HTTP code z. Got version )r*   
isinstancestrurl_pathhasattrr   r.   r-   lenr8   r   )r   r<   r1   versions       r   r,   zHttpApi.api_versionh   s    ??#45h$2~ " #ht(9: --43G3GHY3Z-[MD's{s7|q029/ ... &sw  zA  'B  C  C...r   c                 V   i }| j                   |z   }| j                  rN|d   dvrGt        | j                               t        d      k\  rd| j                  z   |d<   n| j                  |d<   | j	                  d      }| j	                  d      }|rF|d	k7  rA|d
z   |z   j                  d      }dt        j                  |      j                  d      z   |d<   	 | j                  |||d          | j                  j                  |t        j                  |      ||      \  }}	t        |	j                               }
	 |
rt        j                   |
      ni }d|v r=d|d   d   v r| j'                  | j                  j	                  d      | j                  j	                  d             t        j                   |      }| j                  j(                  d   |d<   t        j                  |      }| j                  j                  ||||      \  }}	t        |	j                               }
	 |
rt        j                   |
      ni }d|v rt%        d|d   d|      d|v r|d   }	 t+        |       |j/                         |fS t%        d|d   d|      d|v r|d   }	 t+        |       |j/                         |fS # t"        $ r t%        d|
z        w xY w# t"        $ r t%        d|
z        w xY w# t,        $ r |j/                         |fcY S w xY w# t,        $ r |j/                         |fcY S w xY w# t0        $ r[}| j                  j3                  dd|z         t        d      t        |      v r t        d      t        |      v rY d }~yY d }~yd }~wt4        $ r}|d }~ww xY w)Nmethod)r"   r9   z6.4zBearer Authorizationr+   http_login_userhttp_login_passwordz-42:asciizBasic )rA   headersInvalid JSON response: %serrorzre-loginr'   remote_userr$   zREST API returned z when sending resultvvvzAnsibleConnectionFailure: %szCould not connect to401)  zAuthentication failure)i  zObject not found)r<   r+   r
   r,   r*   encodebase64	b64encodedecode_display_requestr   sendjsondumpsr   getvalueloads
ValueErrorr   r2   r   iter	TypeErrorgetcoder   queue_message	Exception)r   r'   request_methodpathrG   rC   rD   credentialsr   response_datavalue	json_dataes                r   r.   zHttpApi.send_requestx   s   }}t#99h/PPT--/0M%4HH+4tyy+@(#yyV//*;<"oo.CD%7*S03FFNNwWK'/&2B2B;2O2V2VW^2_'_GO$M	!!.$XG&*oo&:&:

4 %	 '; '#Hm M2245EK16DJJu-B	
 )#7!3F!;;JJt99-H$//JdJdeoJpq::d+D#'??#8#8#@DL::d+D.2oo.B.B- '	 /C /+Hm $M$:$:$<=ES9>DJJu$5B	
 )+-XabiXjlp.qrr9,$-h$7	=Y
 $++-y88%PYZaPbdh&ijj9$%h/	5Y
 ##%y00m  K%&AE&IJJK0 & S-.IE.QRRS % ='//19<<=  5'')9445
 ( 	/OO))%1ORS1ST-.'!*<u~+4. 	G	s   A$L6 <J> CL6 +K !L6 &K4 1L6 L6 !L ,L6 >KL6 K11L6 4LL6 LL6 L30L6 2L33L6 6	N(?ANN(!N##N(c                 |    | j                   j                  dd|d| j                   j                  |d|d       y )NvvvvzZabbix httpapi request:  z ())r   r]   _url)r   r_   r`   jsonrpc_methods       r   rS   zHttpApi._display_request   s0    %% 4 4dNL	
r   c                 4    t        |j                               S r   )r   rW   )r   rb   s     r   _get_response_valuezHttpApi._get_response_value   s    }--/00r   c                 l    	 |rt        j                  |      S i S # t        $ r t        d|z        w xY w)NrH   )rU   rX   rY   r   )r   r   s     r   _response_to_jsonzHttpApi._response_to_json   s@    	O0=4::m,E2E 	O!"="MNN	Os     3c                 B    t        t                     }d| |d}||d<   |S )Nz2.0)jsonrpcrA   idparams)r;   r   )method_auth_kwargsreqidreqs        r   r-   zHttpApi.payload_builder   s'    EG7%@H
r   c                 &    |j                   dk(  ry|S )NrN   F)r1   )r   excs     r   handle_httperrorzHttpApi.handle_httperror   s     88s?
r   )POSTz/api_jsonrpc.phpr   )__name__
__module____qualname__r+   r<   r   r   r   r2   r4   r,   r.   rS   rm   ro   staticmethodr-   r{   __classcell__)r   s   @r   r   r   A   s\    DHI
!$,
/ ^@
1O  r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONrU   rP   uuidr   ansible.module_utils.basicr   ansible.errorsr   ansible.plugins.httpapir	   #ansible.module_utils.compat.versionr
   ansible.module_utils.connectionr   r   r   r   r   <module>r      sC    A @)V    . 3 / = ;wk wr   