HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/zabbix.cpython-310.pyc
o

�N�g���	@sldZddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlmZddl
mZe�e�Zgd�ZdZidd�d	d
�dd�d
d�dd�dd�dd�dd�dd�dd�dd�dd�dd�d d!�d"d#�d$d�d%d&�id'd(�d)d�d*d�d+d,�d-d�d.d/�d0d1�d2d3�d4d5�d6d7�d8d9�d:d;�d<d=�d>d?�d@d9�dAd;�dBd��dCdCdDdEdFdGdHdI��ZdJZdKdL�ZdMdN�Zd�dOdP�ZdQdR�Zd�dTdU�ZdVdW�ZdXdY�Zd�d[d\�Zd�d]d^�Zd�d_d`�Zdadb�Z dcdd�Z!dedf�Z"dgdh�Z#d�didj�Z$dkdl�Z%d�dmdn�Z&dodp�Z'dqdr�Z(dsdt�Z)dudv�Z*dwdx�Z+d�dydz�Z,d�d{d|�Z-d}d~�Z.dd��Z/d�d��Z0d�d��Z1	d�d�d��Z2d�d�d��Z3d�d��Z4d�d��Z5d�d��Z6d�d��Z7d�d��Z8d�d��Z9	d�d�d��Z:d�d�d��Z;d�d�d��Z<d�d��Z=d�d��Z>	d�d�d��Z?d�d��Z@d�d��ZA						Sd�d�d��ZBd�d��ZCd�d��ZDd�d��ZEd�d��ZFd�d��ZGd�d��ZHd�d�d��ZId�d��ZJd�d��ZKd�d�d��ZLd�d�d��ZMd�d��ZNd�d�dÄZOdS)�a�
Support for Zabbix

:optdepends:    - zabbix server

:configuration: This module is not usable until the zabbix user and zabbix password are specified either in a pillar
    or in the minion's config file. Zabbix url should be also specified.

    .. code-block:: yaml

        zabbix.user: Admin
        zabbix.password: mypassword
        zabbix.url: http://127.0.0.1/zabbix/api_jsonrpc.php


    Connection arguments from the minion config file can be overridden on the CLI by using arguments with
    ``_connection_`` prefix.

    .. code-block:: bash

        zabbix.apiinfo_version _connection_user=Admin _connection_password=zabbix _connection_url=http://host/zabbix/

:codeauthor: Jiri Kotlin <jiri.kotlin@ultimum.io>
�N)�
SaltException)�Version)iB'�ioi90)
�	hostgroup�template�host�maintenance�action�drule�service�proxy�screen�	usergroup�	mediatype�script�valuemapr	ZactionidZalertZalertidZapplicationZ
applicationidZdhostZdhostidZdserviceZ
dserviceidZdcheckZdcheckidr
Zdruleid�eventZeventidZgraphZgraphidZ	graphitemZgitemidZgraphprototype�historyZitemidr�hostidr�groupidZ
hostinterface�interfaceidZ
hostprototypeZiconmapZ	iconmapidZimageZimageid�itemZ
itemprototyperZ	serviceidZ
discoveryrulerZ
maintenanceid�mapZsysmapidZ	usermediaZmediaidr�mediatypeidrZproxyidr
ZscreenidZ
screenitemZscreenitemidrZscriptidrZ
templateidZtemplatescreenZtemplatescreenitemZtrendZ	triggerid�userid�usrgrpid�
globalmacroidZ
valuemapidZ
httptestid)ZtriggerZtriggerprototype�userrZ	usermacrorZhttptestZzabbixcC�tS)zE
    Only load the module if all modules are imported correctly.
    )�__virtualname__�r r �G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/zabbix.py�__virtual__ksr"c
Cs�t��}d|d}z0z
tjj�|�}|d}Wntjjy0}z
t	|�}WYd}~nd}~ww|�
d�r9|WSt�ttfyFYdSw)zT
    Tries to guess the url of zabbix frontend.

    .. versionadded:: 2016.3.0
    zhttp://z/zabbix/api_jsonrpc.php�errorNz412: Precondition FailedF)
�socket�gethostname�salt�utils�http�query�urllibr#�	HTTPError�str�find�KeyError�
ValueError)�hostnameZfrontend_url�responser#Zhttp_er r r!�
_frontend_urlrs ��
�r2c

Cs*ddg}ddi}dd||d�}||vr||d<tjj�|�}t�d	t|�t|��zDtjjj|d
||ddddd
�}t�dt|��d|vrQt	d�
|d|d���|�di�}d|vrkt	d�
|dd|dd���|WSty�}	zt	d|	�d���d}	~	wt
y�}	zt	d|	�d���d}	~	ww)a�
    JSON request to Zabbix API.

    .. versionadded:: 2016.3.0

    :param method: actual operation to perform via the API
    :param params: parameters required for specific method
    :param url: url of zabbix api
    :param auth: auth token for zabbix api (only for methods with required authentication)

    :return: Response from API with desired data in JSON format. In case of error returns more specific description.

    .. versionchanged:: 2017.7.0
    �
user.login�apiinfo.versionzContent-typezapplication/jsonz2.0r)Zjsonrpc�id�method�params�authz_QUERY input:
url: %s
data: %s�POST�jsonT)r6�data�header_dictZdecode_type�decode�status�headersz_QUERY result: %sr#zZabbix API: Status: {} ({})r>�dictzZabbix API: {} ({})�messager;z/URL or HTTP headers are probably not correct! (�)NzCheck hostname in URL! ()r&r'r:�dumps�log�infor,r(r)r�format�getr/�OSError)
r6r7�urlr8Zunauthenticated_methodsr<r;�result�ret�errr r r!�_query�sP��
������rMc
s�t��d��fdd�	}|dd�|dd�|dd	�d	�vr#t��d	<z8�drZ�drZ�d	rZ�d�dd
�}d}t||�d	�}|d}|�d
<��dd���dd��WSt�tyo}ztd|�d���d}~ww)a%
    Log in to the API and generate the authentication token.

    .. versionadded:: 2016.3.0

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: On success connargs dictionary with auth token and frontend url, False on failure.

    Ncs�|dur|}|�vr�|�|<dSd}|�|�r.z
