Book Image

Zabbix 5 IT Infrastructure Monitoring Cookbook

By : Nathan Liefting, Brian van Baekel
Book Image

Zabbix 5 IT Infrastructure Monitoring Cookbook

By: Nathan Liefting, Brian van Baekel

Overview of this book

Zabbix offers useful insights into your infrastructure performance and issues and enables you to enhance your monitoring setup with its variety of powerful features. This book covers hands-on, easy-to-follow recipes for using Zabbix 5 for effectively monitoring the performance of devices and applications over networks. The book starts by guiding you through the installation of Zabbix and using the Zabbix frontend. You'll then work your way through the most prominent features of Zabbix and make the right design choices for building a scalable and easily manageable environment. The book contains recipes for building items and triggers for different types of monitoring, building templates, and using Zabbix proxies. As you advance, you’ll learn how to use the Zabbix API for customization and manage your Zabbix server and database efficiently. Finally, you'll find quick solutions to the common and not-so-common problems that you may encounter in your everyday Zabbix monitoring work. By the end of this Zabbix book, you’ll have learned how to use Zabbix for all your monitoring needs and be able to build a solid Zabbix setup by leveraging its key functionalities.
Table of Contents (14 chapters)

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…

  1. Let's start by logging in to our Zabbix frontend and going to Configuration | Hosts.
  2. Click on your Zabbix server host; in my case, it's called lar-book-centos.
  3. 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:
    Figure 2.48 – New item creation screen, Get traffic statistics from CLI

    Figure 2.48 – New item creation screen, Get traffic statistics from CLI

  4. 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
  5. 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.
  6. 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:
    Figure 2.49 – Unsupported item information, Unknow metric system.run

    Figure 2.49 – Unsupported item information, Unknow metric system.run

  7. Log in to the Linux CLI of the monitored host and edit the Zabbix agent configuration with this:
    vim /etc/zabbix/zabbix_agent2.conf
  8. Go to the Option: AllowKey line and add AllowKey=system.run[*] as here:
    Figure 2.50 – Zabbix agent configuration file, AllowKey system.run

    Figure 2.50 – Zabbix agent configuration file, AllowKey system.run

  9. Save the file and restart the Zabbix agent with this:
    systemctl restart zabbix-agent2
  10. Back at the Zabbix frontend, the error we noticed in step 6 should be gone after a few minutes.
  11. 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.
  12. 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:
    Figure 2.51 – Zabbix agent system.run command executing 'ifconfig ens192' results

    Figure 2.51 – Zabbix agent system.run command executing 'ifconfig ens192' results

  13. 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.
  14. Go back to Configuration | Hosts and click on your Zabbix server host. Go to Items on this host.
  15. Click on the Get traffic statistics from CLI item to edit it. Change the name to Total RX traffic in bytes for ens192 and add B to Units. It will look like this:
    Figure 2.52 – Zabbix agent system.run item

    Figure 2.52 – Zabbix agent system.run item

  16. Now click on Preprocessing and click on the underlined Add button.
  17. 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:
    Figure 2.53 – Zabbix agent system.run item preprocessing

    Figure 2.53 – Zabbix agent system.run item preprocessing

  18. Make sure to also select the box under Custom on fail.
  19. 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:
    Figure 2.54 – Zabbix agent system.run item preprocessing

    Figure 2.54 – Zabbix agent system.run item preprocessing

  20. We can now press the blue Update button to finish editing this item.
  21. 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.
  22. We can now see our value coming in, and we have an item displaying our total RX traffic for our main interface:
Figure 2.55 – Zabbix Total RX traffic item latest data

Figure 2.55 – Zabbix Total RX traffic item latest data

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:

Figure 2.56 – Zabbix agent system.run command executing 'ifconfig ens192' results

Figure 2.56 – Zabbix agent system.run command executing 'ifconfig ens192' results

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:

Figure 2.57 – Zabbix agent system.run item preprocessing with 2 steps

Figure 2.57 – Zabbix agent system.run item preprocessing with 2 steps

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