In this recipe, we'll learn how to define a contact group for a host. We'll demonstrate this as part of the general best practice of sending notifications to contact groups, rather than to individual contacts. This allows for more flexibility in assigning individual contacts to the appropriate groups for receiving appropriate notifications.
You should have a Nagios Core 4.0 or newer server with at least one host configured already. We'll use the example of sparta.example.net
, a host defined in its own file, which we'll configure to send its notifications to an existing contact group called noc
.
We can configure notifications to be sent to our contact group as follows:
/usr/local/nagios/etc/objects
. If you've put the definition for your host in a different file, move to that directory instead.# cd /usr/local/nagios/etc/objects
contact_group
definition exists for this. A valid definition might look something like the following, perhaps kept in contacts.cfg
:define contactgroup { contactgroup_name noc alias Network Operations members john,jane }
define contact { use generic-contact contact_name john alias John Smith email [email protected] } define contact { use generic-contact contact_name jane alias Jane Doe email [email protected] }
# vi sparta.example.net.cfg
The host definition may look something like this:
define host { use linux-server host_name sparta.example.net alias sparta address 192.0.2.21 notification_period 24x7 }
contact_groups
directive to noc
:define host {
use linux-server
host_name sparta.example.net
alias sparta
address 192.0.2.21
notification_period 24x7
contact_groups noc
}
contacts
directive for the host is already defined, you may wish to remove it, though you don't have to.# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg # /etc/init.d/nagios reload
With this done, all notifications for this host should be sent to each member of the noc
group, in our example, the contacts jane
and john
.
Defining the contacts that should receive notifications for particular hosts can be done by referring to them individually, as groups, or both. We could have managed the same result as the preceding one by defining the contacts directive for the host as jane,john
, referring to the individual contacts directly rather than by their group name, with the two names separated by a comma.
Much the same configuration process applies for setting the contact group for services, rather than hosts; we add a value for the contact_groups
directive to the service definition:
define service {
...
contact_groups noc
}
When an event takes place for the host or service that prompts a notification and the valid notification_option
flags are set for that event, Nagios Core will check the values of both the contacts and the contact_groups
directives for the host to determine whom to send the notification to. In our case, the server finds the value of contact_groups
to be noc
and so refers to that contact group to identify the individual contacts within it; it then sends the notification to each of them.
Which of the two methods to use for defining contacts for hosts or services, whether by individual contact references or groups, is up to you. For large numbers of hosts and services, it tends to be easier to direct notifications to the appropriate groups rather than individuals, as it affords us more flexibility in choosing who the notifications should be sent to by simply changing the individuals in the group.
Note that we can use both methods if necessary; if for a particular host or service it would be appropriate to also send a notification to an individual contact named james
, we could define the host as follows:
define host { ... contact_groups noc contacts james }
You need to define at least one contact or contact group for a host or service, or have it inherit one through a template, for it to be a valid host configuration for Nagios Core.
52.14.84.29