Zabbix Cookbook - Sample Chapter
Zabbix Cookbook - Sample Chapter
ee
Zabbix Cookbook
You will begin with learning the Zabbix installation and conguration steps in detail, and then move on to
exploring its interface and architecture. You will then work with groups, users, and permissions. The book
will then take you through monitoring and testing with Zabbix. Followed by this, you will gain insights into
using templates, and also create impressive graphs and maps for data visualization and reporting. Towards
the end of the book, you will learn how to maintain, upgrade, and troubleshoot your Zabbix infrastructure.
Zabbix Cookbook
This book will teach you the skills required to successfully set up and congure a Zabbix server. We will
guide you through the interface setup and explain all the features available in Zabbix.
Sa
pl
e
system needs
Create and customize your own user settings
Add groups, hosts, and items in Zabbix to
your inventory
IT infrastructure
and problems
Patrik Uytterhoeven
P U B L I S H I N G
Zabbix Cookbook
Over 70 hands-on recipes to get your infrastructure up and
running with Zabbix
P U B L I S H I N G
Patrik Uytterhoeven
Zabbix Cookbook
Since its first release in 2001, Zabbix has distinguished itself from other monitoring
solutions by providing great flexibility and performance. Hence, it is being used more
and more by large companies. This doesn't mean Zabbix can't be used in smaller setups.
As mentioned before, Zabbix is a very flexible monitoring tool that can start small and
grow big. Monitoring more than 100,000 devices with Zabbix is no issue at all.
This book will show you how to set up Zabbix and configure it to your needs,
be it in a small or a large setup.
Creating templates
Linking templates
Nesting templates
Macros in templates
Introduction
So far in previous chapters, we have seen how to add hosts, create items, and add triggers
to those items. Now imagine you have 10 servers with PostgreSQL and you want to monitor
them. What will you do? You possibly will create items for each host to gather data and add
triggers for each item. We then could copy all items and triggers 10 times to all other hosts.
But what if you need to make changes? You will change it again on all hosts individually? What
if we have to do this on 100 hosts, program something with the API? To make our life easier,
we have templates in Zabbix. With templates we only have to create 1 item and 1 trigger. We
can then link this template to all our hosts and reuse all our work over and over.
Creating templates
In this recipe, we will show you how you can create templates in Zabbix. It's always advised to
use templates as much as possible.
143
Getting ready
For this recipe, we need a Zabbix server and access to the server with a Zabbix administrator
or super administrator account.
How to do it ...
1. From the menu, go to Configuration | Templates.
2. Click on the Template button on the upper right corner.
3. In the field Template name, you can write the name of your template, example.
PostgreSQL template.
4. In the box Visible name, you can add a name that will be visible in Zabbix in case that
the name of your template is too long or too cryptic for some reason.
5. In Groups, we will choose the group to which our template belongs. Here we select
Templates as group.
6. In the Description box we can write a note. This can be handy for later if the name of
your template is not informative enough to know what you monitor.
7. Next we click Add to save our templates.
144
Chapter 6
8. From the Configuration | Templates page, we can now see our template in the list
of templates.
9. As you can see each template has the option to add applications, items, triggers,
graphs, screens, and so on, just like we had on our hosts.
How it works
Templates are just a collection of items, triggers, applications, and so on, that we can reuse.
Instead of creating each item or trigger and so on on every host, we just create a template.
Then we make everything in our template and link it to a bunch of hosts so that we can reuse
our work.
There's more...
Templates are often used to link servers with the same service or application such as
PostgreSQL, Apache, Zabbix agent, Red Hat, Ubuntu, proxies, and so.
Since Zabbix 2.2, web scenarios were added to the template. When you edit a already saved
template, you will see some extra buttons at the end of the page. The Update button will, of
course, update any changes made. The Clone button will duplicate your template into a new
template and copy all entities like triggers, items, and so on inherited from linked templates.
The Full clone will do the same as the Clone button but also copy directly attached items,
triggers, and so on, from the template to the new template. Delete will obviously delete your
template but all items will remain with the host while Delete and clear will remove all items
from the linked hosts.
You cannot link a template to a host, if the template has items that are
already on the host as each item on a host has to be unique.
See also
https://www.zabbix.com/documentation/2.4/manual/config/
templates.
145
Getting ready
What do we need for this recipe? We need our Zabbix server properly set up. For this setup to
work we also need an administrator or super administrator account.
How to do it...
1. To export our template, we have to go in our menu to Configuration | Templates.
2. Next, we select the template that we would like to export and select Export selected
from the dropdown box.
3. Click on Go, now Zabbix will export the template in XML format to our disk.
When we want to import templates we have to follow more or less the same steps:
1. Our first step is to go to Configuration | Templates.
2. On the upper right corner, click Import.
3. We now see a box where we can select the file that we want to import.
4. Make a selection of the possible options. There is a column to update missing data
in case our template was already installed on our system and we wanted to update
it with new features. We also have a column for new templates in case we don't want
to install everything from our template. When importing hosts / templates using the
Delete missing option, host / template macros not present in the imported XML file
will be deleted too.
146
Chapter 6
How it works
Importing and exporting templates is very straightforward. When we want to export a template,
we only have to select what template we want to export and click on the Export button. Zabbix
will export the template in a file that we can download. The format of this file is XML.
When we want to import templates we have some more options. When importing templates
we have the options to update existing templates or to import them and make a choice of
what we would like to import.
147
There's more...
Besides templates, we can export and import also hosts, host groups, network maps, images,
and screens. Images are exported in a Base64 format.
Import and exporting templates can be useful in case you want to back up your templates.
It can also be useful if you have a development and a production environment. This way you
could develop and test everything first on the development machine, export templates and
import them on the production environment.
Another way is to share them with the community (I highly recommend you to do this).
See also
https://www.zabbix.com/documentation/2.4/manual/xml_export_
import?s[]=import&s[]=templates
http://en.wikipedia.org/wiki/Base64
http://zabbix.org/wiki/Zabbix_Templates
Linking templates
Having templates is great, but you probably want to link it to a host as well, else we would not
have much use of our templates. In this recipe, we will show you how to link those templates
to your hosts.
Getting ready
For this recipe, we need a Zabbix server and access to the server with a Zabbix administrator
or super administrator account. We also need a fresh host.
How to do it ...
1. Go back to the menu Configuration | Templates.
2. Select a template from the list, example. Template App Zabbix Agent. This can
be done by clicking on its template name.
3. From the box Other | group, select the fresh host that you would like to link with this
template and press the << button to move it to the box Hosts / Templates.
148
Chapter 6
How it works
We need to link our templates to hosts after we have created them. This way it is possible to
link our template to multiple hosts.
It is also possible to link templates from the host itself. This can be done by clicking on the
host from the menu Configuration | Hosts. You then select the Templates tab and select a
new template from the list or type the name in the Link new templates box. Don't forget to
click Add afterwards to add your template to the host.
See also
https://www.zabbix.com/documentation/2.4/manual/config/
templates/linking.
149
Nesting templates
It is also possible in Zabbix to link templates with each other. This may sound weird and
unnecessary at first but it's definitely a great feature. Imagine you have a web server with
Apache, MySQL, and PHP. You could create 1 big template to monitor all items or you could
create 3 templates. One for Apache, one for MySQL, and another one for PHP. But what if you
have another web server that you would like to monitor? Do you add those three templates
again to that host? What you could do in this case is create a new template Webserver
and link it with the three templates we mentioned earlier. In this case, we only have to link 1
template Webserver to our webserver and we can still use the template Apache or MySQL in
case we only want to monitor Apache or MySQL on another server.
Getting ready
To be able to do this recipe, you need a Zabbix server properly set up with an admin account
or super administrator account setup.
How to do it...
1. From the menu go, to Configuration | Templates.
2. Click Create New Template.
3. Fill in the Template name, example. Webserver Template.
4. Add it in the group templates.
5. Select Linked templates from the tab on top.
6. In the box Link new templates, click the Select button.
7.
From the popup window select Template App HTTP Service and Template
App Mysql.
150
Chapter 6
9. Back in the menu Linked templates, you now see the two templates we have
selected, we still have to click Add.
10. And finally click Update.
How it works
In the template menu we just create a new template. This template we link to two or more
templates so that our new template will inherit all of the items of the linked templates. Our
new template will then be linked to our host. This way we don't have to link two templates to
our host but only one. Later it is possible to link more templates to the new template we
have made.
In the Link new templates box, it is possible to type the name of the
template if you know the name or part of the name, and then select it
from a popup window.
There's more...
When we go back to the menu Configuration | Templates, we will see our template web
server and in the column Linked templates we will see the names of the templates linked
to our new template.
See also
https://www.zabbix.com/documentation/2.4/manual/config/
templates/nesting
Macros in templates
If you have a lot of servers then you probably want to have your templates a bit more dynamic.
There are probably also some cases where a certain value in your template is not fit for just
one server in your park. For this, we can make use of macros in our templates.
151
Getting ready
For this recipe to work, we need a Zabbix server and a Zabbix host. We also need to make
sure that we have a SSH session active on port 22 on our host as we will monitor the SSH
service on our host. For this, we will make use of macros. We also need to make use of the
super administrator account in Zabbix.
How to do it ...
1. First thing that we need to do is to go to Administration | general | Macros in our
Zabbix menu. (Macros can be selected from the dropdown menu on the right).
2. In the Macros menu, add a new macro {$SSH_PORT} and give it the value 422 or
something other than 22. It must be a port that is not in use by SSH.
3. Now go to Configuration | Template and create a new template with the name
Template SSH port.
4. Link the template to your client and save it.
152
Chapter 6
153
13. Next we go to our host again from the menu Configuration | Hosts.
14. We then go to the tab Macros in our host.
154
Chapter 6
15. Here we add the macro {$SSH_PORT} with value 22.
16. We now save our work.
17. Next when we go to Monitoring | Latest data, we will see that on our host for the
SSH port the status is 1. This means that our service is up.
How it works
In the Administration panel under General | Macros, we can define global macros. Those
global macros can be used in our templates. So by defining {$SSH_PORT} macro in our
item to monitor the SSH port, we were telling our template to look to the global macro. This
means that our template would always check, in this case, port 422. Because we defined a
new macro on our host with the value 22, that specific macro was over written for only this
particular host. So in our case the template would always look for a service on port 422, but
only for our host it would look for a service on port 22.
See also
https://www.zabbix.com/documentation/2.4/manual/config/macros/
usermacros
155
www.PacktPub.com
Stay Connected: