Registry / devops / pyzabbix

pyzabbix

JSON →
library1.3.1pypypi✓ verified 89d ago

PyZabbix provides a Python interface to the Zabbix API, allowing programmatic interaction with Zabbix monitoring systems. It handles API authentication, requests, and response parsing. The current version is 1.3.1, with releases occurring intermittently, often several months apart.

pip install pyzabbix
INSTALL
IMPORT
SIG · PYZABBIX
P
pyzabbix
devopspythonv1.3.1
Install
2.2s avg
Import
597ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.626s · 22MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 2.2s · import 0.567s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ZabbixAPI
✓ from pyzabbix import ZabbixAPI
✗ from zabbix_api import ZabbixAPI
The PyPI package name is `pyzabbix`, not `zabbix_api`.
ZabbixAPIException
✓ from pyzabbix import ZabbixAPIException
Import for handling API-specific errors returned by the Zabbix server.

This quickstart demonstrates how to connect to the Zabbix API, authenticate using either username/password or an API token (if provided via environment variables), retrieve basic Zabbix server information, and list hosts. It includes error handling for API-specific issues and ensures proper session cleanup with `logout()`.

import os from pyzabbix import ZabbixAPI, ZabbixAPIException # Configure Zabbix access via environment variables or replace directly ZABBIX_URL = os.environ.get('ZABBIX_URL', 'http://localhost/zabbix') # e.g., 'http://your-zabbix-server/zabbix' ZABBIX_USER = os.environ.get('ZABBIX_USER', 'Admin') ZABBIX_PASSWORD = os.environ.get('ZABBIX_PASSWORD', 'zabbix') # Default 'zabbix' for Admin user ZABBIX_TOKEN = os.environ.get('ZABBIX_TOKEN', '') # Optional: for API token authentication # Initialize zapi outside try-block for finally access zapi = None try: # Connect to Zabbix API zapi = ZabbixAPI(ZABBIX_URL) if ZABBIX_TOKEN: # Authenticate using API token (requires Zabbix 5.0+ and pyzabbix >= 1.0.0) zapi.login(api_token=ZABBIX_TOKEN) print("Authenticated using API Token.") else: # Authenticate using username and password zapi.login(ZABBIX_USER, ZABBIX_PASSWORD) print(f"Authenticated as user: {ZABBIX_USER}") print(f"Connected to Zabbix API version: {zapi.api_version()}") # Example: Fetch Zabbix server info version_info = zapi.apiinfo.version() print(f"Zabbix Server Version: {version_info}") # Example: Fetch some hosts # 'output' determines which fields are returned, 'selectInterfaces' specifies linked data hosts = zapi.host.get(output='extend', selectInterfaces=['interfaceid', 'ip', 'port']) print(f"Found {len(hosts)} hosts.") if hosts: print(f"First host: {hosts[0]['host']} (IP: {hosts[0]['interfaces'][0]['ip']})") except ZabbixAPIException as e: print(f"Error interacting with Zabbix API: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") finally: # Ensure proper logout if authenticated if zapi and zapi.is_authenticated: zapi.logout() print("Logged out from Zabbix API.")
Debug
Known issues
breakingVersion 1.0.0 introduced significant changes including API token support and automatic API version detection. Code relying on older manual API version specification or lacking token handling may require updates.
fix
Review authentication methods and ensure compatibility with auto-detected API versions. Consider updating authentication to use API tokens if applicable (Zabbix 5.0+).
affects: <1.0.0
gotchaCompatibility with Zabbix server versions is crucial. PyZabbix targets specific Zabbix API versions. Ensure your `pyzabbix` version supports your Zabbix server's API version.
fix
Consult the `pyzabbix` GitHub page or changelog for explicit Zabbix server version compatibility. Upgrade `pyzabbix` if your Zabbix server has been updated to a newer major version.
affects: All versions
gotchaZabbix API errors are encapsulated in `ZabbixAPIException`. Failure to catch this specific exception can lead to unhandled errors, especially when making API calls that might fail (e.g., non-existent IDs, insufficient permissions).
fix
Always wrap `pyzabbix` API calls in `try...except ZabbixAPIException` blocks to gracefully handle Zabbix API errors.
affects: All versions
gotchaAuthentication management requires careful handling. While `pyzabbix` abstracts `login()`, it's vital to explicitly call `zapi.logout()` after operations or use a context manager (if implemented in future versions) to ensure sessions are properly terminated, especially for long-running applications.
fix
Ensure `zapi.logout()` is called in a `finally` block or at the end of your script to prevent stale sessions.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyzabbix'
The pyzabbix library is not installed in the current Python environment.
fix
pip install pyzabbix
pyzabbix.ZabbixAPIException: Authentication failed: Invalid username or password.
The provided Zabbix API username or password is incorrect, or the user lacks API access permissions.
fix
Verify the Zabbix API URL, username, and password, and ensure the user has sufficient permissions for API access.
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
The Zabbix API server is unreachable, not running, or a firewall is blocking the connection from the client machine.
fix
Ensure the Zabbix server is running, the API URL is correct, the port is open, and network firewalls are not obstructing the connection.
pyzabbix.ZabbixAPIException: {'code': -32602, 'message': 'Invalid params.'}
The Zabbix API call was made with incorrect, missing, or improperly formatted parameters, or the method itself is invalid/non-existent.
fix
Consult the Zabbix API documentation for the specific method being called to ensure all required parameters are provided with correct types and values, and the method name is accurate.
Upgrade
Version history
1.3.1latest on PyPI · released Dec 18, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
2
Resources
pyzabbix — pip install pyzabbix · libregistry