Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Zabbix 5 IT Infrastructure Monitoring Cookbook

You're reading from   Zabbix 5 IT Infrastructure Monitoring Cookbook Explore the new features of Zabbix 5 for designing, building, and maintaining your Zabbix setup

Arrow left icon
Product type Paperback
Published in Feb 2021
Publisher Packt
ISBN-13 9781800202238
Length 428 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Brian van Baekel Brian van Baekel
Author Profile Icon Brian van Baekel
Brian van Baekel
Nathan Liefting Nathan Liefting
Author Profile Icon Nathan Liefting
Nathan Liefting
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Chapter 1: Getting Started with Zabbix and User Management 2. Chapter 2: Setting Up Zabbix Monitoring FREE CHAPTER 3. Chapter 3: Working with Triggers and Alerts 4. Chapter 4: Building Your Own Structured Templates 5. Chapter 5: Visualizing Data, Inventory, and Reporting 6. Chapter 6: Using Discovery for Automatic Creation 7. Chapter 7: Setting Up Zabbix Proxies 8. Chapter 8: Integrating Zabbix with External Services 9. Chapter 9: Extending Zabbix Functionality with Custom Scripts and the Zabbix API 10. Chapter 10: Maintaining Your Zabbix Setup 11. Chapter 11: Advanced Zabbix Database Management 12. Chapter 12: Bringing Zabbix to the Cloud with Zabbix Cloud Integration 13. Other Books You May Enjoy

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

You have been reading a chapter from
Zabbix 5 IT Infrastructure Monitoring Cookbook
Published in: Feb 2021
Publisher: Packt
ISBN-13: 9781800202238
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime