9

I want to set Nagios up to issue warnings (e.g. mildly high load or disk usage) via email, but for critical items to alert by pager.

Right now we notify via email and pager simultaneously for all warnings. My generic contact definition is as follows:

define contact{
    name                            generic-contact
    service_notification_options    w,u,c,r,f,s
    host_notification_options       d,u,r,f,s
    service_notification_commands   notify-service-by-email,notify-service-by-pager
    host_notification_commands      notify-host-by-email,notify-host-by-pager
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

How can I make it so that the email notification occurs for both warnings and critical, but paging for critical only?

1
  • The question isn't an exact duplicate, but the answer is the correct answer for this question too - you need to set up multiple contacts for the same person, then assign these contacts according to how you require notification.
    – dunxd
    Feb 20, 2013 at 12:50

3 Answers 3

11

You should be able to achieve this by defining different contacts - one for pager notification only, one for email notification only - and assigning different values of host/service_notification_options:

define contact{
    name                            email-contact
    service_notification_options    w,u,c,r,f,s
    host_notification_options       d,u,r,f,s
    service_notification_commands   notify-service-by-email
    host_notification_commands      notify-host-by-email
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

define contact{
    name                            pager-contact
    service_notification_options    c,r
    host_notification_options       d,u,r
    service_notification_commands   notify-service-by-pager
    host_notification_commands      notify-host-by-pager
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

If you want to keep host/service definition overhead low, you should aggregate them in a contactgroup like this:

define contactgroup{

    contactgroup_name       pager-email
    members         pager-contact,email-contact
}

and use the contactgroup instead of individual contacts.

6
  • Even better, nice solution +1 Apr 13, 2012 at 19:12
  • does not work, i get error - Error: Could not find any contact matching 'pager-contact-crit' (config file '/usr/local/nagios/etc/objects/contacts.cfg', starting on line 63) Error: Failed to expand contacts for contactgroup 'admins' (config file '/usr/local/nagios/etc/objects/contacts.cfg', starting at line 63)
    – shorif2000
    Mar 21, 2017 at 9:09
  • Does the "r" mean this will notify everyone upon recovery from any state?
    – Dan Pritts
    Apr 25, 2018 at 20:44
  • @DanPritts basically yes - r = send notifications on recoveries (OK state) - documentation reference
    – the-wabbit
    May 2, 2018 at 7:23
  • Therefore, won't the pager and the email get all recovery messages, regardless of whether they got the down message?
    – Dan Pritts
    May 3, 2018 at 14:52
4

To send the WARNING alert via email and the CRITICAL alert via SMS, I also defined 2 contacts: one for email and one for SMS. It's working fine but below are what I have tried to accomplish this with only one contact.

The idea is re-write the (service|host)_notification_commands to check the $SERVICESTATE$ macro then use the corresponding method.

command.cfg

define command{
    command_name    notify-service
    command_line    $USER1$/notify.sh $SERVICESTATE$ $LASTSERVICESTATE$ $NOTIFICATIONTYPE$ $SERVICEDESC$ $HOSTALIAS$ $HOSTADDRESS$ "$LONGDATETIME$" "$SERVICEOUTPUT$" "$SERVICENOTESURL$" $CONTACTEMAIL$ $CONTACTPAGER$ $TIME$
    }

notify.sh

#!/bin/bash

SERVICESTATE=$1
LASTSERVICESTATE=$2
NOTIFICATIONTYPE=$3
SERVICEDESC=$4
HOSTALIAS=$5
HOSTADDRESS=$6
LONGDATETIME=$7
SERVICEOUTPUT=$8
SERVICENOTESURL=$9
CONTACTEMAIL=${10}
CONTACTPAGER=${11}
TIME=${12}

send_email() {
    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE\n\nService: $SERVICEDESC\nHost: $HOSTALIAS\nAddress: $HOSTADDRESS\nState: $SERVICESTATE\n\nDate/Time: $LONGDATETIME\n\nAdditional Info: $SERVICEOUTPUT\n\nURL: $SERVICENOTESURL" | /bin/mail -s "** $NOTIFICATIONTYPE Service Alert: $HOSTALIAS/$SERVICEDESC is $SERVICESTATE **" $CONTACTEMAIL
}

send_sms() {
    /usr/bin/wget --user=notifier --password=x "http://ip:port/smsgate/sms?tos=$CONTACTPAGER&content=$NOTIFICATIONTYPE, $SERVICEDESC, $HOSTADDRESS, $SERVICESTATE, $TIME, $SERVICEOUTPUT"

}

if [ $NOTIFICATIONTYPE = "PROBLEM" ]; then
    if [ $SERVICESTATE = "WARNING" ]; then
        send_email
    elif [ $SERVICESTATE = "CRITICAL" ]; then
        send_email
        send_sms
    fi
elif [ $NOTIFICATIONTYPE = "RECOVERY" ]; then
    if [ $LASTSERVICESTATE = "WARNING" ]; then
        send_email
    elif [ $LASTSERVICESTATE = "CRITICAL" ]; then
        send_email
        send_sms
    fi
fi

Notice that when the service is OK, I need to check the $LASTSERVICESTATE$ macro to decide which method to use.

contacts.cfg

define contact{
        contact_name                    quanta
        use                             single-contact
        alias                           Quan Tong Anh
        service_notifications_enabled   1
        host_notifications_enabled      1
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    c,w,r
        host_notification_options       d,u,r
        email                           quanta@domain.com
        pager                           0912345678
        }

templates.cfg

define contact{
        name                            single-contact
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options       d,u,r,f,s
        service_notification_commands   notify-service
        host_notification_commands      notify-host
        register                        0
        }
2

I don't know if this is the best option, but I can't recall of Nagios being able to page only on a special flag. What you can do however is duplicate the contact with his name and something to id him as sms (name-sms). This will cause redundant contacts. However if you are using groups you can just add the contact to the group.

define contact{
    name                            generic-contact-sms
    service_notification_options    c
    host_notification_options       d,u,r,f,s
    service_notification_commands   notify-service-by-pager
    host_notification_commands      notify-host-by-pager
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.