#!/bin/sh
#
# nagios-notify-test.sh v2.5
#  by mgothard
# www.NagiosExchange.org
VERSION="2.5"

# Change the following variables as needed

set -a
NAGIOSPATH=/srv/nagios
TEMPFILE=nagios-notify-test.out
CONTACTSFILE=$NAGIOSPATH/etc/contacts.cfg
COMMANDSFILE=$NAGIOSPATH/etc/misccommands.cfg
Contact=$1

NAGIOS_HOSTNAME="TestHostName"
NAGIOS_HOSTALIAS="Test Host Alias"
NAGIOS_HOSTPERFDATA="Test Host performance data"
NAGIOS_HOSTOUTPUT="Test Host Output"
NAGIOS_HOSTADDRESS="1.2.3.4"
NAGIOS_HOSTSTATE="UP"
NAGIOS_HOSTATTEMPT=5
NAGIOS_HOSTNOTIFICATIONNUMBER=8
NAGIOS_HOSTLATENCY="1.234"
NAGIOS_HOSTDURATION="1d 2h 3m 4s"
NAGIOS_HOSTEXECUTIONTIME="5.678"
NAGIOS_HOSTACKAUTHOR="Host ACK Author"
NAGIOS_HOSTACKCOMMENT="This is a test of Nagios notifications"
NAGIOS_HOSTPERCENTCHANGE=1.2
NAGIOS_HOSTACTIONURL="telnet://1.2.3.4/"
NAGIOS_LASTHOSTCHECK="1030000000"
NAGIOS_LASTHOSTDOWN="777777777"
NAGIOS_LASTSERVICECRITICAL="888888888"
NAGIOS_LASTHOSTSTATECHANGE="1020000000"
NAGIOS_SERVICEDESC="TestService"
NAGIOS_SERVICEPERFDATA="Test Service performance data"
NAGIOS_SERVICEOUTPUT="Test Service Output"
NAGIOS_SERVICESTATE="OK"
NAGIOS_SERVICEATTEMPT=6
NAGIOS_SERVICENOTIFICATIONNUMBER=9
NAGIOS_SERVICELATENCY="0.123"
NAGIOS_SERVICEDURATION="5d 6h 7m 8s"
NAGIOS_SERVICEEXECUTIONTIME="4.567"
NAGIOS_SERVICEACKAUTHOR="Service ACK Author"
NAGIOS_SERVICEACKCOMMENT="This is a test of Nagios notifications"
NAGIOS_SERVICEPERCENTCHANGE=2.1
NAGIOS_SERVICENOTES="Test Service Notes"
NAGIOS_LASTSERVICECHECK="1030000000"
NAGIOS_LASTSERVICESTATECHANGE="888888888"
NAGIOS_LONGDATETIME="Sat Jan 1 12:00:00 CST 2007"
NAGIOS_SHORTDATETIME="01-01-2007 12:00:00"
NAGIOS_ADMINEMAIL="test@blankurl.com"
NAGIOS_NOTIFICATIONTYPE="TEST"

# *************END MODIFY*****************

[ $Contact ] || {
	echo -e "nagios-notify-test.sh $VERSION\n\nTest Contact Notification for Nagios 3.x and 2.x\n\nUsage: nagios-notify-test.sh <contact_name>\n\n"
	echo -e " contact_name\n    The contact identifier from $NAGIOSPATH/etc/contacts.cfg\n"
	echo -e "This script is used to manually test the notifications for a particular Nagios"
	echo -e "contact.  It parses the config file for the user contact name and executes the"
	echo -e "notification command(s) with test data."
	exit -1
}

[ -f $CONTACTSFILE ] || {
	echo -e "ERROR: Nagios configuration file $CONTACTSFILE could not be found!"
	exit -1
}

[ -f $TEMPFILE ] && {
	echo -e "ERROR: Temporary file $TEMPFILE already exists!\n\nPlease manually delete or rename this file before continuing."
	exit -1
}

