
    Vh&                         d Z ddlmZ ddlZddlZddl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mZ  G d d	e      Z e        y)
a`  
Zabbix Server external inventory script.
========================================

Returns hosts and hostgroups from Zabbix Server.
If you want to run with --limit against a host group with space in the
name, use asterisk. For example --limit="Linux*servers".

Configuration is read from `zabbix.ini`.

Tested with Zabbix Server 2.0.6, 3.2.3 and 3.4.
    )print_functionN)configparser)LooseVersion)Request)URLError	HTTPErrorc                   J    e Zd Zd Zd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd Zy)ZabbixInventoryc                     t        j                         }d}t        j                  j	                  |      sCt        j                  j                  t        j                  j                  t                    dz   }t        j                  j	                  |      r|j                  |       |j                  dd      r|j                  dd      | _        |j                  dd      r|j                  dd      | _        |j                  dd      r|j                  dd      | _        |j                  dd      r|j                  dd      | _        |j                  dd      r|j                  dd      d	v rd
| _        |j                  dd      r|j                  dd      | _        |j                  dd      r|j                  dd      dv rd| _        |j                  dd      r|j                  dd      d	v rd
| _        y y y )Nz./zabbix.iniz/zabbix.inizabbixserverusernamepassword
auth_tokenvalidate_certs)falseFalseFFtimeoutread_host_inventory)trueTrueTTuse_host_interface)r   ConfigParserospathexistsdirnamerealpath__file__read
has_optiongetzabbix_serverzabbix_usernamezabbix_passwordr   r   r   r   r   )selfconfig	conf_paths      m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/zabbix/scripts/inventory/zabbix.pyread_settingszZabbixInventory.read_settings3   s   **,"	ww~~i((8(8(BCmSI77>>)$KK	"Xx0!'Hh!?D Xz2#)::h
#CD Xz2#)::h
#CD X|4$jj<@DOX'78zz($459RR&+#Xy1!::h	:DLX'<=zz($9:>TT+/(X';<zz($89=VV*/' W =    c                     t        j                         }|j                  d       |j                  dd       |j                         | _        y )Nz--hostz--list
store_true)action)argparseArgumentParseradd_argument
parse_argsoptions)r&   parsers     r)   read_clizZabbixInventory.read_cliU   sA    ((*H%H\:((*r+   c                 
    dg iS )Nhosts r&   s    r)   hoststubzZabbixInventory.hoststub[   s    R
 	
r+   Nc                 z   | j                   }| j                  }| j                  }ddi}d|dd}|i |d<   n||d<   | j                  dk7  rCt	        | j
                        t	        d      k\  rd	| j                  z   |d
<   n| j                  |d<   |dz   }t        |||      }		 |	j                  |t        j                  |            }
|
S # t        $ r4 t        dt        j                         t        j                  d       Y 
S t        t         f$ r&}t        |t        j                         Y d }~
S d }~ww xY w)NzContent-Typezapplication/json-rpcz2.01)jsonrpcmethodidparams z6.4zBearer Authorizationauthz/api_jsonrpc.php)headersr   r   )dataz.Error: Something went wrong with JSON loading.file   )r#   r   r   rC   r   zabbix_versionr   postjsondumps
ValueErrorprintsysstderrexitr   r   )r&   r>   r@   
server_urlr   r   rD   payloadapi_urlreqresponseerrors               r)   api_requestzZabbixInventory.api_request`   s7   ''
,,,,!#9:

 > "GH &GH99?T001\%5HH+4tyy+@("&))11)

	*xxdjj.AxBH   	BTHHQK  )$ 	*%cjj))	*s   "&C
 
9D:D:D55D:c                 d    | j                  d      }t        j                  |      }|d   | _        y )Nzapiinfo.versionresult)rX   rK   loadrI   )r&   rV   ress      r)   get_versionzZabbixInventory.get_version   s1    ##
 ii!!(mr+   c                    | j                   }|r|| _        y t        j                  | j                         | j
                  }| j                  }| j                  d||d      }t        j                  |      }|d   | _        y )Nz
user.login)r   r   rZ   )
r   rC   atexitregisterlogout_zabbixr$   r%   rX   rK   r[   )r&   r   
login_userlogin_passwordrV   r\   s         r)   login_zabbixzZabbixInventory.login_zabbix   sy    __
"DI**+))
--##&*
 ii!M	r+   c                 (    | j                  dg        y )Nzuser.logout)rX   r9   s    r)   ra   zZabbixInventory.logout_zabbix   s    	
