Welcome to py-zabbix’s documentation!¶
Zabbix module for Python¶
That set of modules allow to work with Zabbix in Python.
- Currently it contains:
- pyzabbix.api for work with Zabbix API.
- pyzabbix.sender for send metrics to Zabbix.
Note
py-zabbix do not require addition modules, it use only standard Python modules.
Quickstart guide¶
Examples¶
ZabbixAPI¶
You can make zabbix api call with two ways.
- By With dynamicaly mapping
pyzabbix.api.ZabbixAPI
methods on zabbix api:
result = zapi.host.get(status=1)
- By passing zabbix api function and arguments as parameters:
result = zapi.do_request('host.get', {'status':1})
Get list of not monitored hosts from zabbix¶
from pyzabbix import ZabbixAPI
# Create ZabbixAPI class instance
zapi = ZabbixAPI(url='https://localhost/zabbix/', user='admin', password='zabbix')
# Get all disabled hosts
result = zapi.host.get(status=1)
hostnames = [host['host'] for host in result1]
# Other way to get all disabled hosts
result2 = zapi.do_request('host.get', {'status':1})
hostnames2 = [host['host'] for host in result2['result']]
ZabbixSender¶
Send metric to zabbix¶
metrics= [ZabbixMetric('localhost', 'cpu[usage]', '15.4')]
ZabbixSender(use_config=True).send(metrics)
Usage with zabbix_agentd.conf¶
If your box already have zabbix_agent installed. Py-zabbix can use zabbix server from zabbix_agentd.conf file.
from pyzabbix import ZabbixMetric, ZabbixSender
packet = [
ZabbixMetric('centos', 'test[cpu_usage]', 2),
ZabbixMetric('centos', 'test[system_status]', "OK"),
ZabbixMetric('centos', 'test[disk_io]', '0.1'),
ZabbixMetric('centos', 'test[cpu_usage]', 20, 1411598020),
ZabbixMetric('centos', 'test[cpu_usage]', 30, 1411598120),
ZabbixMetric('centos', 'test[cpu_usage]', 40, 1411598240),
]
sender = ZabbixSender(use_config=True)
sender.send(packet)
Module pyzabbix.api¶
This module provide classes to work with Zabbix API.
-
class
pyzabbix.api.
ZabbixAPI
(url='https://localhost/zabbix', use_authenticate=False, user='admin', password='zabbix')¶ ZabbixAPI class, implement interface to zabbix api.
Parameters: - url (str) – URL to zabbix api. Default: https://localhost/zabbix
- use_authenticate (bool) – Use user.authenticate method if True else user.login.
- user (str) – Zabbix user name. Default: admin.
- password (str) – Zabbix user password. Default zabbix.
>>> from pyzabbix import ZabbixAPI >>> z = ZabbixAPI('https://zabbix.server', user='admin', password='zabbix') >>> # Get API Version >>> z.api_info.version() >>> u'2.2.1' >>> # or >>> z.do_request('apiinfo.version') >>> {u'jsonrpc': u'2.0', u'result': u'2.2.1', u'id': u'1'} >>> # Get all disabled hosts >>> z.host.get(status=1) >>> # or >>> z.do_request('host.getobjects', {'status': 1})
-
api_version
()¶ Return version of server Zabbix API.
Return type: str Returns: Version of server Zabbix API.
-
do_request
(method, params=None)¶ Make request to Zabbix API.
Parameters: - method (str) – ZabbixAPI method, like: apiinfo.version.
- params (str) – ZabbixAPI method arguments.
>>> from pyzabbix import ZabbixAPI >>> z = ZabbixAPI() >>> apiinfo = z.do_request('apiinfo.version')
-
get_id
(item_type, item=None, with_id=False, hostid=None, **args)¶ Return id or ids of zabbix objects.
Parameters: - item_type (str) – Type of zabbix object. (eg host, item etc.)
- item (str) – Name of zabbix object. If it is None, return list of all objects in the scope.
- with_id (bool) – Returned values will be in zabbix json id format. Examlpe: {‘itemid: 128}
- name (bool) – Return name instead of id.
- hostid (int) – Filter objects by specific hostid.
- tempateids – Filter objects which only belong to specific templates by template id.
- app_name (str) – Filter object which only belong to specific application.
Return type: int or list
Returns: Return single id, name or list of values.
Module pyzabbix.sender¶
-
class
pyzabbix.sender.
ZabbixMetric
(host, key, value, clock=None)¶ The
ZabbixMetric
contain one metric for zabbix server.Parameters: - host (str) – Hostname as it displayed in Zabbix.
- key (str) – Key by which you will identify this metric.
- value (str) – Metric value.
- clock (int) – Unix timestamp. Current time will used if not specified.
>>> from pyzabbix import ZabbixMetric >>> ZabbixMetric('localhost', 'cpu[usage]', 20)
-
class
pyzabbix.sender.
ZabbixResponse
¶ The
ZabbixResponse
contains the parsed response from Zabbix.-
parse
(response)¶ Parse zabbix response.
-
-
class
pyzabbix.sender.
ZabbixSender
(zabbix_server='127.0.0.1', zabbix_port=10051, use_config=None, chunk_size=250)¶ The
ZabbixSender
send metrics to Zabbix server.Implementation of zabbix protocol.
Parameters: - zabbix_server (str) – Zabbix server ip address. Default: 127.0.0.1
- zabbix_port (int) – Zabbix server port. Default: 10051
- use_config (str) – Path to zabbix_agentd.conf file to load settings from. If value is True then default config path will used: /etc/zabbix/zabbix_agentd.conf
- chunk_size (int) – Number of metrics send to the server at one time
>>> from pyzabbix import ZabbixMetric, ZabbixSender >>> metrics = [] >>> m = ZabbixMetric('localhost', 'cpu[usage]', 20) >>> metrics.append(m) >>> zbx = ZabbixSender('127.0.0.1') >>> zbx.send(metric)
-
send
(metrics)¶ Send the metrics to zabbix server.
Parameters: metrics (list) – List of zabbix.sender.ZabbixMetric
to send to ZabbixReturn type: pyzabbix.sender.ZabbixResponse
Returns: Parsed response from Zabbix Server