Zabbix Utils

2.0.4 · active · verified Thu Apr 16

Zabbix Utils is an official Python library designed for seamless interaction with Zabbix components, including the Zabbix API, Zabbix sender protocol, and Zabbix get protocol. Currently at version 2.0.4, the library is actively maintained with frequent releases to ensure compatibility with the latest Zabbix server versions and to introduce new features and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to the Zabbix API using `ZabbixAPI` class, authenticate with username and password (or token), and perform a basic API call to retrieve a list of hosts. It prioritizes environment variables for credentials, falling back to defaults if not set. Remember to replace placeholder values with your actual Zabbix server URL and credentials. For asynchronous operations, use `AsyncZabbixAPI` and `await` with an `async def` function.

import os
from zabbix_utils import ZabbixAPI

# Configure Zabbix API access using environment variables
ZABBIX_URL = os.environ.get('ZABBIX_URL', 'http://127.0.0.1/zabbix')
ZABBIX_USER = os.environ.get('ZABBIX_USER', 'Admin')
ZABBIX_PASSWORD = os.environ.get('ZABBIX_PASSWORD', 'zabbix')

# Initialize ZabbixAPI instance and log in
try:
    api = ZabbixAPI(url=ZABBIX_URL)
    api.login(user=ZABBIX_USER, password=ZABBIX_PASSWORD)
    print(f"Successfully logged into Zabbix API at {ZABBIX_URL}")

    # Example: Fetch all hosts
    hosts = api.host.get(output=['hostid', 'name'])
    if hosts:
        print("\nKnown hosts:")
        for host in hosts:
            print(f"  ID: {host['hostid']}, Name: {host['name']}")
    else:
        print("No hosts found.")

except Exception as e:
    print(f"Error connecting to Zabbix API: {e}")
finally:
    # Always log out to close the session if logged in
    if 'api' in locals() and api.is_logged_in:
        api.logout()
        print("Logged out from Zabbix API.")

view raw JSON →