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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.626s · 22MB
glibcpy 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.")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyzabbix'
The pyzabbix library is not installed in the current Python environment.
pyzabbix.ZabbixAPIException: Authentication failed: Invalid username or password.
The provided Zabbix API username or password is incorrect, or the user lacks API access permissions.
fixVerify 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.
fixEnsure 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.
fixConsult 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.