This chapter is going to be all about making sure that you as a Zabbix administrator are doing as little work as possible on host and item creation. We are going to learn how to perform (or perfect, maybe) automatic host creation. Check out the recipes featured here to see just what we are going to discover.
In this chapter, we will first learn how to set up Zabbix discovery with Zabbix agent and Simple Network Management Protocol (SNMP). We will then set up active agent autoregistration. Later, we will also cover the automatic creation of Windows performance counters and Java Managements Extension (JMX) items.
In this chapter, we will cover the following recipes:
As this chapter is all about host and item discovery, besides our Zabbix server we will need one new Linux host and a Windows host. Both these hosts will need Zabbix agent 2 installed, but not configured just yet.
Furthermore, we are going to need our JMX host, as configured in Chapter 3, Setting Up Zabbix Monitoring, and a new host with SNMP set up. To learn more about setting up an SNMP-monitored host, check out the Working with SNMP monitoring recipe in Chapter 3, Setting Up Zabbix Monitoring.
A lot of Zabbix administrators use Zabbix agent extensively and thus spend a lot of time creating Zabbix agent hosts by hand. Maybe they don't know how to set up Zabbix agent discovery, maybe they didn't have time to set it up yet, or maybe they just prefer it this way. If you are ready to get started with Zabbix agent discovery, in this recipe we will learn just how easy it is to set it up.
Besides our Zabbix server, in this chapter's introduction I mentioned that we will need two (empty) hosts with Zabbix agent 2 installed: one Windows host and one Linux host. If you don't know how to install Zabbix agent 2, check out Chapter 3, Setting Up Zabbix Monitoring, or see the Zabbix documentation at https://www.zabbix.com/documentation/current/en/manual/concepts/agent2.
Let's give the servers the following hostnames:
vim /etc/zabbix/zabbix_agent2.conf
Hostname=lar-book-disc-lnx
C:Program Filesabbix Agent 2zabbix_agent2.conf
Hostname=lar-book-disc-win
Important note
We are using an Update interval of 1 minute in this example. As this might take up a lot of resources on your server, make sure to adjust this value for your production environment. For example, 1 hour is a much better production value.
Tip
When creating Zabbix actions, it's important to keep the order of creation for Conditions in mind. The labels seen in the preceding screenshot will be added in order of creation. This means that it's easier to keep track of your Zabbix actions if you keep the order of creation the same for all actions.
Tip
Use the Monitoring | Discovery page to keep a close eye on the hosts you expect to show up in your Zabbix setup. It's very useful to track new hosts coming in and see which Zabbix discovery rule was used to create the host.
Network discovery might not be very hard to set up initially, but there are loads of options to configure. For this example, we made the choice to use the agent.hostname key as our check. We create the Zabbix hostname based on what's configured in the Zabbix agent config file.
What happens is that Zabbix network discovery finds our hosts and performs our check. In this case, the check is: What is the hostname used by the Zabbix agent? This information, plus our IP address, is then triggering the action. Our action then performs our configured checks:
If all of those checks are true, our action will then create our newly discovered host with the following:
We will end up with two newly created hosts, with all the right settings:
Creating the host by using the configuration file settings isn't always the right way to go, but it's a solid start to working with network discovery.
If you want a more flexible environment where you don't have to even touch the Zabbix agent configuration file, then you might want to use different checks on the discovery rule. Check out which keys we can use to build different discovery rules in the Zabbix documentation at https://www.zabbix.com/documentation/current/en/manual/config/items/itemtypes/zabbix_agent.
If you work with a lot of SNMP devices but don't always want to set up monitoring manually, network discovery is the way to go. Zabbix network discovery uses the same functionality as Zabbix agent discovery, but with a different configuration approach.
To get started with network discovery, we are going to need a host that we can monitor with SNMP. If you don't know how to set up a host such as this, check out the Working with SNMP monitoring recipe in Chapter 3, Setting Up Zabbix Monitoring. We'll also need our Zabbix server.
hostnamectl set-hostname lar-book-disc-snmp
exec bash
systemctl restart snmpd
Important note
Please note that our check type is not SNMP version-independent. We have three SNMP versions and thus three different check types to choose from, unlike with our new SNMP interface selection on the Zabbix 6 host screen.
In this recipe, we've created another discovery rule, but this time for SNMP. As you've noticed, the principle remains the same, but the application is a bit different.
When we created this Zabbix discovery rule, we gave it two checks instead of the one check we did in the previous recipe. We created one check on SNMP OID .1.3.6.1.2.1.1.5.0 to retrieve the hostname of the device through SNMP. We then put the hostname retrieved from the system into Zabbix as the Zabbix hostname of the system.
We also created a check on SNMP OID .1.3.6.1.2.1.25.1.4.0. This check will retrieve the following string, if present:
"BOOT_IMAGE=(hd0,gpt2)/vmlinuz-4.18.0-193.6.3.el8_2.x86_64 root=/dev/mapper/cl-root ro crashkernel=auto resume=/dev/mapper/cl-swa"
If the string is present, it means that the boot image is Linux on this host. This is a perfect example of how we can retrieve multiple OIDs to do multiple checks in our Zabbix discovery rules. If we'd been monitoring a networking device, for instance, we could have picked an OID to see if it was a Cisco or a Juniper device. We would replace .1.3.6.1.2.1.25.1.4.0 with any OID and poll it. Then, we would create our action based on what we received (Juniper or Cisco) and add our templates accordingly.
Important note
General knowledge of SNMP structure is very important when creating Zabbix discovery rules. We want to make sure we use the right SNMP OIDs as checks. Make sure to do your research well, utilize SNMP walks, and plan out what OIDs you want to use to discover SNMP agents. This way, you'll end up with a solid monitoring infrastructure.
Using discovery to set up your Zabbix agents is a very useful method to automate your host creation. But what if we want to be even more upfront with our environment and automate further? That's when we use a Zabbix feature called active agent autoregistration.
For this recipe, we will need a new Linux host. We will call this host lar-book-lnx-agent-auto. Make sure to install the Zabbix agent 2 to this host. Besides this new host, we'll also need our Zabbix server.
vim /etc/zabbix/zabbix_agent2.conf
ServerActive=10.16.16.152
Hostname=lar-book—lnx-agent-auto
This is not a requirement though, as Zabbix agent will use the system hostname if it is not filled out.
Your page should now look like this:
Tip
We can set up conditions for different types of hosts. For instance, if we want to add Windows hosts, we set up a new action with a different hostname filter. This way, it is easy to maintain the right groups and templates, even with autoregistration.
Active agent autoregistration is a solid method to let a host register itself. Once the ServerActive= line is set up with the Zabbix server IP, the host agent will start requesting configuration data from the Zabbix server. The Zabbix server will receive these requests, and if there is an action set up in Zabbix (as we just did in this recipe), the host autoregisters to Zabbix:
We can do a bunch of cool automations with this functionality. We could create a script to fill up our Zabbix agent configuration file with the right ServerActive= line on our hosts in a certain IP pool.
It would also be super-easy to set up new hosts with Ansible. We can automate the Zabbix agent installation with Ansible and we can add the ServerActive= line in the /etc/zabbix/zabbix_agent2.conf file using Ansible as well. Our Zabbix server autoregistration action will take care of the rest from here.
Zabbix agent autoregistration is a perfect way to get a zero-touch monitoring environment that's always up to date with our latest new hosts.
Not every company uses hostnames that reflect the machine's OS or other attributes. This is when Zabbix HostMetadata can come in very useful. We can add this field to the active Zabbix agent configuration to reflect the attributes of the machine.
Afterward, we can use this HostMetadata in our Zabbix discovery action to do the same kind of filtering we did on the hostname.
We also have the HostInterface and HostInterfaceItem parameters in the Zabbix agent configuration file, which are used for autoregistration. The host will use the specified IP or DNS name as its Zabbix agent interface IP or DNS, as seen in the Zabbix frontend. We can also use this functionality to enable passive agent monitoring while using autoregistration to create the host.
Check out this link for more information:
https://www.zabbix.com/documentation/current/manual/discovery/auto_registration#using_host_metadata
In Zabbix 6, it is possible to discover Windows performance counters. In this recipe, we will go over the process of discovering Windows performance counters to use in our environments.
Discovering Windows performance counters might seem to be a little tricky at first, as it uses both Windows- and Zabbix-specific concepts. But once we finish this recipe you'll know exactly how to set it up.
In this chapter, we added the lar-book-disc-win host to our setup, which is the host used in our Zabbix agent discovery process. We can reuse this host to discover Windows performance counters easily.
Of course, we'll also need our Zabbix server.
Important note
We are using an Update interval of 1 minute in this example. As this might take up a lot of resources on your server, make sure to adjust this value to your production environment. For example, 1 hour is a much better production value.
Windows performance counters have been around for a long time and they are very important to anyone who wants to monitor Windows machines with Zabbix. Using Low-Level Discovery (LLD) in combination with Windows performance counters makes it a lot easier and more flexible to build solid Windows monitoring.
In this recipe, we created a very simple but effective Windows performance counter discovery rule by adding the discovery rule with the perf_instance.discovery[Processor] item key. The [Processor] part of this item key directly correlates to the perfmon.exe window we saw. If we look at the following screenshot, we already see Processor listed:
When our discovery rule polls this item key, Zabbix agent will return the following value for our host:
[{"{#INSTANCE}":"0"},{"{#INSTANCE}":"1"},{"{#INSTANCE}":"_Total"}]
This value means that Zabbix will fill the {#INSTANCE} macro with three values:
We can then use these three values by using the {#INSTANCE} macro in our Item prototype, like we did here:
It will then create three items with our macro values, with the right keys to monitor the second part of our counter—% C1 time. If you expand the window in your perfmon.exe file, you can see all the different counters we could add to our item prototypes to monitor more Windows performance counters:
In Chapter 3, Setting Up Zabbix Monitoring, we went over setting up JMX monitoring in the recipe titled Setting up JMX monitoring. What we didn't cover yet though was discovering JMX objects.
In this recipe, we will go over how to set up JMX objects with LLD, and after you've finished this recipe, you'll know just how to set it up.
For this recipe, we will need the JMX host that you set up for the Setting up JMX monitoring recipe in Chapter 3, Setting Up Zabbix Monitoring. Make sure to finish that recipe before working on this one.
We will also need our Zabbix server with our Zabbix JMX host titled lar-book-jmx.
Monitoring JMX applications can be quite daunting at first, as there is a lot of work to figure out while building your own LLD rules. But now that you've built your first LLD rule for JMX, there is a clear structure in it.
First, for our discovery rule, we've picked the item key:
jmx.discovery[beans,"*:type=MemoryPool,name=*"]
MemoryPool is what we call an MBean object in Java. We poll this MBean object for several JMX objects and fill the macros accordingly.
We picked the name=* object to fill the {#JMXNAME} macro in this discovery rule. Our macro is then used in our item prototype to create our items.
Our items are then created, like this:
If we look at the keys of the items, we can see that we poll the Type JMX attribute on every MemoryPool with different names.
That's how we create JMX LLD rules with ease.
If you are not familiar with MBeans objects, then make sure to check out the Java documentation. This will explain to you a lot about what MBeans are and how they can be used for monitoring JMX attributes: https://docs.oracle.com/javase/tutorial/jmx/mbeans/index.html
Tip
Before diving deeper into using JMX object discovery, dive deeper into the preceding JMX object documentation. There's a lot of information in it and it will greatly improve your skills in creating these LLD rules.
18.116.14.245