Different Emails For Different Alerts

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Different Emails For Different Alerts

Post by abrist »

What you want to do should work.
jbennett wrote:I think you're still trying to help me with the method that hhlodge suggested, which isn't what I'm trying to do in this instance.
I was going to take you down the road of creating new notification handlers, but it looks like you will be using the xi handler and notification templates - which do not use notify-*-by-email handlers, but instead uses the xi_*_notification_handlers.
You will need to include/remap the $SERVICENOTES$ macro in the xi_service_notification_handler command. Add the following to the end:

Code: Select all

--servicenotes="$SERVICENOTES$"
And then you can use the %servicenotes% macro in the template.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
hhlodge
Posts: 206
Joined: Tue Mar 08, 2011 2:13 pm

Re: Different Emails For Different Alerts

Post by hhlodge »

On an aside, this exercise has gotten me to pursue something I've wanted Nagios to do for years and that's to have all the recipients of a notification be in the To: header so people can "reply all" to issues, yet each notification from Nagios only go the one intended recipient ($CONTACTEMAIL) so they don't get multiple messages and their Respond URL is unique. I now have this working well by constructing the message myself with a similar called script I suggested here, looping through the contacts in $NOTIFICATIONRECIPIENTS$ and then piping it all to sendmail/Postfix directly. My last challenge is to parse the config file to get the email field - which goes in the To: header - mapped to the contact_name. I can post the final notification handler and script when I have that working if anyone is interested.
- Kyle
hhlodge
Posts: 206
Joined: Tue Mar 08, 2011 2:13 pm

Re: Different Emails For Different Alerts

Post by hhlodge »

I am running into trouble passing arguments from Nagios to a Korn shell script. Can someone shed some light on these PROBLEM[0-9] values? Is it some sort of limit?

Code: Select all

/usr/local/scripts/notify-custom-service-by-email.ksh "$NOTIFICATIONTYPE$" "$SERVICEDESC$" "$SERVICEGROUPNAMES$" "$HOSTALIASHOSTADDRESS$" "$SERVICESTATE$" "$LONGDATETIME$" "$NOTIFICATIONRECIPIENTS$" "$SERVICEOUTPUT$" "$LONGSERVICEOUTPUT$" "$SERVICEACKAUTHOR$" "$NOTIFICATIONCOMMENT$" "$SERVICEACTIONURL$" "$NOTIFICATIONTYPE$" "$HOSTNAME$" "$SERVICEDESC$" "$CONTACTEMAIL$"

NOTIFICATIONTYPE="$1"
SERVICEDESC="$2"
SERVICEGROUPNAMES="$3"
HOSTALIASHOSTADDRESS="$4"
SERVICESTATE="$5"
LONGDATETIME="$6"
NOTIFICATIONRECIPIENTS="$7"
SERVICEOUTPUT="$8"
LONGSERVICEOUTPUT="$9"
SERVICEACKAUTHOR="$10"
NOTIFICATIONCOMMENT="$11"
SERVICEACTIONURL="$12"
NOTIFICATIONTYPE="$13"
HOSTNAME="$14"
SERVICEDESC="$15"
CONTACTEMAIL="$16"

echo "NOTIFICATIONTYPE="$1" SERVICEDESC="$2" SERVICEGROUPNAMES="$3" HOSTALIASHOSTADDRESS="$4" SERVICESTATE="$5" LONGDATETIME="$6" NOTIFICATIONRECIPIENTS="$7" SERVICEOUTPUT="$8" LONGSERVICEOUTPUT="$9" SERVICEACKAUTHOR="$10" NOTIFICATIONCOMMENT="$11" SERVICEACTIONURL="$12" NOTIFICATIONTYPE="$13" HOSTNAME="$14" SERVICEDESC="$15" CONTACTEMAIL="$16"" >> /tmp/nagargs

[root@sigitmon-vm ~]# cat /tmp/nagargs 
NOTIFICATIONTYPE=PROBLEM SERVICEDESC=Check Badness SERVICEGROUPNAMES= HOSTALIASHOSTADDRESS=$ SERVICESTATE=WARNING LONGDATETIME=Mon Feb 24 09:12:37 EST 2014 NOTIFICATIONRECIPIENTS=hhlodge,ktucker SERVICEOUTPUT=A bit of a problem here. LONGSERVICEOUTPUT= SERVICEACKAUTHOR=PROBLEM0 NOTIFICATIONCOMMENT=PROBLEM1 SERVICEACTIONURL=PROBLEM2 NOTIFICATIONTYPE=PROBLEM3 HOSTNAME=PROBLEM4 SERVICEDESC=PROBLEM5 CONTACTEMAIL=PROBLEM6
- Kyle
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Different Emails For Different Alerts

Post by abrist »

I believe (someone correct me if I am wrong) ksh maxes out at 10 passed parameters ($0-$9). In bash, you can wrap the parameter number in curly braces for parameters over 10 - ie. ${13}.
I think you may have to use "shift" to get more parameters in ksh.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
hhlodge
Posts: 206
Joined: Tue Mar 08, 2011 2:13 pm

Re: Different Emails For Different Alerts

Post by hhlodge »

Not what I am seeing.

Code: Select all

# cat args.ksh 
#!/bin/ksh
echo "Arg1 is: $1"
echo "Arg2 is: $2"
echo "Arg3 is: $3"
echo "Arg4 is: $4"
echo "Arg5 is: $5"
echo "Arg6 is: $6"
echo "Arg7 is: $7"
echo "Arg8 is: $8"
echo "Arg9 is: $9"
echo "Arg10 is: $10"
echo "Arg11 is: $11"
echo "Arg12 is: $12"
echo "Arg13 is: $13"
echo "Arg14 is: $14"
# ./args.ksh 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Arg1 is: 1
Arg2 is: 2
Arg3 is: 3
Arg4 is: 4
Arg5 is: 5
Arg6 is: 6
Arg7 is: 7
Arg8 is: 8
Arg9 is: 9
Arg10 is: 10
Arg11 is: 11
Arg12 is: 12
Arg13 is: 13
Arg14 is: 14
- Kyle
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Different Emails For Different Alerts

Post by abrist »

Fair enough. How are you populating the macros in your testing? Using nagios env vars, or did you just hand export these env vars?
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
hhlodge
Posts: 206
Joined: Tue Mar 08, 2011 2:13 pm

Re: Different Emails For Different Alerts

Post by hhlodge »

Scratch that, I was tricked! :-)

Code: Select all

# ./args.ksh "one word" "two more words" "how many here"
Arg1 is: one word
Arg2 is: two more words
Arg3 is: how many here
Arg4 is: 
Arg5 is: 
Arg6 is: 
Arg7 is: 
Arg8 is: 
Arg9 is: 
Arg10 is: one word0
Arg11 is: one word1
Arg12 is: one word2
Arg13 is: one word3
Arg14 is: one word4
- Kyle
hhlodge
Posts: 206
Joined: Tue Mar 08, 2011 2:13 pm

Re: Different Emails For Different Alerts

Post by hhlodge »

I have this working well now but cannot get $SERVICEACTIONURL$ macro to expand. All documents I see say it should be available in a service notification. ??? Here's the command.

Code: Select all

/usr/local/scripts/notify-custom-service-by-email.ksh "$NOTIFICATIONTYPE$" "$SERVICEDESC$" "$SERVICEGROUPNAMES$" "$HOSTALIAS$" "$HOSTADDRESS$" "$SERVICESTATE$" "$LONGDATETIME$" "$NOTIFICATIONRECIPIENTS$" "$SERVICEOUTPUT$" "$LONGSERVICEOUTPUT$" "$SERVICEACKAUTHOR$" "$NOTIFICATIONCOMMENT$" "$SERVICEACTIONURL$" "$NOTIFICATIONTYPE$" "$HOSTNAME$" "$SERVICEDESC$" "$CONTACTEMAIL$"
And here is the script.

Code: Select all

#!/bin/ksh
# Custom Nagios service notifications

MSGFILE=/tmp/msgfile.$$

NOTIFICATIONTYPE="${1}"
SERVICEDESC="${2}"
SERVICEGROUPNAMES="${3}"
HOSTALIAS="${4}"
HOSTADDRESS="${5}"
SERVICESTATE="${6}"
LONGDATETIME="${7}"
NOTIFICATIONRECIPIENTS="${8}"
SERVICEOUTPUT="${9}"
LONGSERVICEOUTPUT="${10}"
SERVICEACKAUTHOR="${11}"
NOTIFICATIONCOMMENT="${12}"
SERVICEACTIONURL="${13}"
NOTIFICATIONTYPE="${14}"
HOSTNAME="${15}"
SERVICEDESC="${16}"
CONTACTEMAIL="${17}"

