Choosing states for notification

In this recipe, we'll learn how to refine which notifications are sent by a host or service and which notifications should be received by a particular contact. We'll do this by changing both the notification types that hosts and services should generate and, to complement that, the notification types that a contact should be configured to receive.

Getting ready

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 only the DOWN and RECOVERY notifications, ignoring other notifications such as WARNING and UNKNOWN. It will send the notifications to an existing contact called john; we'll ensure that this contact is configured to receive those notifications.

How to do it...

We can configure the notification types for our host as follows:

  1. Change to the objects configuration directory for Nagios Core. The default is /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
    
  2. Edit the file containing the definition for your host. It might look something like this:
    define host {
        use                    linux-server
        host_name              sparta.example.net
        alias                  sparta
        address                192.0.2.21
        contacts               john
        notification_period    24x7
    }
  3. Add a value for the notification_options directive. In our case, we'll use d,r, to correspond to the notifications for the DOWN and RECOVERY states.
    define host {
        use                    linux-server
        host_name              sparta.example.net
        alias                  sparta
        address                192.0.2.21
        contacts               john
        notification_period    24x7
        notification_options   d,r
    }
  4. We should also ensure that the notifications for this host are enabled; specifically, that notifications_enabled is set to 1. An easy way to set this and other relevant directives is to have the host inherit from a template like linux-server, as done previously, but we can set it explicitly if we prefer:
    define host {
        use                    linux-server
        host_name              sparta.example.net
        alias                  sparta
        address                192.0.2.21
        contacts               john
        notification_period    24x7
        notification_options   d,r
        notifications_enabled  1
    }
  5. Edit the file containing the definition for the host's nominated contacts, or the contacts within its nominated contact group. In this case, we're editing the host's nominated contact, john. The definition might look something like the following:
    define contact {
        use           generic-contact
        contact_name  john
        alias         John Smith
        email         [email protected]
    }
  6. Edit or set the directive for host_notification_options for each contact to include the same values that you set for the host. In this case, we set it to d,u,r, which means that John Smith can receive the DOWN, UNREACHABLE, and RECOVERY notifications about the hosts.
    define contact {
        use                        generic-contact
        contact_name               john
        alias                      John Smith
        email                      [email protected]
        host_notification_options  d,u,r
    }
  7. Again, you should ensure that the contacts are configured to receive host notifications, with the host_notifications_enabled directive set to 1. We can do this via inheritance from a template like generic-contact as done in the preceding case, which tends to be easier, or we can set it explicitly:
    define contact {
        use                         generic-contact
        contact_name                john
        alias                       John Smith
        email                       [email protected]
        host_notification_options   d,r
        host_notifications_enabled  1
    }

    The analogous directive for receiving service notifications is service_notifications_enabled.

  8. Validate the configuration and restart the Nagios Core server:
    # /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
    # /etc/init.d/nagios reload
    

With this done, the notifications sent when the host enters the DOWN state and also when it recovers into OK state should be sent to the contact. But no other notifications about the host (such as UNREACHABLE or DOWNTIMESTART notifications) should be sent about the sparta.example.net host to the contact john.

How it works...

The kind of notifications a host should produce is defined in its notification_options directive and the kind of notifications that a defined contact should receive is configured in its host_notification_options directive. Both take the form of comma-separated values.

The valid values for the notification_options directive for host objects are:

  • d: Notifications for a host being in the DOWN state
  • u: Notifications for a host being in the UNREACHABLE state
  • r: Notifications for a host recovering from problems (becoming UP)
  • f: Notifications for a host beginning or ending the flapping state
  • s: Notifications for a host beginning or ending its scheduled downtime

For example, we might define a host that should only send notifications when entering the DOWN or UP states, but ignore the notification events for UNREACHABLE or flapping, as follows:

define host {
    ...
    notification_options d,r
}

For service objects, the valid directives are slightly different:

  • w: Notifications for a service being in the WARNING state
  • u: Notifications for a service being in the UNKNOWN state
  • c: Notifications for a service being in the CRITICAL state
  • r: Notifications for a service recovering from problems (becoming OK)
  • f: Notifications for a service beginning or ending the flapping state
  • s: Notifications for a service beginning or ending its scheduled downtime

For example, for a host that should send notifications for all of these events except for the flapping and scheduled downtime states, we might define:

define service {
    ...
    notification_options  w,u,c,r
}

All of the preceding values can be used in contact object definitions to restrict the service notifications that a particular contact should receive, through the service_notification_options directive:

define contact {
    ...
    service_notifications_enabled  1
    service_notification_options   w,u,c,r
}

There's more...

Unless we have a specific reason for particular contacts not to accept notifications of a particular kind, it's good practice to configure the contacts to receive all the notifications that they are sent, by including all the flags in both the directives for the contact:

define contact {
    ...
    host_notification_options     d,u,r,f,s
    service_notification_options  w,u,c,r,f,s
}

This means that the contact will process any notification dispatched to it and we can instead restrict the kinds of notifications that are sent out by the hosts and services for which this is the delegated contact. With new configurations, it is generally better to start by sending too much information and then removing the unnecessary notification flags if appropriate, rather than potentially missing important messages because they're never sent.

If we intend to do this for all of our contacts, it may be appropriate to include these directives as part of a contact template and inherit from it using the use directive. The generic-contact contact template included with the default configuration may be suitable.

Tip

If you want to completely disable the notifications for a given host, service, or contact, the single value n for notification_options, host_notification_options and service_notification_options will do this.

See also

  • The Configuring notification for groups section in this chapter
  • The Automating contact rotation section in this chapter
  • The Defining a custom notification method section in this chapter
  • Using inheritance to simplify configuration, Chapter 9, Managing Configuration
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.51.117