Zabbix: Accessing Zabbix using the py-zabbix Python module

The open-source Zabbix monitoring solution has a REST API that provides the ability for deep integrations with your existing monitoring, logging, and alerting systems.

This fosters development of community-driven modules like the py-zabbix Python module, which is an easy way to automate Zabbix as well as send/retrieve metrics.


We will make sure python and pip are installed on our Ubuntu host, and then use pip to install the py-zabbix module.

$ sudo apt-get install git software-properties-common python python-pip -y

$ pip install py-zabbix

Validate Web access

Before moving on, validate that you have access to the Zabbix server via your browser, and that you know the correct credentials.

You should see a login screen like below when going to the URL http://<zabbixserver>/zabbix

Zabbix API for automation

Go ahead and grab my file hosted on github and run a quick test which will connect to Zabbix, retrieve all the enabled host names, look for any templates with ‘Linux’ in the name, and then show you a list of the most recent trigger issues.

$ wget

$ chmod 755

$ ./ Admin zabbix

The output should look something like below:

Going to connect to as Admin
Zabbix API version: 2.4.8
All enabled hosts: [u'zabbix2', u'grafana', u'trusty1']
templates with Linux in their name: [u'Template OS Linux', u'Template SNMP OS Linux']
TRIGGER PRIORITY 2: Lack of free swap space on {HOST.NAME}
TRIGGER PRIORITY 2: /etc/passwd has been changed on {HOST.NAME}
TRIGGER PRIORITY 2: Lack of free swap space on {HOST.NAME}
TRIGGER PRIORITY 2: Lack of free swap space on {HOST.NAME}
TRIGGER PRIORITY 3: Zabbix agent on {HOST.NAME} is unreachable for 5 minutes

You can see that the triggers reported above match the ones reported in the web GUI.


Of interest in is making the connection to the Zabbix server:

zapi = ZabbixAPI(url, use_older_authenticate_method, user, password)

Then constructing a REST call to the ‘host.get’ method using the correct parameters (as defined by the Zabbix API).

enabledhosts = zapi.do_request('host.get',
'filter': {'status': '0'},
'output': 'extend'
hostnames = [host['host'] for host in enabledhosts['result']]

And then pulling the latest triggers, once again using the API reference for ‘trigger.get’

triggers = zapi.do_request('trigger.get', { 'min_severity':'2', 'withUnacknowledgedEvents':'true','sortfield':['priority'], 'sortorder': 'ASC', 'limit':'5', 'output':'extend' })
for trigger in triggers['result']:
 print "TRIGGER PRIORITY {}: {}".format(trigger['priority'],trigger['description'])

Sending/Retrieving metrics

There is some asymmetry here to understand.  In the examples above using the REST API, you used the web port of the Zabbix server and it required authorization.  Then same holds true when you retrieve item values.

However, when you send metrics to Zabbix, that is done via port 10051 (by default), and is done using a simple binary protocol.  Values are sent without requiring authentication.

Before running these next two scripts, you MUST create two custom fields of type “zabbix trapper”.  In the web GUI, select Configuration > Hosts, then on the host you want to experiment with, select “items” and it will list the items it is monitoring.

Press “create item”, and use the name “mystr1” of type “zabbix trapper” of data type “text” and press “add”.  Then create another item with name “myint1” of type “zabbix trapper” of data type “numeric unsigned” and press “add”.

Now we will populate that string field by using my script:

$ wget

$ chmod 755

$ ./ myhost mystr1 testvalue1
{"failed": 0, "chunk": 1, "total": 1, "processed": 1, "time": "0.000097"}

$ ./ myhost mystr1 testvalue2
{"failed": 0, "chunk": 1, "total": 1, "processed": 1, "time": "0.000095"}

From the web GUI, go to Monitoring > Latest data, and filter by the host you selected.  You should see that the ‘mystr1’ field is populated with ‘testvalue2’ if you followed the steps above.

Now, let’s retrieve those values using my instead of the web GUI.

$ wget

$ chmod 755

$ ./ Admin zabbix trusty1 mystr1
Going to connect to as Admin, and retrieve from host trusty1 the item teststr1
Zabbix API version: 2.4.8
Found host trusty1 with id 10106
Found item mystr1 on host trusty1 with item id/type 23769/4
Retrieved 2 rows of history
2017-04-21 15:15:48 @ testvalue2
2017-04-21 15:15:45 @ testvalue1



Installing Zabbix Sender for debugging:


dpkg -i zabbix-sender_2.4.8-1+trusty_amd64.deb

zabbix_sender -vv -z <zabbixServer> -s <hostKey> -k <key> -o <value>