Python module to work with Zabbix monitoring systems through API and sender functionality
npx @tessl/cli install tessl/pypi-py-zabbix@1.1.0A comprehensive Python module to work with Zabbix monitoring systems through both API and sender functionality. It provides a ZabbixAPI class for authentication and performing various operations like host management, data retrieval, and configuration changes through Zabbix's JSON-RPC API, plus ZabbixSender functionality for pushing metrics data to Zabbix trappers.
pip install py-zabbixfrom pyzabbix import ZabbixAPI, ZabbixAPIException, ZabbixMetric, ZabbixSender, ZabbixResponse, ssl_context_compatSpecific module imports:
from pyzabbix.api import ZabbixAPI, ZabbixAPIException, ssl_context_compat
from pyzabbix.sender import ZabbixMetric, ZabbixSender, ZabbixResponsefrom pyzabbix import ZabbixAPI
# Create ZabbixAPI instance and authenticate
zapi = ZabbixAPI(url='https://zabbix.example.com', user='Admin', password='zabbix')
# Get all monitored hosts
hosts = zapi.host.get(monitored_hosts=1, output='extend')
# Get disabled hosts using raw request
disabled_hosts = zapi.do_request('host.get', {
'filter': {'status': 1},
'output': 'extend'
})
# Logout when done
zapi.user.logout()from pyzabbix import ZabbixMetric, ZabbixSender
# Create metrics to send
metrics = [
ZabbixMetric('hostname1', 'cpu_usage', 85.2),
ZabbixMetric('hostname1', 'memory_usage', '60%'),
ZabbixMetric('hostname1', 'disk_free', 1024, 1411598020), # with timestamp
]
# Send metrics to Zabbix server
sender = ZabbixSender(zabbix_server='192.168.1.100')
result = sender.send(metrics)py-zabbix provides two main functional areas:
The library supports Python 2 and 3, includes automatic password masking for security, context manager support for resource cleanup, and built-in logging capabilities.
Complete interface to Zabbix's JSON-RPC API with authentication management, dynamic method dispatch, and comprehensive error handling for all Zabbix monitoring operations.
class ZabbixAPI:
def __init__(self, url=None, use_authenticate=False, use_basic_auth=False, user=None, password=None): ...
def api_version(self): ...
def do_request(self, method, params=None): ...
def get_id(self, item_type, item=None, with_id=False, hostid=None, **args): ...Send monitoring metrics to Zabbix server via trapper protocol with batch processing, configurable connection parameters, and comprehensive response handling.
class ZabbixSender:
def __init__(self, zabbix_server='127.0.0.1', zabbix_port=10051, use_config=None, chunk_size=250, socket_wrapper=None, timeout=10): ...
def send(self, metrics): ...
class ZabbixMetric:
def __init__(self, host, key, value, clock=None): ...
class ZabbixResponse:
def parse(self, response): ...class ZabbixAPIException(Exception):
"""Custom exception for Zabbix API errors"""
def __init__(self, error): ...The ZabbixAPIException provides detailed error information including error code, message, and sanitized request data for debugging.