To install the library with dependencies for asynchronous work use the following way:
$ pip install zabbix_utils[async]
Installation from Zabbix repository
First of all, you need to install Zabbix repository. Official Zabbix packages for Red Hat Enterprise Linux and derivatives, as well as for Debian and derivatives are available on Zabbix website.
Red Hat Enterprise Linux and derivatives
Install zabbix_utils library from Zabbix repository:
# dnf install python3-zabbix-utils
To install additional dependencies such as aiohttp for asynchronous work use the following way:
$ cd python-zabbix-utils/
$ python3 setup.py install
To install dependencies use one of the ways above.
Documentation
Use cases
To work with Zabbix API
To work with Zabbix API via synchronous I/O you can import and use zabbix_utils library as follows:
from zabbix_utils import ZabbixAPI
api = ZabbixAPI(url="127.0.0.1")
api.login(user="User", password="zabbix")
users = api.user.get(
output=['userid','name']
)
for user in users:
print(user['name'])
api.logout()
To work with Zabbix API via asynchronous I/O you can use the following way:
import asyncio
from zabbix_utils import AsyncZabbixAPI
async def main():
api = AsyncZabbixAPI(url="127.0.0.1")
await api.login(user="User", password="zabbix")
users = await api.user.get(
output=['userid','name']
)
for user in users:
print(user['name'])
await api.logout()
asyncio.run(main())
You can also authenticate using an API token (supported since Zabbix 5.4):
api = ZabbixAPI(url="127.0.0.1")
api.login(token="xxxxxxxx")
api = AsyncZabbixAPI(url="127.0.0.1")
await api.login(token="xxxxxxxx")
When token is used, calling api.logout() is not necessary.
It is possible to specify authentication fields by the following environment variables:
ZABBIX_URL, ZABBIX_TOKEN, ZABBIX_USER, ZABBIX_PASSWORD
You can compare Zabbix API version with strings and numbers, for example:
# Method to get version
ver = api.api_version()
print(type(ver).__name__, ver) # APIVersion 7.0.0
# ZabbixAPI prototype with version
ver = api.version
print(type(ver).__name__, ver) # APIVersion 7.0.0
# Comparing versions
print(ver > 6.0) # True
print(ver != 7.0) # False
print(ver != "7.0.0") # False
# Version additional methods
print(ver.major) # 7.0
print(ver.minor) # 0
print(ver.is_lts()) # True
In case the API object or method name matches one of Python keywords, you can use the suffix _ in their name to execute correctly, for example:
from zabbix_utils import ZabbixAPI
api = ZabbixAPI(url="127.0.0.1")
api.login(token="xxxxxxxx")
template_source = ''
with open('template_example.xml', mode='r', encoding='utf-8') as f:
template_source = f.read()
response = api.configuration.import_(
source=template_source,
format="xml",
rules={...}
)
if response:
print("Template imported successfully")
Since Zabbix 8.0, the Allowed hosts field of trapper items defaults to the {$TRAPPER.ALLOWED_HOSTS} macro, which is 127.0.0.1,::1, so only values sent from the Zabbix server host itself are accepted, and an empty field denies all hosts. To send values from other hosts, add their addresses to the field or to the macro.
If you need to send values to several Zabbix clusters at once, you can do this by passing a list of Zabbix clusters:
If it needed to debug some issue with Zabbix API, sender or get you can enable the output of logging. The zabbix_utils library uses the default python logging module, but it doesn't log by default. You can define logging handler to see records from the library, for example: