Nagios notification script issues


#1

Hello all, I need some assistance with a script and hope someone here can be of help. We desired a script that would allow us acknowledgement from emails, who acknowledged and additional enhancements. I found a thread: http://article.gmane.org/gmane.network.nagios.user/49195 that provided a shell script to do exactly what we needed. However, the script was not formatted exactly to the Nagios stock emails of a default install so I modified it to the following the script to more suite what the default Nagios notifications look like:


#!/bin/sh
# Purpose: Modified shell script replacing stock Nagios notification to provide acknowledgement logic. 
# Modified by dky at gmail.com
#Original script format: http://article.gmane.org/gmane.network.nagios.user/49195

NAGIOSHOST=nagios.example.com
NAGIOSCGIURL="https://${NAGIOSHOST}/nagios/cgi-bin/cmd.cgi";

tmpfile=$(mktemp /tmp/$$.XXXXXX)
exec > $tmpfile

host_info() {
	printf "%b" "***** Nagios *****\n\n"
        printf "Notification Type: $NAGIOS_NOTIFICATIONTYPE\n"
        printf "Host: $NAGIOS_HOSTNAME\n"
        printf "Address: $NAGIOS_HOSTADDRESS"

        if  "$SERVICEDESC" ]; then
                printf "Service:\t$NAGIOS_SERVICEDESC\n"
        fi
}

state_info() {
        printf "State: "
        if  "$NAGIOS_SERVICESTATE" ]; then
                printf "$NAGIOS_SERVICESTATE"
        elif  "$NAGIOS_HOSTSTATE" ]; then
                printf "$NAGIOS_HOSTSTATE"
        fi
        printf "\n"
}

state_detail() {
        if  "$NAGIOS_SERVICEOUTPUT" ]; then
                printf "Date/Time: $NAGIOS_LONGDATETIME\n\n"
                printf "Additional Info: $NAGIOS_SERVICEOUTPUT\n"
        elif  "$NAGIOS_HOSTOUTPUT" ]; then
                printf "Date/Time: $NAGIOS_LONGDATETIME\n\n"
                printf "Additional Info: $NAGIOS_HOSTOUTPUT\n"
        fi
        printf "\n\n"

}

alert_ack_url() {
        printf "Acknowledge this alert: "
        if  "$NAGIOS_SERVICEDESC" ]; then
                printf "${NAGIOSCGIURL}?cmd_typ=34&host=${NAGIOS_HOSTNAME}&service=${NAGIOS_SERVICEDESC}\n"
        else
                printf "${NAGIOSCGIURL}?cmd_typ=33&host=${NAGIOS_HOSTNAME}\n"
        fi
}

ack_info() {
	printf "Date/Time: $NAGIOS_LONGDATETIME\n\n"
        printf "Acknowledged by: "
        if  "$NAGIOS_SERVICEACKAUTHOR" ]; then
                printf "$NAGIOS_SERVICEACKAUTHOR"
        elif  "$NAGIOS_HOSTACKAUTHOR" ]; then
                printf "$NAGIOS_HOSTACKAUTHOR"
        fi
        printf "\n"

        if  "$NAGIOS_SERVICEACKCOMMENT" ]; then
                printf "Comment: $NAGIOS_SERVICEACKCOMMENT"
        elif  "$NAGIOS_HOSTACKCOMMENT" ]; then
                printf "Comment: $NAGIOS_HOSTACKCOMMENT"
        fi
        printf "\n"
}

case "$NAGIOS_NOTIFICATIONTYPE" in
        PROBLEM|FLAPPINGSTART)
                TAG="ALERT"

                if !  "$NAGIOS_CONTACTPAGER" ]; then
                        host_info
                        printf "\n"
                        state_info
                        printf "\n"
                fi

                state_detail

                if !  "$NAGIOS_CONTACTPAGER" ]; then
                        printf "\n"
                        alert_ack_url
                fi
        ;;

        RECOVERY|FLAPPINGSTOP)

                TAG="OK"

                if !  "$NAGIOS_CONTACTPAGER" ]; then
                        host_info
                        printf "\n"
                        state_info
                        printf "\n"
                fi

                state_detail
        ;;

        ACKNOWLEDGEMENT)
                TAG="ACK"
                
                if !  "$NAGIOS_CONTACTPAGER" ]; then
                        host_info
                        printf "\n"
                fi

                ack_info
        ;;

        *)
                echo "invalid notification type" > /dev/stderr
                exit 1
        ;;