|t|�d�}Wn
ty-YdSwtdd|��d�pAtdd|��d�}|durL|�|<dSdS)ap
        Add key to connargs, only if name exists in our kwargs or, as zabbix.<name> in __opts__ or __pillar__

        Evaluate in said order - kwargs, opts, then pillar. To avoid collision with other functions,
        kwargs-based connection arguments are prefixed with 'connection_' (i.e. '_connection_user', etc.).

        Inspired by mysql salt module.
        NZ_connection_z
config.getzzabbix.zzabbix:)�
startswith�len�
IndexError�__salt__)�name�key�prefix�val�Zconnargs�kwargsr r!�_connarg�s&	
��
��z_login.<locals>._connargZ_connection_userrZ_connection_password�passwordZ_connection_urlrI)rrYr3rJr8zURL is probably not correct! (rB�N)r@r2rM�popr.r)rWrXr7r6rKr8rLr rVr!�_login�s,




��r\FcKsh|D]}|�d�s|�|||�q|r2|�dd�d|vr'|�d�|d<|Sd|vr2|�d�|d<|S)aG
    Extends the params dictionary by values from keyword arguments.

    .. versionadded:: 2016.3.0

    :param params: Dictionary with parameters for zabbix API.
    :param _ignore_name: Salt State module is passing first line as 'name' parameter. If API uses optional parameter
    'name' (for ex. host_create, user_create method), please use 'visible_name' or 'firstname' instead of 'name' to
    not mess these values.
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Extended params dictionary with parameters.

    �_rRNZ	firstnameZvisible_name)rN�
setdefaultr[)r7�_ignore_namerWrSr r r!�_params_extends
��r`cCs g}|D]	}|�||i�q|S)a
    Maps list of values to list of dicts of values, eg:
        [usrgrpid1, usrgrpid2, ...] => [{"usrgrpid": usrgrpid1}, {"usrgrpid": usrgrpid2}, ...]

    :param source:  list of values
    :param key: name of dict key
    :return: List of dicts in format: [{key: elem}, ...]
    )�append)�sourcerS�output�elemr r r!�_map_to_list_of_dicts's	recCr)z�
    .. versionadded:: 2017.7.0

    Make ZABBIX_ID_MAPPER constant available to state modules.

    :return: ZABBIX_ID_MAPPER

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.get_zabbix_id_mapper
    )�ZABBIX_ID_MAPPERr r r r!�get_zabbix_id_mapper6srgrRcs��duri�t|t�r���fdd�|D�St|t�r_d|vrRi}|dtvr,|���z|�d�|dii�t|d|fi���WStyQtd�|���w���fdd	�|�	�D�St
|�S)
a
    .. versionadded:: 2017.7.0

    Go through Zabbix object params specification and if needed get given object ID from Zabbix API and put it back
    as a value. Definition of the object is done via dict with keys "query_object" and "query_name".

    :param input_object: Zabbix object type specified in state file
    :param extend_params: Specify query with params
    :param filter_key: Custom filtering key (default: name)
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Params structure with values converted to string for further comparison purposes

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.substitute_params '{"query_object": "object_name", "query_name": "specific_object_name"}'
    Ncs g|]}t|��fi����qSr ��substitute_params)�.0Zoitem��
extend_params�
filter_keyrWr r!�
<listcomp>`s��z%substitute_params.<locals>.<listcomp>Zquery_object�filterZ
query_namez=Qyerying object ID requested but object name not provided: {}cs&i|]\}}|t|��fi����qSr rh)rjrSrUrkr r!�
<dictcomp>vs��z%substitute_params.<locals>.<dictcomp>)�
isinstance�listr@�ZABBIX_TOP_LEVEL_OBJECTS�update�get_object_id_by_paramsr.rrF�itemsr,)Zinput_objectrlrmrWZquery_paramsr rkr!riGs<
�

������ric
CsBt|t|��std�t|�t|�||���tjj�|�s=t|�t|�kr/|r/t|�t|�d�St|�t|�kr=|s=t|�St|t	�r�t
|�t
|�krXt�d�|rV||d�S|Sg}|D]'}g}|D]}t
|||�}|rt|�|d�qb|�|�qbt|�r�|�|�q\t|�r�|r�||d�St|�r�|s�|St|t��rzkt|�t|�kr�t|�t|�@}|r�iid�ni}	|D]@}
t
||
||
|�}|r�|s�|s�t|t	�r�|	d�|
||
i�|	d�|
||
i�q�|s�|s�t|t	�r�|	�|
||
i�q�|	WS|�r||d�WS|WSt�ytd�t|�t|�||���wdS)a�
    .. versionadded:: 2017.7.0

    Compares Zabbix object definition against existing Zabbix object.

    :param defined: Zabbix object definition taken from sls file.
    :param existing: Existing Zabbix object taken from result of an API call.
    :param return_old_value: Default False. If True, returns dict("old"=old_val, "new"=new_val) for rollback purpose.
    :return: Params that are different from existing object. Result extended by
        object ID can be passed directly to Zabbix API update method.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.compare_params new_zabbix_object_dict existing_zabbix_onject_dict
    zwZabbix object comparison failed (data type mismatch). Expecting {}, got {}. Existing value: "{}", defined value: "{}").)�new�oldzDifferent list length!rwrxN)rq�typerrFr&r'r;Zis_iterr,rrrOrDrE�compare_paramsra�all�anyr@�setrt�	TypeError)Zdefined�existingZreturn_old_valueZdifflistZditemZd_in_eZeitem�comp�intersectionZdiffdict�ir r r!rz�sr��


�
������rzcKs\|duri}t|d|fi|��}|r#t|�dkr#t|dt|�Std�|d||���)a�
    .. versionadded:: 2017.7.0

    Get ID of single Zabbix object specified by its name.

    :param obj: Zabbix object type
    :param params: Parameters by which object is uniquely identified
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: object ID

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.get_object_id_by_params object_type params=zabbix_api_query_parameters_dict
    Nz.get�rz�Zabbix API: Object does not exist or bad Zabbix user permissions or other unexpected result. Called method {} with params {}. Result: {})�	run_queryrOr,rfrrF)�objr7�connection_args�resr r r!ru�s
