Creating a new servicegroup
In this recipe, we'll create a new servicegroup. This allows us to make meaningful groups out of a set of arbitrary services so that we can view the status of all those services in a separate part of the web administration area.
Getting ready
You should have a working Nagios Core 4.0 or better server running with a web interface.
You should also have at least two defined services that form a meaningful group; perhaps they're similar kinds of services, such as mail services, are monitored by the same team, or they are both on the same set of servers at a physical location.
In this example, we have three servers performing mail functions: smtp.example.net
, pop3.example.net
, and imap.example.net
, running an SMTP, POP3, and IMAP daemon, respectively. All three of the hosts are set up in Nagios Core, and so are their services. We're going to add them into a new servicegroup called mailservices
.
Here are the definitions of the hosts and services used in this example, so you can see how everything fits together:
define host { use linux-server host_name smtp.example.net alias smtp address 192.0.2.31 hostgroups webservers } define service { use generic-service host_name smtp.example.net service_description SMTP check_command check_smtp } define host { use linux-server host_name pop3.example.net alias pop3 address 192.0.2.32 hostgroups webservers } define service { use generic-service host_name pop3.example.net service_description POP3 check_command check_pop } define host { use linux-server host_name imap.example.net alias imap address 192.0.2.33 hostgroups webservers } define service { use generic-service host_name imap.example.net service_description IMAP check_command check_imap }
How to do it...
We can add our new servicegroup with the following steps:
- Change to our Nagios Core configuration objects directory and edit a new file called
servicegroups.cfg
:# cd /usr/local/nagios/etc/objects # vi servicegroups.cfg
- Add the following definition to the new file, substituting the values in bold with your own values:
define servicegroup { servicegroup_name mailservices alias Mail services }
- Move a directory up and then edit the
nagios.cfg
file:# cd .. # vi nagios.cfg
- Add this line to the end of the file:
cfg_file=/usr/local/nagios/etc/objects/servicegroups.cfg
- For each of the services we want to add to the group, find their definitions and add a
servicegroups
directive to put them into the new servicegroup. The definitions may end up looking something like this:define service { use generic-service host_name smtp.example.net service_description SMTP check_command check_smtp servicegroups mailservices } define service { use generic-service host_name pop3.example.net service_description POP3 check_command check_pop servicegroups mailservices } define service { use generic-service host_name imap.example.net service_description IMAP check_command check_imap servicegroups mailservices }
- Restart Nagios using the following command:
# /etc/init.d/nagios reload
We should now be able to visit the Service Groups section of the web interface and see a new servicegroup with three members:
How it works...
The configuration we've added in the preceding section includes a new file with a new servicegroup into the Nagios Core configuration and inserts appropriate services into the group. The servicegroup creates a separate section in the web interface for us to get a quick overview of only the services in that particular group.
There's more...
The way we've added services to the preceding groups is not the only way to do it. If we prefer, we can instead name the services (and their applicable hosts) for the group inside the group definition using the members
directive, so we could have something like the following:
define servicegroup { servicegroup_name mailservices alias Mail services members smtp.example.net,SMTP,pop3.example.net,POP3,imap.example.net,IMAP }
Note that we need to specify both the host that the service is on and the services to be monitored on it. This string needs to be comma-separated. The hostname always comes first, but it can be followed by any number of its services that are to be monitored.
This allows us to make a servicegroup that always includes every single service on every host, if we find that useful:
define servicegroup { servicegroup_name all alias All services members * }
If we're going to use servicegroup definitions extensively in our Nagios Core configuration to add services to groups, we should use whichever of the two methods we think is going to be easiest for us to maintain.
It's worth noting that a service can be in more than one group and there is no limit on the number of groups we can declare, so we can afford to be quite liberal with how we group our services into any sort of useful categories. A few examples of this could be organizing services by the appropriate contact for their notifications, internal functions, or customer-facing functions.
See also
- The Creating a new service section in this chapter
- The Running a service on all hosts in a group section in this chapter
- Using inheritance to simplify a configuration, Chapter 9, Managing Configuration