Zabbix API | The beginning

Juan Lamarão
5 min readJun 27, 2021

--

Image from Zabbix official website.

TL;DR

Zabbix has an API free to use just out of the box. Did you know that? My goal here is to note down what it is, ideas of use cases, and how to set up an effortless way of using it.

What is an API

An API (the short version for Application Programming Interface) is a box of functions and procedures that access data from another source, such as an application, an operational system, a service, or even a database.

There are different ways of using an API, also called Methods (or even Verbs). Here is a brief description:

  • GET — used to retrieve information;
  • POST — to create new resources;
  • PUT — to update/replace information of resources, or even the resource itself;
  • PATCH — to update/modify information of resources, or even the resource itself;
  • DELETE — self-explanatory, used to delete a resource.

For Zabbix, it is not different. It’s just a fast way of doing things you can already do on the Zabbix front-end. Of course, in some cases, the operation done in the Front-end can be tedious because of the repetition of actions.

For instance, the creation of 10 users will lead us to at least 30 clicks, a bunch of pages loading, and the insert of information, such as name, alias, group, and the set of privileges. On the other hand, with the Zabbix API, we can pass a JSON with the name of the users and set all we need in one request.

The benefits are countless. I will show below some use cases like the one mentioned before.

Ideas of use cases

Here, the only limitation is your imagination (or necessity), but let’s see some ideas to tinker with your mind:

  • Create users in a batch;
  • User, group, host, item, trigger, and template management (retrieve, create, link, update, delete);
  • Get alerts, problems, events, and items, both values and configuration;
  • Integration with 3rd party software, ie. Grafana, ITSM tools, Chat software like Slack, Discord, or MS Teams;
  • Maintenance (ie. after opening a ticket on your ITSM tool for the repair of your company servers, it’s possible to execute automatically a call to Zabbix API and to create an outage with data collection, so your team can continue to be able to monitor the hosts, but avoid unnecessary alerts);
  • Retrieve current alerts and history with information of ACK, comments, duration, and more. If you already tried to export alerts from the Zabbix front-end to make a report of the team or alerts that occurred in the past month, you know what I’m talking about. It is sad, but Zabbix isn’t able to export more than a thousand records yet.

Setting up an effortless environment

Image from Insomnia official website.

For this task and future articles, we’ll use a software called Insomnia. The reason is that this API Rest Client is free, it does not need installation, and it can use variables.

The best part of using Insomnia is to get our API Access Token and set it as a variable, so we don’t have to rewrite it in every new request we make (stay in tune for the next post).

Let’s do the first test of our Zabbix API: Get the version of our Zabbix API Environment.

Open your Insomnia software, create a folder and let’s name it Zabbix API (because we are civilized):

Click in the ‘+’ and then in the option ‘New Folder’
Insert the name for your new folder

Now that you have your folder, hover your cursor over it and then click on the right icon that just appeared:

Select New Request:

Or Ctrl+N, for the intimate.

Set the name API Info and click Create:

Keep the GET Method while creating this request.

Okay, we have a folder and an empty request. Let’s set it up.

In the URL write down your Zabbix Front End access plus ‘/api_jsonrpc.php’. In my case, it was: http://localhost:8080/zabbix/api_jsonrpc.php

Also, change the ‘Body’ option from ‘No Body’ to ‘JSON’:

In this first test, we will use the Zabbix method ‘apiinfo. version’. Paste this JSON below in the body of your request.

{
"jsonrpc": "2.0",
"method": "apiinfo.version",
"params": [],
"id": 1
}

Explanation of the JSON body:

  • jsonrpc — the version of JSON-RPC protocol. Zabbix uses version 2.0;
  • method — let's make no mistake with the API Method. In the JSON body we are setting up the function that we want to access from the Zabbix API;
  • params — the parameters to be used in the previously mentioned method option. In this case, none is needed;
  • id — an arbitrary identification of your request, so you can find it later in the debug logs.

All set, now just hit ‘Send’:

The return should appear in the right panel:

This request brought us the Zabbix API version, in my case “5.2.6”. In the next post, we’ll see how to generate an authentication token and how to set it up as a variable.

Hope you find it useful and have had fun in the process.

God bless you all.

--

--

Juan Lamarão

Christian, IT Professional, Professor, Linux and Monitoring enthusiastic