��rucKsXtdi|��}d}z|r d}i}t|||d|d�}|dWSt�ty+YdSw)aR
    Retrieve the version of the Zabbix API.

    .. versionadded:: 2016.3.0

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: On success string with Zabbix API version, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.apiinfo_version
    Fr4rIr8rJNr �r\rMr.�r��	conn_argsrKr6r7r r r!�apiinfo_version�s
�r�c
Ks�tdi|��}tdi|��}d}d}t|�td�krd}z9|rTd}||d|dgi}	t|t�s1|g}t|d�|	d<t|	fd	d
i|��}	t||	|d|d�}|d
dWSt�ty`|YSw)ai
    .. versionadded:: 2016.3.0

    Create new zabbix user

    .. note::
        This function accepts all standard user properties: keyword argument
        names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.0/manual/appendix/api/user/definitions#user

    :param alias: user alias
    :param passwd: user's password
    :param usrgrps: user groups to add the user to

    :param _connection_user: zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: url of zabbix frontend (can also be set in opts or pillar, see module's docstring)

    :param firstname: string with firstname of the user, use 'firstname' instead of 'name' parameter to not mess
                      with value supplied from Salt sls file.

    :return: On success string with id of the created user.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_create james password007 '[7, 12]' firstname='James Bond'
    F�alias�5.2�usernamezuser.create�passwd�usrgrpsrr_TrIr8rJ�useridsNr )	r\r�rrqrrrer`rMr.)
r�r�r�r�r��zabbix_versionrK�username_fieldr6r7r r r!�user_creates(
�r�cK�ptdi|��}d}z#|r+d}t|t�s|g}n|}t|||d|d�}|ddWSt�ty7|YSw)	af
    Delete zabbix users.

    .. versionadded:: 2016.3.0

    :param users: array of users (userids) to delete
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: On success array with userids of deleted users.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_delete 15
    Fzuser.deleterIr8rJr�Nr �r\rqrrrMr.)�usersr�r�rKr6r7r r r!�user_deleteRs
�r�cKs�td
i|��}td
i|��}d}d}t|�td�krd}z"|r=d}d||id�}t|||d|d	�}|d
r:dWSdWSt�tyI|YSw)aU
    Checks if user with given alias exists.

    .. versionadded:: 2016.3.0

    :param alias: user alias
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: True if user exists, else False.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_exists james
    Fr�r�r��user.get�extend�rcrorIr8rJTNr )r\r�rrMr.)r�r�r�r�rKr�r6r7r r r!�user_existsws �r�c	Ks�tdi|��}tdi|��}d}d}t|�td�krd}zI|rdd}ddddid�}|s3|s3ddd	�WS|r=|d
�||�|rE|�d|�t|fi|��}t|||d|d
�}|dra|dWSdWSt�typ|YSw)a�
    Retrieve users according to the given parameters.

    .. versionadded:: 2016.3.0

    :param alias: user alias
    :param userids: return only users with the given IDs
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with details of convenient users, False on failure of if no user found.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_get james
    Fr�r�r�r�r�)rcZ
selectUsrgrpsZselectMediasZselectMediatypesroz;Please submit alias or userids parameter to retrieve users.�rJZcommentror�rIr8rJNr �r\r�rr^r`rMr.)	r�r�r�r�r�rKr�r6r7r r r!�user_get�s<���r�cKs.tdi|��}tdi|��}d}|�dd�}|dur!|�dd�}n	|�|�dg��za|r�d}d|i}t|�td�krG|durGddd	�}|WSt|�td
�krX|durX||d<n|dur`||d<d|vrnt|�d�d�|d<t|fd
di|��}t|||d|d�}|ddWSt�ty�|YSw)an
    .. versionadded:: 2016.3.0

    Update existing users

    .. note::
        This function accepts all standard user properties: keyword argument
        names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.0/manual/appendix/api/user/definitions#user

    :param userid: id of the user to update
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Id of the updated user on success.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_update 16 visible_name='James Brown'
    F�mediasNZuser_mediaszuser.updater�3.4z'Setting medias available in Zabbix 3.4+r�z5.0r�rr_TrIr8rJr�r )	r\r�r[r�rrer`rMr.)rr�r�r�rKr�r6r7r r r!�user_update�s@��

��r�c
Ks�td
i|��}td
i|��}d}t|�td�kr3td
d|i|��}g}|D]}|�|�dg��q%|Sz&|rXd}i}	|r@||	d<t|	fi|��}	t||	|d|d�}|dWSt�tyd|YSw)a�
    .. versionadded:: 2016.3.0

    Retrieve media according to the given parameters

    .. note::
        This function accepts all standard usermedia.get properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/3.2/manual/api/reference/usermedia/get

    :param userids: return only media that are used by the given users

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: List of retrieved media, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_getmedia
    Fr�r�r�z
usermedia.getrIr8rJNr )	r\r�rr�r�rGr`rMr.)
r�r�r�r�rKr�r�rr6r7r r r!�
user_getmedias,
�r�c
Ks�tdi|��}tdi|��}d}	d}
t|�td�kr$dd�|
�d�}	|	Sz=|r`d}
dgi}t|t�s5|g}|D]}|d�d|i�q7|||||d�g|d	<t|
||d
|d�}	|	dd
WSt�tyl|	YSw)aT
    Add new media to multiple users. Available only for Zabbix version 3.4 or older.

    .. versionadded:: 2016.3.0

    :param userids: ID of the user that uses the media
    :param active: Whether the media is enabled (0 enabled, 1 disabled)
    :param mediatypeid: ID of the media type used by the media
    :param period: Time when the notifications can be sent as a time period
    :param sendto: Address, user name or other identifier of the recipient
    :param severity: Trigger severities to send notifications about
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the created media.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_addmedia 4 active=0 mediatypeid=1 period='1-7,00:00-24:00' sendto='support2@example.com'
        severity=63

    Fz
