ZABBIX + REST API for beginners from beginner

If you want to connect your Zabbix to the server with an API that gives you information on the state of different objects, but do not know how to do this, then this article will give you an understanding and a concrete example of this implementation. I could not find similar HOWTOs on the Internet, so I decided to write it myself.

What do we have (base)

In our example, there is a Frigate video server that reports information on cameras. The request is sent to http://frigate.corp:3000/acl/monitor on the video server API. The response from the server comes in the form of a simple JSON array with simple values:

[ 
  { name: "Internal camera", isWork: true }, 
  { name: "External camera", isWork: false },
  ...
]

Those. I can in the server response we can get the name of the camera and its current state of operation.

Zabbix is ​​version 5.0.20, not the latest. But, judging by the documentation of new versions, the principles of interaction in this matter have not changed.

What we want to do

We want Zabbix to receive data from the server and create everything there itself. As a result, in Zabbix we want to see:

  • Host (host) – our video server

  • Monitoring elements (items) – cameras

  • Triggers – information about non-working cameras in the dashboard

Zabbix will receive JSON data from the video server.
Based on this data, it will create Monitoring Elements and Triggers.

How do we do it

In short, the whole process will consist of the following steps:

  • Creating a template (Template), which we will then apply to hosts

  • Create a Discovery rule for a template

  • Creating an Item prototype for a discovery rule

  • Creating a Trigger prototype for a Discovery Rule

Create a template

creating a template

creating a template

Everything is simple here – we indicate the name of the template and indicate the group of our hosts. If there is no ready-made group, you can write in text and create it right in this field. And click the Add button.

Create a discovery rule

Let’s open our template. And go to the Detection Rules tab (top menu). In the upper right corner there is a button Create a new rule, so we poke into it to create a new rule.

Specify the name of our rule – Cameras discovery.

Specify the type of detection – HTTP agent.

Specify the discovery key – videoserver.camera.discovery.

Specify the address to which the request will be sent –http://{HOST.HOST}:3000/acl/monitor.

{HOST.HOST} is the “technical” hostname that is used in the Zabbix configuration and matches the DNS name or IP address of the host. This name must be unique throughout the system.

Specify the request timeout. If the request is “heavy” and will be executed for a long time, set more. In our case – 10s.

In addition to these parameters, there are many other parameters that you may need if you have a complex query. For example, you can specify query parameters or change the query type. You can also add a request body. But in our example, this is not required.

Next, we indicate Время обновления запроса (Update interval)1m.

Still need to decide on an important parameter Период сохранения потерянных ресурсов (Keep lost resources period). If the objects do not change often, then you can leave it as is 30din our case the dynamics of adding/removing cameras can be frequent, so we set 1d.

Discovery Rule

Discovery Rule

That’s it with this tab. Next, go to the parsing tab – LLD macros.

In this case, we need to specify the name of the variable, which we will later use when creating the Elements, in our case – {#CAMERA}.

And the variable itself, which we receive in the response in the form of JSON, in our case, this is – $['name'].

Zabbix has its own JSON element conversion pattern. And it’s pretty complex. Personally, it took me a long time to understand what is right not $..nameA $['name'].

You can read more about JSONPath conversion here. There are also examples of transformations.

LLD macros

LLD macros

In general, the topic of creating discovery rules is very voluminous. You can read more about LLD Discovery rules here.

We are done with the discovery rule. Click Добавить (Add) and we go further.

Create an element prototype

The discovery rule will detect our elements (cameras) in the server response, but other than detecting and creating a variable, it will not do anything. And in order for these elements / cameras to appear at our host, we need to create a prototype of the element.

To do this, in our template, click on the item prototype (Item prototype)

open the prototype

open the prototype

In the upper right corner there is a Create item prototype button, click on it.

Fill in the element prototype Name – {#CAMERA}. In this case, this is the same variable that Zabbix got through the discovery rule.

Select the element type – HTTP agent. The same as in detection, but there are other implementation options. This means that we will send a request to the server, receive a response from it, and process this response already, just like in discovery.

Specify the key – videoserver.camera[{#CAMERA}]. The key must be unique throughout Zabbix.

Specify the request URL – http://{HOST.HOST}:3000/acl/monitor. Same as in Discovery.

Choose Type of information (Type of information) – Text. Why the text, I will explain further.

item prototype

item prototype

Everything is in this tab. Let’s go to Preprocessing.

Add a step, specify its JSONPath type and specify in the parameters – $[?(@.name=="{#CAMERA}")].isWork. In this case, it is our camera that is sampled and the value of the isWork parameter is read. Although the parameter is of type boolean, Zabbix will perceive it as a string. This is important to remember. Working with types in Zabbix, like working with JSON parsing, unfortunately leaves much to be desired. Well, either I didn’t get it. That is why we previously chose the type of returned information as Text.

Preprocessing item prototype

Preprocessing item prototype

This is where we’re done with setting up the element’s prototype. Click the Add button and move on.

Creating a Trigger Prototype

And finally, we come to the most important thing, creating a trigger that will receive our values ​​from the element and, based on them, give us a message about the problem.

Everything is as usual – open our template, go to the Trigger prototypes tab, click on Create trigger prototype.

In the name we indicate – {#CAMERA} is not working.

And we add an expression (Expression), which will signal us about the problem. Click Add. The form for adding a condition will open:

add expression

add expression

In this form, we click Select prototype. The element prototype selection form will open:

Select our wonderful element by clicking on it.

Select the str() function. This function performs a string comparison. You can read more here.

Specify V – [true]. This is the value we are comparing against the value in the element’s prototype. Yes, it comes like this. It is possible to remove the square brackets, but for this you will need to add a preprocessing rule in the element prototype, if desired.

The result is shown – 0. This means that if our value element does not match, a trigger will fire and we will receive a notification.

As a result, we get the following result:

trigger condition

trigger condition

Click Insert. And add (Add) trigger prototype.

That’s basically it.

Now you can create your own Video Server host with your own parameters. Here is an example:

host example

host example

And connect our template to it. Again a sample:

host template

host template

Well, now our wonderful Zabbix will create our elements and triggers on its own and report problems on its own.
Since I am not a very big specialist in Zabbix, I ask you not to kick much, there may be inaccuracies. Willing to make adjustments if needed.
Thanks to all! And all the best!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *