In this recipe, you'll learn a shortcut to building groups of hosts using regular expressions tested against their hostnames.
This recipe is likely only of use to you if you use a naming convention for your hosts that allows them to be reasonably grouped by location, function, or some other useful metric by a shared string in their hostnames.
You will need to have a server running Nagios Core 4.0 or later, have access to the command line to change its configuration, and understand the basics of how host and servicegroups work. These are covered in the Creating a new hostgroup and Creating a new servicegroup recipes in Chapter 1, Understanding Hosts, Services, and Contacts.
In this example, we'll group three existing hosts named web-server-01
, web-server-02
, and web-server-03
into a new hostgroup, web-servers
, based only on their hostnames.
It would help to have some familiarity with regular expressions, but the recipe includes a simple example that should meet many use cases for this trick. An excellent site about regular expressions including tutorials can be found at http://www.regular-expressions.info/.
We can build a hostgroup by matching regular expressions to hostnames like so:
/usr/local/nagios/etc
. Edit the nagios.cfg
file in this directory via the following code:# cd /usr/local/nagios/etc # vi nagios.cfg
use_regexp_matching
directive. Uncomment it if necessary and set it to 1
, as follows:use_regexp_matching=1
use_true_regexp_matching
directive. Uncomment it if necessary and ensure that it's set to 0
, which it should be by default. Use the following code for this:use_true_regexp_matching=0
/usr/local/nagios/etc/objects
. If you have already followed the Grouping configuration files in directories recipe in this chapter, your own directory may differ. Take a look at this line:# cd /usr/local/nagios/etc/objects
hostgroups.cfg
, add a definition similar to the following. In this case, .+
means "any string at least one character in length"; you will probably want a pattern of your own devising appropriate to your own host names:define hostgroup {
hostgroup_name web-servers
members web-server-.+
alias Web Servers
}
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg # /etc/init.d/nagios restart
With this done, if your regular expression was correctly formed to match all the appropriate hostnames, you should find that the hosts become part of the group:
With the use_regexp_matching
directive set to 1
, Nagios Core will attempt to use hostname strings containing the *
, ?
, +
, or .
strings as regular expressions to match against hostnames. As web-server-01
, web-server-02
, and web-server-03
all match the regular expression web-server-.+
given in the members
directive for the web-servers
hostgroup, all three hosts are added to the group.
We will keep use_true_regexp_matching
off. If it were on, it would use every hostname pattern as a regular expression, whether or not it had any special regular expression characters. This is probably not what you want for most configurations; for example, if you had a host named database
that you wanted to add to a group, specifying database
for the members
directive would add all hosts with a host name matching this string, including, for example, database-frontend
, customer-database
.
This matching works in other places aside from hostgroup definitions; for example, you can also use it in the host_name
definitions for services, as follows:
define service {
use generic-service
host_name web-server-.+
service_description HTTP
check_command check_http
}
This is one of a number of very good suggestions for simplifying object definitions suggested in the Nagios Core manual: https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/objecttricks.html
18.119.235.79