user.addmediar��4Method '{}' removed in Zabbix 4.0+ use 'user.update'r�r�r)�activer�period�sendto�severityr�rIr8rJ�mediaidsNr )	r\r�rrFrqrrrarMr.)
r�r�rr�r�r�r�r�r�rKr6r7rr r r!�
user_addmediaRs@��
��
�r�cKs�tdi|��}tdi|��}d}d}t|�td�kr$dd�|�d�}|Sz |rCt|t�s/|g}|}t|||d|d�}|dd	WSt�tyO|YSw)a�
    Delete media by id. Available only for Zabbix version 3.4 or older.

    .. versionadded:: 2016.3.0

    :param mediaids: IDs of the media to delete
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the deleted media, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_deletemedia 27
    Fzuser.deletemediar�r�r�rIr8rJr�Nr )r\r�rrFrqrrrMr.)r�r�r�r�rKr6r7r r r!�user_deletemedia�s,��
�r�cK�^td	i|��}d}z|r"d}ddi}t|||d|d�}|dWSt�ty.|YSw)
a&
    Retrieve all of the configured users.

    .. versionadded:: 2016.3.0

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with user details.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.user_list
    Fr�rcr�rIr8rJNr r�r�r r r!�	user_list�s
�r�cK�rtd	i|��}d}z$|r,d}d|i}t|fi|��}t|||d|d�}|ddWSt�ty8|YSw)
aj
    .. versionadded:: 2016.3.0

    Create new user group

    .. note::
        This function accepts all standard user group properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.0/manual/appendix/api/usergroup/definitions#user_group

    :param name: name of the user group
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return:  IDs of the created user groups.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_create GroupName
    Fzusergroup.createrRrIr8rJ�	usrgrpidsNr �r\r`rMr.�rRr�r�rKr6r7r r r!�usergroup_create���r�cKsntdi|��}d}z"|r*d}t|t�s|g}|}t|||d|d�}|ddWSt�ty6|YSw)	aG
    .. versionadded:: 2016.3.0

    :param usergroupids: IDs of the user groups to delete

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the deleted user groups.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_delete 28
    Fzusergroup.deleterIr8rJr�Nr r�)Zusergroupidsr�r�rKr6r7r r r!�usergroup_deletes
�r�c	Ks�tdi|��}tdi|��}d}zX|rgt|�td�kr-|sd}t|dfi|��}t|�WSd}i}|s=|s=|s=ddd�WS|rC||d<t|�td	�krW|rQ||d
<|rW||d<t|||d|d
�}|dWSt�tys|YSw)a|
    Checks if at least one user group that matches the given filter criteria exists

    .. versionadded:: 2016.3.0

    :param name: names of the user groups
    :param node: name of the node the user groups must belong to (This will override the nodeids parameter.)
    :param nodeids: IDs of the nodes the user groups must belong to

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: True if at least one user group that matches the given filter criteria exists, else False.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_exists Guests
    F�2.5�Nzusergroup.existszYPlease submit name, node or nodeids parameter to check if at least one user group exists.r�rR�2.4�node�nodeidsrIr8rJr )r\r�r�
usergroup_get�boolrMr.)	rRr�r�r�r�r�rKr6r7r r r!�usergroup_exists2s:
�
�r�c	Ks�tdi|��}tdi|��}d}z\|rkd}t|�td�kr$ddid�}ndid�}|s2|s2|s2WdS|r<|d�d|�|rD|�d	|�|rL|�d
|�t|fi|��}t|||d|d�}|d
sfdWS|d
WSt�tyw|YSw)a/
    .. versionadded:: 2016.3.0

    Retrieve user groups according to the given parameters

    .. note::
        This function accepts all usergroup_get properties: keyword argument
        names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/usergroup/get

    :param name: names of the user groups
    :param usrgrpids: return only user groups with the given IDs
    :param userids: return only user groups that contain the given users
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with convenient user groups details, False if no user group found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_get Guests
    F�
usergroup.getr�r�)ZselectRightsrcror�rorRr�r�rIr8rJNr r�)	rRr�r�r�r�r�rKr6r7r r r!r�os0
�r�cKr�)
a�
    .. versionadded:: 2016.3.0

    Update existing user group

    .. note::
        This function accepts all standard user group properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/usergroup/object#user_group

    :param usrgrpid: ID of the user group to update.
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the updated user group, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_update 8 name=guestsRenamed
    Fzusergroup.updaterrIr8rJr�Nr r�)rr�r�rKr6r7r r r!�usergroup_update�r�r�cKr�)
aH
    Retrieve all enabled user groups.

    .. versionadded:: 2016.3.0

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with enabled user groups details, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_list
    Fr�rcr�rIr8rJNr r�r�r r r!�usergroup_list���
�r�cKs�tdi|��}d}zA|rId}d|i}t|t�s|g}t|d�|d<t|t�s)|g}||d<t|fddi|��}t|||d	|d
�}|ddWSt�tyU|YSw)a/
    .. versionadded:: 2016.3.0

    Create new host

    .. note::
        This function accepts all standard host properties: keyword argument
        names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/host/object#host

    :param host: technical name of the host
    :param groups: groupids of host groups to add the host to
    :param interfaces: interfaces to be created for the host
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)
    :param visible_name: string with visible name of the host, use
        'visible_name' instead of 'name' parameter to not mess with value
        supplied from Salt sls file.

    return: ID of the created host.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_create technicalname 4
        interfaces='{type: 1, main: 1, useip: 1, ip: "192.168.3.1", dns: "", port: 10050}'
        visible_name='Host Visible Name' inventory_mode=0 inventory='{"alias": "something"}'
    Fzhost.createrr�groups�
interfacesr_TrIr8rJ�hostidsNr )r\rqrrrer`rMr.)rr�r�r�r�rKr6r7r r r!�host_create�s& 

�r�cKr�)	aF
    Delete hosts.

    .. versionadded:: 2016.3.0

    :param hostids: Hosts (hostids) to delete.
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the deleted hosts.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_delete 10106
    Fzhost.deleterIr8rJr�Nr r��r�r�r�rKr6r7r r r!�host_delete(�
�r�cKstdi|��}tdi|��}d}zq|r�t|�td�kr6|sd}|s#d}|s'd}t|||fi|��}t|�WSd}	i}
|r@||
d<|rF||
d<|rL||
d<t|�td�kr`|rZ||
d	<|r`||
d
<|sp|sp|sp|sp|spddd�WSt|	|
|d
|d�}|dWSt�ty�|YSw)a�
    Checks if at least one host that matches the given filter criteria exists.

    .. versionadded:: 2016.3.0

    :param host: technical name of the host
    :param hostids: Hosts (hostids) to delete.
    :param name: visible name of the host
    :param node: name of the node the hosts must belong to (zabbix API < 2.4)
    :param nodeids: IDs of the node the hosts must belong to (zabbix API < 2.4)
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the deleted hosts, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_exists 'Zabbix server'
    Fr�Nzhost.existsrrrRr�r�r�z�Please submit hostid, host, name, node or nodeids parameter tocheck if at least one host that matches the given filter criteria exists.r�rIr8rJr )r\r�r�host_getr�rMr.)rrrRr�r�r�r�r�rKr6r7r r r!�host_existsLsJ
�
�r�cKs�td
i|��}d}zO|rWd}did�}|s|s|sWdS|r&|d�d|�|r.|�d|�|r8|d�d|�t|fi|��}t|||d	|d
�}|drT|dWSdWSt�tyc|YSw)a�
    .. versionadded:: 2016.3.0

    Retrieve hosts according to the given parameters

    .. note::
        This function accepts all optional host.get parameters: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/host/get

    :param host: technical name of the host
    :param name: visible name of the host
    :param hostids: ids of the hosts
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)


    :return: Array with convenient hosts details, False if no host found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_get 'Zabbix server'
    F�host.getr�r�rorRr�rrIr8rJNr �r\r^r`rMr.)rrRr�r�r�rKr6r7r r r!r��s*
�r�cKs�td
i|��}d}z4|r<d}d|i}d|vr t|�d�d�|d<t|fddi|��}t|||d|d	�}|d
dWSt�tyH|YSw)a�
    .. versionadded:: 2016.3.0

    Update existing hosts

    .. note::
        This function accepts all standard host and host.update properties:
        keyword argument names differ depending on your zabbix version, see the
        documentation for `host objects`_ and the documentation for `updating
        hosts`_.

        .. _`host objects`: https://www.zabbix.com/documentation/2.4/manual/api/reference/host/object#host
        .. _`updating hosts`: https://www.zabbix.com/documentation/2.4/manual/api/reference/host/update

    :param hostid: ID of the host to update
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)
    :param visible_name: string with visible name of the host, use
        'visible_name' instead of 'name' parameter to not mess with value
        supplied from Salt sls file.

    :return: ID of the updated host.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_update 10084 name='Zabbix server2'
    F�host.updaterr�rr_TrIr8rJr�Nr )r\rer[r`rMr.)rr�r�rKr6r7r r r!�host_update�s"
��r�cKs�tdi|��}d}z=|rEd}ddi}|r|�d|�t|fi|��}t|||d|d�}|drB|dd	d
rB|dd	d
WSdWSt�tyQ|YSw)
a'
    Retrieve host inventory according to the given parameters.
    See: https://www.zabbix.com/documentation/2.4/manual/api/reference/host/object#host_inventory

    .. versionadded:: 2019.2.0

    :param hostids: ID of the host to query
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with host inventory fields, populated or not, False if host inventory is disabled or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_inventory_get 101054
    Fr�ZselectInventoryr�r�rIr8rJr�	inventoryNr r�r�r r r!�host_inventory_get�s*�����r�c