r+   c                    ddd|gid}| j                   rg d|d<   | j                  rd|d<   d|i}| j                   s| j                  r| j                  d|      }t        j                  |      }t        |d	         d
kD  re|d	   d
   }d|v r<t        |d         dk\  r+|d   d
   d   dk(  r|d   d
   d   |d<   n|d   d
   d   |d<   d|v r|d   r|j                  |d          |S )Nextendhost)outputselectGroupsfilteruseipipdnsselectInterfacesselectInventoryansible_ssh_hosthost.getrZ   r   
interfacesrH   rm   0ro   rn   	inventory)r   r   rX   rK   r[   lenupdate)r&   name	api_queryrE   rV   response_obj	host_datas          r)   get_hostzZabbixInventory.get_host   s'   'fW[V\M]^	"",BI()##+3I'("D)""d&>&>''
I>H99X.L<)*Q.(215	9,9\23q8$\215g>#E7@7Nq7QRW7XD!347@7Nq7QRV7WD!349,9[3IKK	+ 67r+   c                    ddd}| j                   rg d|d<   | j                  rd|d<   | j                  d|      }t        j                  |      d   }dd	i ii}| j                         || j                  <   |D ]  }|d
   }t               }|| j                     d   j                  |       |d   D ]5  }|d
   }	|	|vr| j                         ||	<   ||	   d   j                  |       7 d|v r<t        |d         dk\  r+|d   d   d   dk(  r|d   d   d   |d<   n|d   d   d   |d<   d|v r|d   r|j                  |d          ||d   d	   |<    |S )Nrg   )ri   rj   rl   rp   rq   rs   rZ   _metahostvarsry   r7   groupsrt   rH   r   rm   ro   rr   rn   rv   )r   r   rX   rK   r[   r:   defaultgroupdictappendrw   rx   )
r&   rz   rV   
hosts_datarE   rh   hostnamer   group	groupnames
             r)   get_listzZabbixInventory.get_list   s   'B	"",BI()##+3I'(##J	:YYx(2
*b)*"&--/T 	;DF|HvH""#G,33H=h :!&M	D(&*mmoDOY(//9: t#tL)*a/L)!,W5:7;L7I!7LU7S!347;L7I!7LT7R!34t#${*;[ 122:DM*%h//	;2 r+   c                    d| _         d | _        d | _        d | _        d | _        d| _        d| _        d| _        d| _        d| _	        d| _
        i | _        | j                          | j                          | j                  r| j                  r	 | j                          | j                          | j,                  j.                  rF| j1                  | j,                  j.                        }t%        t3        j4                  |d	
             y | j,                  j6                  r1| j9                         }t%        t3        j4                  |d	
             y t%        dt&        j(                         t'        j*                  d       y t%        dt&        j(                         t'        j*                  d       y # t         t"        f$ r>}t%        d|z  t&        j(                         t'        j*                  d       Y d }~Md }~ww xY w)N	group_allrA   T   Fz5Error: got the exception '%s'. Check your zabbix.ini.rF   rH      )indentz'usage: --list  ..OR.. --host <hostname>zLError: Configuration of server and credentials are required. See zabbix.ini.)r   r#   r$   r%   r   rC   r   r   r   r   rI   metar*   r5   r]   rd   	Exception
SystemExitrN   rO   rP   rQ   r3   rh   r}   rK   rL   listr   )r&   erE   s      r)   __init__zZabbixInventory.__init__   s}   '!##	"#( "& 	$"6"6  "!!# ||  }}T\\%6%67djja01""}}djja01 ?cjjQ `gjgqgqrHHQK% z* MPQQX[XbXbcs    F4 4H3G<<H)N)__name__
__module____qualname__r*   r5   r:   rX   r]   rd   ra   r}   r   r   r8   r+   r)   r
   r
   1   s7     0D+

$L,"(
4$L+r+   r
   )__doc__
__future__r   r   rO   r/   rK   r_   ansible.module_utils.six.movesr   #ansible.module_utils.compat.versionr   ansible.module_utils.urlsr   +ansible.module_utils.six.moves.urllib.errorr   r   objectr
   r8   r+   r)   <module>r      sA   . & 	 
    7 < - Kaf aH  r+   