Category Archives: Nagios

Nagios

Nagios alert to SMS message via Kannel

Another very useful script!

I use the excellent Kannel SMS gateway for sending and receiving of SMS messages and this script allows Nagios to send alerts via Kannel.

The telephone numbers are currently hard coded into the script but it would be fairly trivial to pass those in as part of the Nagios command.

The host command for Nagios should look like this:

define command {
                command_name                          notify-host-by-sms
                command_line                          /usr/lib/nagios/plugins/sms.sh host
}

The service command for Nagios should look like this:

define command {
                command_name                          notify-service-by-sms
                command_line                          /usr/lib/nagios/plugins/sms.sh service
}

Here’s the script:

#!/bin/bash

#define command {
#                command_name                          notify-host-by-sms
#                command_line                          /usr/lib/nagios/plugins/sms.sh host
#}

#define command {
#                command_name                          notify-service-by-sms
#                command_line                          /usr/lib/nagios/plugins/sms.sh service
#}

# Location of the logfile
LOGFILE=/var/log/nagios3/sms.log

MESSAGE="Nagios Alert"

# Determine the type of message.
case "$1" in
	'host')
	case "$NAGIOS_NOTIFICATIONTYPE" in
		'ACKNOWLEDGEMENT')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nHost '$NAGIOS_HOSTALIAS' is $NAGIOS_HOSTSTATE\nI: $NAGIOS_HOSTOUTPUT\nT: $NAGIOS_SHORTDATETIME\nA: $NAGIOS_HOSTACKAUTHOR R: $NAGIOS_HOSTACKCOMMENT"
		;;
		'PROBLEM')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nHost '$NAGIOS_HOSTALIAS' is $NAGIOS_HOSTSTATE\nI: $NAGIOS_HOSTOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
		'FLAPPINGSTART')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nHost '$NAGIOS_HOSTALIAS' is $NAGIOS_HOSTSTATE\nI: $NAGIOS_HOSTOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
		'FLAPPINGSTOP')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nHost '$NAGIOS_HOSTALIAS' is $NAGIOS_HOSTSTATE\nI: $NAGIOS_HOSTOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
		'RECOVERY')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nHost '$NAGIOS_HOSTALIAS' is $NAGIOS_HOSTSTATE\nI: $NAGIOS_HOSTOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
	esac
	;;
	'service')
	case "$NAGIOS_NOTIFICATIONTYPE" in
		'ACKNOWLEDGEMENT')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nH: $NAGIOS_HOSTNAME\nS: $NAGIOS_SERVICEDESC\nV: $NAGIOS_SERVICESTATE\nI: $NAGIOS_SERVICEOUTPUT\nT: $NAGIOS_SHORTDATETIME\nA: $NAGIOS_SERVICEACKAUTHOR R: $NAGIOS_SERVICEACKCOMMENT"
		;;
		'PROBLEM')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \\nH: $NAGIOS_HOSTNAME\\nS: $NAGIOS_SERVICEDESC\\nV: $NAGIOS_SERVICESTATE\\nI: $NAGIOS_SERVICEOUTPUT\\nT: $NAGIOS_SHORTDATETIME"
		;;
		'FLAPPINGSTART')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nH: $NAGIOS_HOSTNAME\nS: $NAGIOS_SERVICEDESC\nV: $NAGIOS_SERVICESTATE\nI: $NAGIOS_SERVICEOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
		'FLAPPINGSTOP')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nH: $NAGIOS_HOSTNAME\nS: $NAGIOS_SERVICEDESC\nV: $NAGIOS_SERVICESTATE\nI: $NAGIOS_SERVICEOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
		'RECOVERY')
		MESSAGE="$NAGIOS_NOTIFICATIONTYPE \nH: $NAGIOS_HOSTNAME\nS: $NAGIOS_SERVICEDESC\nV: $NAGIOS_SERVICESTATE\nI: $NAGIOS_SERVICEOUTPUT\nT: $NAGIOS_SHORTDATETIME"
		;;
	esac
	;;
esac

# Write to log file.
echo "*********************************************************" >> $LOGFILE
echo -e $MESSAGE >> $LOGFILE
echo "*********************************************************" >> $LOGFILE
echo -e "\n" >> $LOGFILE

# Send the alert.

wget -O- "http://ahspogoplug1.aceshigh.local:13003/cgi-bin/sendsms?username=test&password=test&from=XXXXXXXXXXX&to=XXXXXXXXXXX&text=$MESSAGE"
wget -O- "http://ahspogoplug1.aceshigh.local:13003/cgi-bin/sendsms?username=test&password=test&from=XXXXXXXXXXX&to=XXXXXXXXXXX&text=$MESSAGE"

# Unset the variables used.
unset $MESSAGE# Exit the program.
exit

Alternatively you can download it be clicking sms.sh

Hope you find it useful!

Thanks for reading,

Martyn Wendon