Ks�tdi|��}d}zc|rki}d}d}|�d�rd}|�dd�|�dd�}tt|fi|���}|D]}	|�|	d�q1|rB|�d|�|rSd	|d<t|||d
|d�}||d<||d<t|||d
|d�}|d
WSt�tyw|YSw)a�
    Update host inventory items
    NOTE: This function accepts all standard host: keyword argument names for inventory
    see: https://www.zabbix.com/documentation/2.4/manual/api/reference/host/object#host_inventory

    .. versionadded:: 2019.2.0

    :param hostid: ID of the host to update
    :param clear_old: Set to True in order to remove all existing inventory items before setting the specified items
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: ID of the updated host, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_inventory_set 101054 asset_tag=jml3322 type=vm clear_old=True
    Fr��	clear_oldTN�inventory_mode�0rz-1rIr8r�rJr )r\rGr[r@r`r^rMr.)
rr�r�rKr7r�r6r�Zinventory_paramsrSr r r!�host_inventory_set#s6

�r�cKr�)
a-
    Retrieve all hosts.

    .. versionadded:: 2016.3.0

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with details about hosts, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.host_list
    Fr�rcr�rIr8rJNr r�r�r r r!�	host_list]r�r�cKr�)
ab
    .. versionadded:: 2016.3.0

    Create a host group

    .. note::
        This function accepts all standard host group properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/hostgroup/object#host_group

    :param name: name of the host group
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: ID of the created host group.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostgroup_create MyNewGroup
    Fzhostgroup.createrRrIr8rJ�groupidsNr r�r�r r r!�hostgroup_creater�r�cKr�)	as
    Delete the host group.

    .. versionadded:: 2016.3.0

    :param hostgroupids: IDs of the host groups to delete
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: ID of the deleted host groups, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostgroup_delete 23
    Fzhostgroup.deleterIr8rJr�Nr r�)Zhostgroupidsr�r�rKr6r7r r r!�hostgroup_delete�r�r�c
Kstdi|��}tdi|��}d}zd|rst|�td�kr1|sd}|s#d}t||fi|��}t|�WSi}d}	|r;||d<|rA||d<t|�td�krU|rO||d<|rU||d	<|sc|sc|sc|scdd
d�WSt|	||d|d
�}|dWSt�ty|YSw)a�
    Checks if at least one host group that matches the given filter criteria exists.

    .. versionadded:: 2016.3.0

    :param name: names of the host groups
    :param groupid: host group IDs
    :param node: name of the node the host groups must belong to (zabbix API < 2.4)
    :param nodeids: IDs of the nodes the host groups must belong to (zabbix API < 2.4)
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: True if at least one host group exists, False if not or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostgroup_exists MyNewGroup
    Fr�Nzhostgroup.existsrrRr�r�r�z�Please submit groupid, name, node or nodeids parameter tocheck if at least one host group that matches the given filter criteria exists.r�rIr8rJr )r\r�r�
hostgroup_getr�rMr.)
rRrr�r�r�r�r�rKr7r6r r r!�hostgroup_exists�sB
�
�r�c	Ks�td
i|��}d}zN|rVd}ddi}|s|s|sWdS|r'd|i}|�d|�|r/|�d|�|r7|�d|�t|fi|��}t|||d	|d
�}|drS|dWSdWSt�tyb|YSw)a�
    .. versionadded:: 2016.3.0

    Retrieve host groups according to the given parameters

    .. note::
        This function accepts all standard hostgroup.get properities: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.2/manual/api/reference/hostgroup/get

    :param name: names of the host groups
    :param groupid: host group IDs
    :param node: name of the node the host groups must belong to
    :param nodeids: IDs of the nodes the host groups must belong to
    :param hostids: return only host groups that contain the given hosts

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with host groups details, False if no convenient host group found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostgroup_get MyNewGroup
    F�
hostgroup.getrcr�rRror�r�rIr8rJNr r�)	rRr�r�r�r�rKr6r7Z	name_dictr r r!r�s,�r�cKs~td
i|��}d}z*|r2d}d|i}|r||d<t|fi|��}t|||d|d�}|ddWSt�ty>|YSw)a�
    .. versionadded:: 2016.3.0

    Update existing hosts group

    .. note::
        This function accepts all standard host group properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/hostgroup/object#host_group

    :param groupid: ID of the host group to update
    :param name: name of the host group
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of updated host groups.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostgroup_update 24 name='Renamed Name'
    Fzhostgroup.updaterrRrIr8rJr�Nr r�)rrRr�r�rKr6r7r r r!�hostgroup_updateEs�r�cKr�)
a>
    Retrieve all host groups.

    .. versionadded:: 2016.3.0

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with details about host groups, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostgroup_list
    Fr�rcr�rIr8rJNr r�r�r r r!�hostgroup_listpr�r�cKs�td
i|��}d}z1|r9d}ddi}|r|�d|�t|fi|��}t|||d|d�}|dr6|dWSdWSt�tyE|YSw)a�
    .. versionadded:: 2016.3.0

    Retrieve host groups according to the given parameters

    .. note::
        This function accepts all standard hostinterface.get properities:
        keyword argument names differ depending on your zabbix version, see
        here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/hostinterface/get

    :param hostids: Return only host interfaces used by the given hosts.

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)

    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)

    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: Array with host interfaces details, False if no convenient host interfaces found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostinterface_get 101054
    Fzhostinterface.getrcr�r�rIr8rJNr r�r�r r r!�hostinterface_get�s�r�r�r�cKs�td	i|��}d}	|st|}z*|r8d}
|||||||d�}t|fi|��}t|
||d|d�}	|	ddWSt�tyD|	YSw)
a�
    .. versionadded:: 2016.3.0

    Create new host interface

    .. note::
        This function accepts all standard host group interface: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/3.0/manual/api/reference/hostinterface/object

    :param hostid: ID of the host the interface belongs to

    :param ip_: IP address used by the interface

    :param dns: DNS name used by the interface

    :param main: whether the interface is used as default on the host (0 - not default, 1 - default)

    :param port: port number used by the interface

    :param type: Interface type (1 - agent; 2 - SNMP; 3 - IPMI; 4 - JMX)

    :param useip: Whether the connection should be made via IP (0 - connect
        using host DNS name; 1 - connect using host IP address for this host
        interface)

    :param _connection_user: Optional - zabbix user (can also be set in opts or
        pillar, see module's docstring)

    :param _connection_password: Optional - zabbix password (can also be set in
        opts or pillar, see module's docstring)

    :param _connection_url: Optional - url of zabbix frontend (can also be set
        in opts, pillar, see module's docstring)

    :return: ID of the created host interface, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostinterface_create 10105 192.193.194.197
    Fzhostinterface.create)r�ip�dns�main�portry�useiprIr8rJ�interfaceidsNr )r\�INTERFACE_DEFAULT_PORTSr`rMr.)rZip_r�r�Zif_typer�r�r�r�rKr6r7r r r!�hostinterface_create�s,/�	�r�cK�ptdi|��}d}z#|r+d}t|t�r|}n|g}t|||d|d�}|ddWSt�ty7|YSw)	az
    Delete host interface

    .. versionadded:: 2016.3.0

    :param interfaceids: IDs of the host interfaces to delete
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: ID of deleted host interfaces, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostinterface_delete 50
    Fzhostinterface.deleterIr8rJr�Nr r�)r�r�r�rKr6r7r r r!�hostinterface_delete
s
�r�cKr�)
a�
    .. versionadded:: 2016.3.0

    Update host interface

    .. note::
        This function accepts all standard hostinterface: keyword argument
        names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/2.4/manual/api/reference/hostinterface/object#host_interface

    :param interfaceid: ID of the hostinterface to update

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)

    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)

    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: ID of the updated host interface, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.hostinterface_update 6 ip_=0.0.0.2
    Fzhostinterface.updaterrIr8rJr�Nr r�)rr�r�rKr6r7r r r!�hostinterface_update.s�r�cKs,tdi|��}d}z�|r�d}	did�}