esac

if  "$NAGIOS_CONTACTEMAIL" ]; then
        recipient="$NAGIOS_CONTACTEMAIL"
elif  "$NAGIOS_CONTACTPAGER" ]; then
        recipient="$NAGIOS_CONTACTPAGER"

else
        [EMAIL PROTECTED]
fi

subject="${TAG}: ${NAGIOS_HOSTNAME}"

if  "$NAGIOS_SERVICEDESC" ]; then
        subject="${subject}/${NAGIOS_SERVICEDESC}"
fi

if  "$NAGIOS_SERVICESTATE" ]; then
        subject="** $NAGIOS_NOTIFICATIONTYPE Service ${subject} is ${NAGIOS_SERVICESTATE} **"
elif  "$NAGIOS_HOSTSTATE" ]; then
        subject="** $NAGIOS_NOTIFICATIONTYPE Host ${subject} is ${NAGIOS_HOSTSTATE} **"
fi

mail -s "$subject" "$recipient" < $tmpfile
rm -f $tmpfile
exit 0

The issue is that I cannot get the proper expansion of “$NAGIOS_SERVICEOUTPUT” variable. It seems to cut off here is an example:

From:	nagios@example.com
Sent:	Monday, January 25, 2010 11:20 AM
To:	dky@someaddress
Subject:	** PROBLEM Service ALERT: networkdevice.example.com/Check PING is CRITICAL **

***** Nagios *****

Notification Type: PROBLEM
Host: networkdevice.example.com
Address: 99.99.99.99
State: CRITICAL

Date/Time: Mon Jan 25 11:19:48 EST 2010

Additional Info: PING CRITICAL - Packet loss = 0


Acknowledge this alert: https://nagios.example.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&host=networkdevice.example.com

Notice “Additonal Info: PING CRITICAL - Packet loss = 0”. With the stock Nagios config we get: “Additional Info: PING CRITICAL - Packet loss = 0%, RTA = 558.89 ms.” The script seems to be truncating the everything after the 0. I have tried to use the “NAGIOS_LONGSERVICEOUTPUT” variable but it returns no data at all! :shock: . I also tried enclosing the variable between double quotes ““NAGIOS_SERVICEOUPUT”” but still no luck. Can anyone chime in and tell me what I could be doing wrong to fully expand the variable?

Thank you for any input.

Don


#2

To me it looks like it needs some sort of escaping because it interprets the % sign in some way.
Try your script on some other service which doesn’t have a % sign in it to check…


#3

Ah, Thanks Luca I think that might be it. Here is another message I received without the % character.

[code]From: nagios@example.com
Sent: Saturday, January 23, 2010 1:48 PM
To: someyoungguy
Subject: ** PROBLEM Service ALERT: somehost.example.com/check_NRPE_Win_memory
is CRITICAL **

***** Nagios *****

Notification Type: PROBLEM
Host: somehost.example.com
Address: 99.99.99.99
State: CRITICAL

Date/Time: Sat Jan 23 13:47:38 EST 2010

Info: Connection refused by host

Acknowledge this alert: https://example.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&host=
app&service=Check_NRPE_Win_memory[/code]

Now to figure out how to escape the % symbol from script.

Thanks again


#4

Keysor,

Replace the printf with echo:

state_detail() {
        if  "$NAGIOS_SERVICEOUTPUT" ]; then
                printf "Date/Time: $NAGIOS_LONGDATETIME\n\n"
                echo "Additional Info: $NAGIOS_SERVICEOUTPUT\n"
        elif  "$NAGIOS_HOSTOUTPUT" ]; then
                printf "Date/Time: $NAGIOS_LONGDATETIME\n\n"
                echo "Additional Info: $NAGIOS_HOSTOUTPUT\n"
        fi
        printf "\n\n"

}

Allowed me to see the % sign in a PING output.

Thanks!

Jason