官术网_书友最值得收藏!

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
主站蜘蛛池模板: 通榆县| 正定县| 建平县| 苏尼特右旗| 贞丰县| 沾益县| 无为县| 搜索| 绥宁县| 吉林市| 双桥区| 尚志市| 杭锦旗| 兴义市| 蕲春县| 南宫市| 调兵山市| 重庆市| 白城市| 高密市| 岑巩县| 札达县| 广州市| 依兰县| 贡山| 靖州| 夏河县| 健康| 竹北市| 广平县| 托克逊县| 商水县| 东丰县| 正安县| 视频| 永年县| 昆明市| 泰顺县| 张家港市| 万荣县| 枣庄市|