|r>t|t�r&dttt|���d}|�d�s6|�d�s6d|d}|
d�d|�|rG|
�d	|�n|rO|
�d
|�|rX|
�d|�n
|rbd}|
�d
|�|rjt	|
dd�}
t	|
fi|��}
t
|	|
|d|d�}|dr�|dWSdWSt�ty�|YSw)a
    Retrieve user macros according to the given parameters.

    Args:
        macro:          name of the usermacro
        hostids:        Return macros for the given hostids
        templateids:    Return macros for the given templateids
        hostmacroids:   Return macros with the given hostmacroids
        globalmacroids: Return macros with the given globalmacroids (implies globalmacro=True)
        globalmacro:    if True, returns only global macros


        optional connection_args:
                _connection_user: zabbix user (can also be set in opts or pillar, see module's docstring)
                _connection_password: zabbix password (can also be set in opts or pillar, see module's docstring)
                _connection_url: url of zabbix frontend (can also be set in opts or pillar, see module's docstring)

    Returns:
        Array with usermacro details, False if no usermacro found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_get macro='{$SNMP_COMMUNITY}'
    Fz
usermacro.getr�r��{�}ro�macror��templateids�hostmacroidsT�globalmacroids)�globalmacrorIr8rJNr )r\rqr@r,�next�iterrN�endswithr^r`rMr.)r�r�r�r�r�r�r�r�rKr6r7r r r!�
usermacro_getYs<#

�r�cKs�tdi|��}d}zU|r]i}d}|r7t|t�r#dttt|���d}|�d�s3|�d�s3d|d}||d<||d<||d<t|fdd	i|��}t	|||d
|d�}|dd
dWSt
�t
yi|YSw)a�
    Create new host usermacro.

    :param macro: name of the host usermacro
    :param value: value of the host usermacro
    :param hostid: hostid or templateid
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: ID of the created host usermacro.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_create '{$SNMP_COMMUNITY}' 'public' 1
    Fzusermacro.creater�r�r��valuerr_TrIr8rJr�rNr �r\rqr@r,r�r�rNr�r`rMr.)r�r�rr�r�rKr7r6r r r!�usermacro_create�s*
�r�cKs�tdi|��}d}zQ|rYi}d}|r7t|t�r#dttt|���d}|�d�s3|�d�s3d|d}||d<||d<t|fddi|��}t	|||d	|d
�}|ddd
WSt
�t
ye|YSw)a�
    Create new global usermacro.

    :param macro: name of the global usermacro
    :param value: value of the global usermacro
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: ID of the created global usermacro.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_createglobal '{$SNMP_COMMUNITY}' 'public'
    Fzusermacro.createglobalr�r�r�r�r_TrIr8rJr�rNr r�)r�r�r�r�rKr7r6r r r!�usermacro_createglobal�s(
�r�cKr�)	aA
    Delete host usermacros.

    :param macroids: macroids of the host usermacros

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: IDs of the deleted host usermacro.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_delete 21
    Fzusermacro.deleterIr8rJr�Nr r��Zmacroidsr�r�rKr6r7r r r!�usermacro_delete��
�r�cKr�)	aM
    Delete global usermacros.

    :param macroids: macroids of the global usermacros

    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: IDs of the deleted global usermacro.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_deleteglobal 21
    Fzusermacro.deleteglobalrIr8rJr�Nr r�r�r r r!�usermacro_deleteglobal	r�r�cK�td
i|��}d}z.|r6i}d}||d<||d<t|fddi|��}t|||d|d�}|d	d
dWSt�tyB|YSw)a|
    Update existing host usermacro.

    :param hostmacroid: id of the host usermacro
    :param value: new value of the host usermacro
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: ID of the update host usermacro.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_update 1 'public'
    Fzusermacro.update�hostmacroidr�r_TrIr8rJr�rNr r�)r�r�r�r�rKr7r6r r r!�usermacro_update7	��r�cKr�)a�
    Update existing global usermacro.

    :param globalmacroid: id of the host usermacro
    :param value: new value of the host usermacro
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: ID of the update global usermacro.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usermacro_updateglobal 1 'public'
    Fzusermacro.updateglobalrr�r_TrIr8rJr�rNr r�)rr�r�r�rKr7r6r r r!�usermacro_updateglobalZ	r�rcKs�tdi|��}tdi|��}d}zM|r\d}did�}|r5t|�td�kr-|d�d|�n|d�d|�|r=|�d	|�t|fi|��}t|||d
