zabbix_utils lets you collect data from Zabbix agent (similarly to Zabbix get).
Data can be collected in synchronous or asynchronous mode:
The examples on this page focus on synchronous mode, though asynchronous mode follows similar patterns. Additional examples are available in the zabbix_utils GitHub repository.
To use zabbix_utils for collecting item values, import the Getter class in your Python script:
To request an item value:
Getter instance, specifying the IP address and port of your Zabbix agent.get() method on the Getter instance, specifying the key of the item you want to retrieve.For example, to request data for the system.uname item:
If the server running your script has multiple IP addresses, you can specify a source_ip for the Getter to use when connecting to Zabbix agent:
You can set a response timeout for the Getter to control how long your script should wait for a response from Zabbix agent before giving up:
The Getter does not include built-in encryption support, but you can provide it by creating a wrapper using third-party libraries:
def psk_wrapper(sock, tls):
# ...
# Implementation of TLS PSK wrapper for the socket
# ...
agent = Getter(
host='192.0.2.0',
port=10050,
socket_wrapper=psk_wrapper
)The response from Zabbix agent is processed by the library and returned as an AgentResponse object:
print(response)
# {
# "error": null,
# "raw": "Linux zabbix_server 5.15.0-3.60.5.1.el9uek.x86_64",
# "value": "Linux zabbix_server 5.15.0-3.60.5.1.el9uek.x86_64"
# }
print(response.value)
# Linux zabbix_server 5.15.0-3.60.5.1.el9uek.x86_64
print(response.error)
# NoneAsynchronous mode lets your script collect values without waiting for each one to arrive. This can make your script more efficient when it needs to collect many values or when some values take a long time to be collected.
When using asynchronous mode, there are important differences compared to synchronous mode:
asyncio module (you must first install the required dependencies).AsyncGetter instead of Getter.async function.await when calling the get() method.For example, to collect a single value using asynchronous mode:
# 1. Import asyncio for asynchronous mode, and AsyncGetter from zabbix_utils:
import asyncio
from zabbix_utils import AsyncGetter
# 2. Define the main async function where all data requests will be executed:
async def main():
agent = AsyncGetter(host='192.0.2.0', port=10050)
# 3. Fetch the system.uname value from Zabbix agent (must await):
response = await agent.get('system.uname')
# 4. Print the value returned by Zabbix agent:
print(response.value)
# 5. Run the async main() function using asyncio's event loop:
asyncio.run(main())