#echo "Args are: $*" > /tmp/nagargs
#echo "NOTIFICATIONTYPE="${1}" SERVICEDESC="${2}" SERVICEGROUPNAMES="${3}" HOSTALIAS="${4}" HOSTADDRESS="${5}" SERVICESTATE="${6}" LONGDATETIME="${7}" NOTIFICATIONRECIPIENTS="${8}" SERVICEOUTPUT="${9}" LONGSERVICEOUTPUT="${10}" SERVICEACKAUTHOR="${11}" NOTIFICATIONCOMMENT="${12}" SERVICEACTIONURL="${13}" NOTIFICATIONTYPE="${14}" HOSTNAME="${15}" SERVICEDESC="${16}" CONTACTEMAIL="${17}"" >> /tmp/nagargs

for USER in $(echo $NOTIFICATIONRECIPIENTS | /bin/sed -e "s/,/ /g")
do
	ADDR=$(/bin/egrep "contact_name$"[^I]"|email"[^I]"" /usr/local/nagios/etc/contacts.cfg  | /bin/awk ' { print $2 } ' | /bin/awk 'NR % 2 == 1 { o=$0 ; next } { print o ":" $0 }' | /bin/grep "^${USER}:" | cut -d: -f2)
	ALLADDR="$ALLADDR $ADDR"
done

MAILTO=$(echo "$ALLADDR" | /bin/sed -e "s/^ //" -e s"/ /,/g")

if [ "$SERVICEACKAUTHOR" != "" ]
then
	SERVICEACKAUTHORSTR="\nAcknowledged by: $SERVICEACKAUTHOR\n"
fi

if [ "$NOTIFICATIONCOMMENT" != "" ]
then
	NOTIFICATIONCOMMENTSTR="Comment: $NOTIFICATIONCOMMENT\n"
fi

echo -e "To: $MAILTO" > $MSGFILE
echo -e "Subject: ${NOTIFICATIONTYPE} Service Alert: ${HOSTNAME}/${SERVICEDESC} is ${SERVICESTATE}" >> $MSGFILE
/usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: ${NOTIFICATIONTYPE}\n\nService: ${SERVICEDESC}\nService Groups: ${SERVICEGROUPNAMES}\nHost: ${HOSTALIAS}\nAddress: ${HOSTADDRESS}\nState: ${SERVICESTATE}\n\nDate/Time: ${LONGDATETIME}\nNotified: ${NOTIFICATIONRECIPIENTS}\n\nAdditional Info: ${SERVICEOUTPUT}\n${LONGSERVICEOUTPUT}${SERVICEACKAUTHORSTR}${NOTIFICATIONCOMMENTSTR}\nURL: ${SERVICEACTIONURL}" >> ${MSGFILE}
/bin/cat $MSGFILE | /usr/sbin/sendmail $CONTACTEMAIL
/bin/rm -f $MSGFILE
- Kyle
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Different Emails For Different Alerts

Post by abrist »

A few questions:
1) I presume that you have defined the $SERVICEACTIONURL$ on the object?
2) Could be a shell escaping issue, can you try setting the macro to just alphanumerical characters for a test?
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
jbennett
Posts: 522
Joined: Mon Apr 16, 2012 3:00 pm

Re: Different Emails For Different Alerts

Post by jbennett »

abrist wrote:What you want to do should work.
jbennett wrote:I think you're still trying to help me with the method that hhlodge suggested, which isn't what I'm trying to do in this instance.
I was going to take you down the road of creating new notification handlers, but it looks like you will be using the xi handler and notification templates - which do not use notify-*-by-email handlers, but instead uses the xi_*_notification_handlers.
You will need to include/remap the $SERVICENOTES$ macro in the xi_service_notification_handler command. Add the following to the end:

Code: Select all

--servicenotes="$SERVICENOTES$"
And then you can use the %servicenotes% macro in the template.
This worked perfectly!

Just one thing (and I only realized this once I implemented it): is it possible to nest the macros?

For instance in the service notes field, have hte following: http://%hostaddress%:8080/mainmenu.html.

Currently, in the service notes field I just have :8080/mainmenu.html and it works great.

Nesting the %hostaddress% information would allow me to jsut call the %servicenotes% field in the email template, making it more flexible.

Thoughts?
Locked