|d�}|drY|dWSdWSt�tyh|YSw)a�
    Retrieve mediatypes according to the given parameters.

    Args:
        name:         Name or description of the mediatype
        mediatypeids: ids of the mediatypes

        optional connection_args:
                _connection_user: zabbix user (can also be set in opts or pillar, see module's docstring)
                _connection_password: zabbix password (can also be set in opts or pillar, see module's docstring)
                _connection_url: url of zabbix frontend (can also be set in opts or pillar, see module's docstring)

                all optional mediatype.get parameters: keyword argument names depends on your zabbix version, see:

                https://www.zabbix.com/documentation/2.2/manual/api/reference/mediatype/get

    Returns:
        Array with mediatype details, False if no mediatype found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.mediatype_get name='Email'
        salt '*' zabbix.mediatype_get mediatypeids="['1', '2', '3']"
    Fz
mediatype.getr�r��4.4rorR�description�mediatypeidsrIr8rJNr r�)rRrr�r�r�rKr6r7r r r!�
mediatype_get}	s(
�rc	Ks�td
i|��}td
i|��}d}z:|rId}t|�td�kr$d|i}d}nd|i}d}||d<t||fi|��}t|||d|d	�}|d
dWSt�tyU|YSw)aR
    Create new mediatype

    .. note::
        This function accepts all standard mediatype properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/3.0/manual/api/reference/mediatype/object

    :param mediatype: media type - 0: email, 1: script, 2: sms, 3: Jabber, 100: Ez Texting
    :param exec_path: exec path - Required for script and Ez Texting types, see Zabbix API docs
    :param gsm_modem: exec path - Required for sms type, see Zabbix API docs
    :param smtp_email: email address from which notifications will be sent, required for email type
    :param smtp_helo: SMTP HELO, required for email type
    :param smtp_server: SMTP server, required for email type
    :param status: whether the media type is enabled - 0: enabled, 1: disabled
    :param username: authentication user, required for Jabber and Ez Texting types
    :param passwd: authentication password, required for Jabber and Ez Texting types
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    return: ID of the created mediatype.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.mediatype_create 'Email' 0 smtp_email='noreply@example.com'
        smtp_server='mailserver.example.com' smtp_helo='zabbix.example.com'
    Fzmediatype.createrrRrTryrIr8rJrNr )r\r�rr`rMr.)	rRrr�r�r�rKr6r7r_r r r!�mediatype_create�	s& �rcKr�)	aF
    Delete mediatype


    :param interfaceids: IDs of the mediatypes to delete
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: ID of deleted mediatype, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.mediatype_delete 3
    Fzmediatype.deleterIr8rJrNr r�)rr�r�rKr6r7r r r!�mediatype_delete�	r�rcKs�tdi|��}d}z0|r8d}d|i}|r||d<|r||d<t|fi|��}t|||d|d�}|dd	WSt�tyD|YSw)ah
    Update existing mediatype

    .. note::
        This function accepts all standard mediatype properties: keyword
        argument names differ depending on your zabbix version, see here__.

        .. __: https://www.zabbix.com/documentation/3.0/manual/api/reference/mediatype/object

    :param mediatypeid: ID of the mediatype to update
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    :return: IDs of the updated mediatypes, False on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.usergroup_update 8 name="Email update"
    Fzmediatype.updaterrryrIr8rJrNr r�)rrRrr�r�rKr6r7r r r!�mediatype_update

s"�rcKs�td
i|��}d}zF|rNd}did�}|r|d�d|�|r'|d�d|�|r/|�d|�t|fi|��}t|||d	|d
�}|drK|dWSdWSt�tyZ|YSw)a#
    Retrieve templates according to the given parameters.

    Args:
        host: technical name of the template
        name: visible name of the template
        hostids: ids of the templates

        optional connection_args:
                _connection_user: zabbix user (can also be set in opts or pillar, see module's docstring)
                _connection_password: zabbix password (can also be set in opts or pillar, see module's docstring)
                _connection_url: url of zabbix frontend (can also be set in opts or pillar, see module's docstring)

                all optional template.get parameters: keyword argument names depends on your zabbix version, see:

                https://www.zabbix.com/documentation/2.4/manual/api/reference/template/get

    Returns:
        Array with convenient template details, False if no template found or on failure.

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.template_get name='Template OS Linux'
        salt '*' zabbix.template_get templateids="['10050', '10001']"
    Fztemplate.getr�r�rorRrr�rIr8rJNr r�)rRrr�r�r�rKr6r7r r r!�template_get4
s&
�rcKs�tdi|��}d}z/|r7t|fi|��}t|||d|d�}t|dt�r+|dWS|dr4|dWSdWSt�tyC|YSw)a�
    Send Zabbix API call

    Args:
        method: actual operation to perform via the API
        params: parameters required for specific method

        optional connection_args:
                _connection_user: zabbix user (can also be set in opts or pillar, see module's docstring)
                _connection_password: zabbix password (can also be set in opts or pillar, see module's docstring)
                _connection_url: url of zabbix frontend (can also be set in opts or pillar, see module's docstring)

                all optional template.get parameters: keyword argument names depends on your zabbix version, see:

                https://www.zabbix.com/documentation/2.4/manual/api/reference/

    Returns:
        Response from Zabbix API

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.run_query proxy.create '{"host": "zabbixproxy.domain.com", "status": "5"}'
    FrIr8rJNr )r\r`rMrqr�r.)r6r7r�r�rKr r r!r�e
s
�r��xmlcKsDtdi|��}|dur
i}dddd�dddd�ddiddd�ddd�dddd�ddd�ddd�ddiddd�dddd�dddd�ddd�d�
}t|�td�krWdddd�|d	<t|�td
�krgddd�|d<ndddd�|d<t|�}|r�|D]}||vr�||�||�qw||||<qwd
|vr�tjj��}td||�}	|	r�t	j
�|	�dkr�|ddd�Sn|}	t	j
�|	�s�|ddd�Stjjj
|	dd��}
|
��}Wd�n1s�wYd
|vr�tjj�|	�|||d�}t�dt|d��ztd|fi|��|ddd�WSt�y!}
z|dt|
�d�WYd}
~
Sd}
~
ww)a�
    .. versionadded:: 2017.7.0

    Imports Zabbix configuration specified in file to Zabbix server.

    :param config_file: File with Zabbix config (local or remote)
    :param rules: Optional - Rules that have to be different from default (defaults are the same as in Zabbix web UI.)
    :param file_format: Config file format (default: xml)
    :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)
    :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)
    :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)

    CLI Example:

    .. code-block:: bash

        salt '*' zabbix.configuration_import salt://zabbix/config/zabbix_templates.xml         "{'screens': {'createMissing': True, 'updateExisting': True}}"
    NTF)�
createMissing�updateExisting�
deleteMissingr
)r
r)
ZdiscoveryRulesZgraphsr��hostsZimagesrv�mapsZscreensZtemplateLinkageZ	templatesZtemplateScreensZtriggersZ	valueMapsz3.2Z	httptestsr�)r
rZapplicationszsalt://zcp.get_filerzFailed to fetch config file.)rRrJrAzInvalid file path.�r)�mode)rF�rulesrbzCONFIGURATION IMPORT: rules: %srzconfiguration.importz=Zabbix API "configuration.import" method called successfully.r )r�rr@rtr&r'�filesZmkstemprQ�os�path�getsize�isfileZfopen�readZsafe_rmrDrEr,r�r)Zconfig_filerZfile_formatr�r�Z
default_rulesZ	new_rulesZruleZtmpfile�cfileZfp_r	r7�excr r r!�configuration_import�
s�������#
�
����
����rrZ)F)NrR)NN)NNN)NNNNN)NNNN)r�r�r�r�N)NNNNNF)FF)Nr	)P�__doc__�loggingrr$�urllib.errorr*Zsalt.utils.datar&Zsalt.utils.filesZsalt.utils.httpZsalt.utils.jsonZsalt.exceptionsrZsalt.utils.versionsr�	getLogger�__name__rDr�rsrfrr"r2rMr\r`rergrirzrur�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrr�rr r r r!�<module>s>
��������	�
���
������������������� �!�"�#�-
=
A!

9
V" :%
);
C4G/ (
"
=8("7%
�
I32):"(%
�
D
6+"/
�J$,
�D+)###
#37
#
*1)