awk '
BEGIN {
	IGNORECASE=1
	InContact=0
	RightContact=0
	FoundContact=0
	InCommand=0
	RightHostCommand=0
	FoundHostCommand=0
	RightServiceCommand=0
	FoundServiceCommand=0
	hostrecords=0
	servicerecords=0
} /^[ \t\f\n\r\v]*define[ \t\f\n\r\v]*contact[ \t\f\n\r\v]*\{/ {
	InContact=1
} /^[ \t\f\n\r\v]*define[ \t\f\n\r\v]*command[ \t\f\n\r\v]*\{/ {
	InCommand=1
} /\}/ {
	InContact=0
	RightContact=0
	InCommand=0
	RightHostCommand=0
	RightServiceCommand=0
} /^[ \t\f\n\r\v]*contact_name[ \t\f\n\r\v]*/ {
	if(ENVIRON["Contact"] == $2) {
		RightContact=1
		FoundContact=1
	}
} /^[ \t\f\n\r\v]*command_name[ \t\f\n\r\v]*/ {
	gsub(/[\n\r]*/,"",$2)
	for(i = 0; i <= hostrecords; i++) {
		if(hostcommandname[i] == $2) {
			RightHostCommand=1
			FoundHostCommand=1
			curhostrecord=i
		}
	}
	for(i = 0; i <= servicerecords; i++) {
		if(servicecommandname[i] == $2) {
			RightServiceCommand=1
			FoundServiceCommand=1
			curservicerecord=i
		}
	}
} /^[ \t\f\n\r\v]*alias\W*/ {
	if(RightContact) {
		$1=""
		alias=gensub(/\W*/,"",1,$0)
	}
} /^[ \t\f\n\r\v]*email\W*/ {
	if(RightContact) {
		$1=""
		email=gensub(/\W*/,"",1,$0)
	}
} /^[ \t\f\n\r\v]*pager[ \t\f\n\r\v]*/ {
	if(RightContact) {
		$1=""
		pager=$0
	}
} /^[ \t\f\n\r\v]*host_notification_commands[ \t\f\n\r\v]*/ {
	if(RightContact) {
		$1=""
		FS=","
		OFS=","
		$0=$0
		FS=" "
		OFS=" "
		for(i = 2; i <= NF; i++)
			hostcommandname[i-2]=gensub(/\W*/,"",1,$i)
		hostrecords=i-3
	}
} /^[ \t\f\n\r\v]*service_notification_commands[ \t\f\n\r\v]*/ {
	if(RightContact) {
		FS=","
		OFS=","
		$1=""
		$0=$0
		FS=" "
		OFS=" "
		for(i = 2; i <= NF; i++)
			servicecommandname[i-2]=gensub(/\W*/,"",1,$i)
		servicerecords=i-3
	}
} /^[ \t\f\n\r\v]*command_line[ \t\f\n\r\v]*/ {
	$1=""
	gsub(/[\n\r]*/,"",$0)
	sub(/[ \t]*/,"",$0)
	if(RightHostCommand) {
		hostcommand[curhostrecord]=$0
	}
	if(RightServiceCommand) {
		servicecommand[curservicerecord]=$0
	}
} END {
	if(FoundContact && FoundHostCommand && FoundServiceCommand) {
		OFS=""
		print "set -a"
		print "NAGIOS_CONTACTALIAS=\"" alias "\""
		print "NAGIOS_CONTACTEMAIL=\"" email "\""
		for(i = 0; i <= hostrecords; i++)
			if(hostcommand[i]) {
				print "echo \"Sending host notification #",i+1," to: $NAGIOS_CONTACTEMAIL\""
				command=gensub(/\$([^\$ ]+)\$/,"$NAGIOS_\\1","g",hostcommand[i])
				print command
			}
			else
				print "echo \"Could not locate command ",hostcommandname[i],"\""
		for(i = 0; i <= servicerecords; i++)
			if(servicecommand[i]) {
				print "echo \"Sending service notification #",i+1," to: $NAGIOS_CONTACTEMAIL\""
				command=gensub(/\$([^\$ ]+)\$/,"$NAGIOS_\\1","g",servicecommand[i])
				print command
			}
			else
				print "echo \"Could not locate command ",servicecommandname[i],"\""
	}
	else {
		OFS=""
		if(!FoundContact)
			print "echo \"Contact ",ENVIRON["Contact"]," was not found in ",ENVIRON["CONTACTSFILE"],"\""
		if(!FoundHostCommand && FoundContact)
			for(i = 0; i <= hostrecords; i++)
				print "echo \"Host command ",hostcommand[i]," was not found in ",ENVIRON["COMMANDSFILE"],"\""
		if(!FoundServiceCommand && FoundContact)
			for(i = 0; i <= servicerecords; i++)
				print "echo \"Service command ",servicecommand[i]," was not found in ",ENVIRON["COMMANDSFILE"],"\""
		print "echo -e \"\nExiting\""
		exit
	}
}' $CONTACTSFILE $COMMANDSFILE >$TEMPFILE && {
	chmod +x $TEMPFILE
	./$TEMPFILE
}

[ -f $TEMPFILE ] && rm $TEMPFILE
