#!/usr/local/bin/bash

#  $1 = hostname 
#  $2 = hostackcomment/serviceackcomment
#  $3 = hostackauthor/serviceackauthor
#  $4 = hostoutput
#  $5 = notificationtype
#  $6 = servicedesc (if a service)


hostname=$1
ackcomment=$2
ackauthor=$3
output=$4
notificationtype=`echo $5 | /usr/bin/awk '{print $1}'`
servicedesc=$6

queue="QUEUENAME"
debug=true

if [ "$servicedesc" != "" ]
then
	service="true"
fi

if [ "$notificationtype" != "ACKNOWLEDGEMENT" ]
then
	echo "Not an ACKNOWLEDGEMENT : $notificationtype"
	exit
fi

now=`date +%s`
commandfile='/apps/nagios/var/rw/nagios.cmd'

if [ "$service" = "true" ]
then
	subject="$hostname $servicedesc"
	rt_result=`/apps/rt/bin/rt create -t ticket set status=new subject="$subject" owner="$ackauthor" queue="$queue" requestors="$ackauthor" text="$ackcomment $output"`
	ticketid=`echo $rt_result | /usr/bin/awk '{print $3}'`
	## Add comment to link to rt
	/usr/bin/printf "[%s] ADD_SVC_COMMENT;$hostname;$servicedesc;1;$ackauthor;<a href=\"http://RTSERVER/Ticket/Display.html?id=$ticketid\" TARGET='_blank'>Ticket #$ticketid Raised in RT by $ackauthor<\/a>\n" $now > $commandfile
else
	subject=$hostname
	rt_result=`/apps/rt/bin/rt create -t ticket set status=new subject="$subject" owner="$ackauthor" queue="$queue" requestors="$ackauthor" text="$ackcomment $output"`
        ticketid=`echo $rt_result | /usr/bin/awk '{print $3}'`
	## Add comment to link to rt
	/usr/bin/printf "[%s] ADD_HOST_COMMENT;$hostname;1;$ackauthor;<a href=\"http://RTSERVER/Ticket/Display.html?id=$ticketid\" TARGET='_blank'>Ticket #$ticketid Raised in RT by $ackauthor<\/a>\n" $now > $commandfile
fi

if [ "$debug" = "true" ]
then
	echo "Subject = $subject"
	echo "Queue = $queue"
	echo "Requestor = $contactemail"
	echo "Owner = $ackauthor"
	echo "Text = $ackcomment"
	echo "Notification Type = $notificationtype"
	echo "Output = $output"
	echo "======"
	echo "Ticket ID = $ticketid"
fi
