Using Zabbix preprocessing to alter item values
Preprocessing item values is an important functionality in Zabbix; we can use it to create all kinds of checks. We've already done some preprocessing in this chapter, but let's take a deeper dive into it and what it does.
Getting started
We are going to need a Zabbix server to create our check for. We will also need a passive Zabbix agent on a Linux host to get our values from and preprocess them. We can use the agent that is running on our Zabbix server for this; in my case, this is lar-book-centos.
How to do it…
- Let's start by logging in to our Zabbix frontend and going to Configuration | Hosts.
- Click on your Zabbix server host; in my case, it's called lar-book-centos.
- Now go to Items and click on the blue Create item button in the top-right corner. Let's create a new item with the following information:
- Make sure to change
ens192
to your own primary network interface. You can find your primary network interface by logging in to the Linux CLI and executing the following:ifconfig
- Back at the create item screen, click on the blue Add button. This item will use the Zabbix agent to execute a remote command on the Linux CLI.
- When we navigate to this new item, we can see that the item becomes unsupported. This is because when we use the
system.run
key, we need to allow it in the Zabbix agent configuration: - Log in to the Linux CLI of the monitored host and edit the Zabbix agent configuration with this:
vim /etc/zabbix/zabbix_agent2.conf
- Go to the Option: AllowKey line and add
AllowKey=system.run[*]
as here: - Save the file and restart the Zabbix agent with this:
systemctl restart zabbix-agent2
- Back at the Zabbix frontend, the error we noticed in step 6 should be gone after a few minutes.
- Navigate to Monitoring | Latest data and filter on your Zabbix server host lar-book-centos and the name of the new Get traffic statistics from CLI item.
- The value should now be pulled from the host. If we click on History, we can see the full value; it should look as follows:
- The information seen in the image is way too much for just one item. We need to split this up. Let's use pre-processing to get the number of RX bytes from the information.
- Go back to Configuration | Hosts and click on your Zabbix server host. Go to Items on this host.
- Click on the Get traffic statistics from CLI item to edit it. Change the name to
Total RX traffic in bytes for ens192
and addB
to Units. It will look like this: - Now click on Preprocessing and click on the underlined Add button.
- A Regular expression (regex) field will be added, which we are going to fill to match the total number of bytes for your interface. Fill in the following:
- Make sure to also select the box under Custom on fail.
- Let's click on the underlined Add button again and use the drop-down menu for this new step to select Discard unchanged. The end result will look like this:
- We can now press the blue Update button to finish editing this item.
- Navigate back to Monitoring | Latest data and filter on your host and the new item name, Total RX traffic in bytes for ens192. Make sure to use your own interface name.
- We can now see our value coming in, and we have an item displaying our total RX traffic for our main interface:
How it works…
We've already done some preprocessing in the Working with calculated and dependent items recipe to get data from a master item. We also used preprocessing in the Setting up HTTP agent monitoring recipe to get a specific value from a web page. We didn't go over the preprocessing concepts used in those recipes, though, so let's go over them.
When working with preprocessing, it's important to know the basic setup. Let's take a look at the incoming data before we used preprocessing:
This is a lot of information. When we look at how Zabbix items are used, we try to put graspable information in a single item. Luckily, we can preprocess this item before we store the value in Zabbix. In the following figure, we can see the preprocessing steps we added to our item:
Our first step is a regex. This step will make sure to only use the numbers we need. We match on the word RX, then the word bytes and a sequence of numbers after them. This way, we end up with the total number of RX bytes in capture group 2. This is why we fill in \2 in the output field. We also specify Custom on fail, which will discard any value if the regex doesn't match.
Our second step is to discard any values that are the same as the value received before. Instead of storing duplicate values, we simply discard them and save some space in our Zabbix database.
Tip
It's a lot easier to build a regex when using an online tool such as https://regex101.com/. You can see what number your capture groups will get and there's a lot of valuable information in the tools as well.
It's important to note here that steps are executed in the sequence they are defined in the frontend. If the first step fails, the item becomes unsupported unless Custom on fail is set to do something else.
By adding preprocessing to Zabbix, we open up a whole range of options for our items, and we are able to alter our data in almost any way required. These two steps are just the beginning of the options opened up when diving into the world of Zabbix preprocessing.
See also
Preprocessing in Zabbix is an important subject and it's impossible to cover every aspect of it in a single recipe. The two preprocessing steps in this recipe's example are just two of the many options we can use. Check out the official Zabbix documentation to see the other options we can use:
https://www.zabbix.com/documentation/current/manual/config/items/preprocessing