Install PyZabbix via pip
masterInstall the pyzabbix package using pip to start working with the Zabbix API in Python.
$ pip install pyzabbixrepository·master·Indexed 19 days ago
https://github.com/lukecyca/pyzabbixA Python module for interfacing with the Zabbix API to programmatically manage Zabbix configurations, hosts, and data. It supports authentication via username/password or API tokens (for Zabbix >= 5.4), custom HTTP request parameters through the requests library, and debug logging for JSON-RPC interactions.
Install the pyzabbix package using pip to start working with the Zabbix API in Python.
$ pip install pyzabbixPyZabbix uses the requests library. You can modify the underlying requests.Session object via zapi.session to configure headers, authentication, and SSL verification. You can also set a custom timeout directly on the zapi object.
from pyzabbix import ZabbixAPI
zapi = ZabbixAPI("http://zabbixserver.example.com")
# Enable HTTP auth
zapi.session.auth = ("http user", "http password")
# Disable SSL certificate verification
zapi.session.verify = False
# Specify a timeout (in seconds)
zapi.timeout = 5.1
# Login (if HTTP Auth is enabled, only the username is needed)
zapi.login("http user", "http password")To use PyZabbix, import ZabbixAPI, instantiate it with your Zabbix server URL, and authenticate using either a username/password or an API token (for Zabbix >= 5.4).
from pyzabbix import ZabbixAPI
zapi = ZabbixAPI("http://zabbixserver.example.com")
zapi.login("zabbix user", "zabbix pass")
# For Zabbix >= 5.4, use an API token instead:
# zapi.login(api_token='xxxxx')
print("Connected to Zabbix API Version %s" % zapi.api_version())
for h in zapi.host.get(output="extend"):
print(h['hostid'])To debug API interactions, configure the pyzabbix logger to output to a stream (like sys.stdout) at the DEBUG level. This will show the JSON-RPC requests being sent and the responses received.
import sys
import logging
from pyzabbix import ZabbixAPI
stream = logging.StreamHandler(sys.stdout)
stream.setLevel(logging.DEBUG)
log = logging.getLogger('pyzabbix')
log.addHandler(stream)
log.setLevel(logging.DEBUG)
zapi = ZabbixAPI("http://zabbixserver.example.com")
zapi.login('admin','password')The docker-compose.yml file provides a development environment consisting of a PostgreSQL database, a Zabbix server, and a Zabbix web interface.
ZABBIX_VERSION: Specifies the Zabbix version for both the server and web images. Defaults to 6.2 if not provided.postgres:13-alpine. Requires POSTGRES_USER and POSTGRES_PASSWORD (defaults to zabbix/zabbix).zabbix/zabbix-server-pgsql:alpine-${ZABBIX_VERSION}-latest. Maps port 10051 to 10051. Requires POSTGRES_USER and POSTGRES_PASSWORD to connect to the database. Includes ZBX_CACHEUPDATEFREQUENCY: 1.zabbix/zabbix-web-nginx-pgsql:alpine-${ZABBIX_VERSION}-latest. Maps port 8888 on the host to 8080 in the container. Requires POSTGRES_USER and POSTGRES_PASSWORD.version: "3.8"
services:
postgres-server:
image: postgres:13-alpine
environment:
POSTGRES_USER: zabbix
POSTGRES_PASSWORD: zabbix
zabbix-server:
image: zabbix/zabbix-server-pgsql:alpine-${ZABBIX_VERSION:-6.2}-latest
ports:
- 10051:10051
environment:
POSTGRES_USER: zabbix
POSTGRES_PASSWORD: zabbix
ZBX_CACHEUPDATEFREQUENCY: 1
depends_on:
- postgres-server
zabbix-web:
image: zabbix/zabbix-web-nginx-pgsql:alpine-${ZABBIX_VERSION:-6.2}-latest
ports:
- 8888:8080
environment:
POSTGRES_USER: zabbix
POSTGRES_PASSWORD: zabbix
depends_on:
- postgres-server
- zabbix-serverIf you are using Zabbix version 5.4 or newer, you can authenticate using an API token instead of a username and password by passing the api_token keyword argument to the login method.
zapi.login(api_token='xxxxx')