[comment]: # translation:outdated

[comment]: # aside:4

[comment]: # ({new-ebcec779})
# Collect data from Zabbix agent

[comment]: # ({/new-ebcec779})

[comment]: # ({new-02f6ab0d})
#### Overview

[zabbix\_utils](https://github.com/zabbix/python-zabbix-utils/blob/main/README.md) lets you collect data from Zabbix agent (similarly to [Zabbix get](/manual/concepts/get)).

Data can be collected in synchronous or asynchronous mode:

-   In synchronous mode, your Python script requests and waits for data before continuing, which is suitable for simple, sequential, and predictable operations.
-   In asynchronous mode, the script requests data without waiting for each response, allowing other operations to proceed in parallel; this is more efficient for slow requests or large batches of data.

The examples on this page focus on synchronous mode, though [asynchronous mode](#asynchronous-mode) follows similar patterns.
Additional examples are available in the [zabbix_utils](https://github.com/zabbix/python-zabbix-utils/tree/main/examples) GitHub repository.

[comment]: # ({/new-02f6ab0d})

[comment]: # ({new-8b91e8b3})
#### Import

To use zabbix\_utils for collecting item values, import the `Getter` class in your Python script:

```python
from zabbix_utils import Getter
```

[comment]: # ({/new-8b91e8b3})

[comment]: # ({new-7e0dc385})
#### Request data

To request an item value:

1. Create a `Getter` instance, specifying the IP address and port of your Zabbix agent.
2. Call the `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:

```python
agent = Getter(host='192.0.2.0', port=10050)
response = agent.get('system.uname')
```

[comment]: # ({/new-7e0dc385})

[comment]: # ({new-b5e0ad53})
##### Using non-default IP

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:

```python
agent = Getter(
    host='192.0.2.0',
    port=10050,
    source_ip='10.10.7.1'
)
```

[comment]: # ({/new-b5e0ad53})

[comment]: # ({new-6bc440d9})
##### Using timeout

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:

```python
agent = Getter(
    host='192.0.2.0',
    port=10050,
    timeout=30
)
```

[comment]: # ({/new-6bc440d9})

[comment]: # ({new-6a0fc829})
##### Using encryption

The `Getter` does not include built-in encryption support, but you can provide it by creating a wrapper using third-party libraries:

```python
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
)
```

[comment]: # ({/new-6a0fc829})

[comment]: # ({new-9556e19b})
##### Response

The response from Zabbix agent is processed by the library and returned as an `AgentResponse` object:

```python
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)
# None
```

[comment]: # ({/new-9556e19b})

[comment]: # ({new-abc52038})
##### Asynchronous mode

Asynchronous 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:

-   Import Python's `asyncio` module (you must first [install](/devel/python/install) the required dependencies).
-   Import `AsyncGetter` instead of `Getter`.
-   Write your code inside an `async` function.
-   Use `await` when calling the `get()` method.

For example, to collect a single value using asynchronous mode:

```python
# 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())
```

[comment]: # ({/new-abc52038})
