Nagios alert by email for WARNING and pager for CRITICAL [duplicate]

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.


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                           [email protected]
        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
        }

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
}