Nagios Core Administration Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Running a service on all hosts on a group

In this recipe, we'll create a new service but, instead of applying it to an existing host, we'll apply it to an existing hostgroup; in this case, to a hostgroup called webservers. The steps to do this are very similar to adding a service for just one host; only one directive is different.

Getting ready

You should have a working Nagios Core 4.0 or better server running with a web interface. You should be familiar with adding services to inpidual hosts.

You should also have at least one hostgroup defined, with at least one host in it; we'll use a group called webservers with the sparta.example.net and athens.example.net hosts defined in it.

For reference, here is the hostgroup definition and the definitions for the two hosts in it:

define hostgroup {
    hostgroup_name  webservers
    alias           Webservers
}
define host {
    use         linux-server
    host_name   athens.example.net
    alias       athens
    address     192.0.2.22
    hostgroups  webservers
}
define host {
    use         linux-server
    host_name   sparta.example.net
    alias       sparta
    address     192.0.2.21
    hostgroups  webservers
}

How to do it...

We can create the service definition for the webservers group as follows:

  1. Change to the directory containing the file in which the webservers hostgroup is defined and edit it:
    # cd /usr/local/nagios/etc/objects
    # vi hostgroups.cfg
    
  2. Add the following just after the hostgroup definition. Change the lines in bold to suit your own template and hostgroup names:
    define service {
     use generic-service
     hostgroup_name webservers
        service_description    HTTP
        check_command          check_http
    }
  3. Reload the configuration:
    # /etc/init.d/nagios reload
    

It's important to note that if we were already monitoring these hosts with a per-host service of the same name, we will need to remove the definitions as well; Nagios Core will not start if a service of the same description is already defined on the same host.

How it works...

Adding a service to a hostgroup works exactly in the same way as adding it to an inpidual host, except it only requires one definition, which is then inpidually applied to all the hosts in the group. This means that it's a very good way to keep a Nagios Core configuration tidier. If we have a group with 50 different web servers in it and we need to monitor their HTTP services on the same basis for every one of them, we don't need to create 50 service definitions as well; we can just create one for its hostgroup, which amounts to a smaller and more easily updated configuration.

There's more...

Like the host_name directive for services, hostgroup_name can actually have several hostgroups defined, separated by commas. This means that we can apply the same service to not just one group but several of them. For services that we would want to run on several different groups, for example basic PING monitoring, this can amount to a much more flexible configuration.

See also

  • The Creating a new service section in this chapter
  • The Creating a new hostgroup section in this chapter
  • Using inheritance to simplify a configuration, Chapter 9, Managing Configuration