Is it possible to have 2 notifications template

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
cesar.garza
Posts: 80
Joined: Mon Aug 19, 2013 3:14 pm

Re: Is it possible to have 2 notifications template

Post by cesar.garza »

tmcdonald wrote:
cbeattie wrote:If it bugs you enough, there are two things I can think of. First, enable your e-mail contact to receive unknown (u) notifications and r, but not w or c. I get far, far fewer unknown states than w or c, and maybe you'll still get all the r.
cesar.garza wrote:Thanks for your help and i tried with notification option ""u"" only but it didn't work.
Did you try with "u" AND "r" like he suggested?

Or have you tried cbeattie's previous suggestion of a custom notification command?

I havent tried cbeattie's suggestion yet because i am not fully understand. I tried just ""u"" with r option and it didn't work. On some website,i read that if there is no option (w and c) then you wont get the r too.
cbeattie
Posts: 19
Joined: Fri Oct 04, 2013 7:12 am

Re: Is it possible to have 2 notifications template

Post by cbeattie »

cesar.garza wrote: I haven't completely understand this one. should i have only one templates without/with ""r"" ? and then add above command to command.cfg file.
The contact needs to have the w, c, and r. The idea is that Nagios is satisfied the contact will receive everything. Then the notification command (not Nagios) filters out the w and c, and only forwards the r. The way I wrote it won't work, though, since "if" is built-in to bash.

What sreinhardt suggested is to replace the printf call in the notification command, and have the command call a shell script instead (and pass it the necessary parameters). The script then checks if it's being called for a recovery notification, and if so, then it sends out the e-mail notification. The script just ignores warnings and criticals, and doesn't send e-mail for them.
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Is it possible to have 2 notifications template

Post by sreinhardt »

I was actually stating that I would wrap the entire notification command in a script, or as much as possible as nothing is evaluated other than variables being expanded. If you just handle the "if" logic in a bash script, we're both right! What are you confused on cesar?
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
cesar.garza
Posts: 80
Joined: Mon Aug 19, 2013 3:14 pm

Re: Is it possible to have 2 notifications template

Post by cesar.garza »

cbeattie wrote:I had another idea which (if it works) is certainly easier and better than changing the source code. It might be possible to use a custom notification command instead. Something along these lines:

Code: Select all

define command {
    command_name  recovery_only
    command_line  if [ "$NOTIFICATIONTYPE$" -eq "RECOVERY" ]; then /bin/printf your_command | /bin/mail your_command; fi
}
Then change your contact's service_notification_options to w,c,r and the service_notification_command to recovery_only (or whatever you called it).
I have tried this suggestion and it didn't work. Not sure who to work on cbeattie second suggetion to delete the code part form the printf.
cesar.garza
Posts: 80
Joined: Mon Aug 19, 2013 3:14 pm

Re: Is it possible to have 2 notifications template

Post by cesar.garza »

cbeattie wrote:
cesar.garza wrote: I haven't completely understand this one. should i have only one templates without/with ""r"" ? and then add above command to command.cfg file.
The contact needs to have the w, c, and r. The idea is that Nagios is satisfied the contact will receive everything. Then the notification command (not Nagios) filters out the w and c, and only forwards the r. The way I wrote it won't work, though, since "if" is built-in to bash.

What sreinhardt suggested is to replace the printf call in the notification command, and have the command call a shell script instead (and pass it the necessary parameters). The script then checks if it's being called for a recovery notification, and if so, then it sends out the e-mail notification. The script just ignores warnings and criticals, and doesn't send e-mail for them.
It sounds good to me but so far what i understand is that make the shell script with this

Code: Select all

 if [ "$NOTIFICATIONTYPE$" -eq "RECOVERY" ]; then /bin/printf your_command | /bin/mail your_command; fi 
and pass the script name and path in the command.cfg
cesar.garza
Posts: 80
Joined: Mon Aug 19, 2013 3:14 pm

Re: Is it possible to have 2 notifications template

Post by cesar.garza »

I have make the script with command.cfg configuratino. please have a look and let me know if i am going in the wrong direction.
Attachments
recovery.sh
(718 Bytes) Downloaded 271 times
cbeattie
Posts: 19
Joined: Fri Oct 04, 2013 7:12 am

Re: Is it possible to have 2 notifications template

Post by cbeattie »

I think you're on the right track. You have to pass the Nagios macros as parameters to the script. So, I think you'll end up with something along the lines of:

Code: Select all

define command{
        command_name    notify-service-by-email-test
        command_line    /usr/local/nagios/libexec/recovery.sh $NOTIFICATIONTYPE$ $SERVICEDESC$ ...etc...
        }
And then in the script, to use the parameters you passed, something along the lines of:

Code: Select all

#! /bin/sh
notificationtype=$1
servicedesc=$2
if 
[ "${notificationtype}" == "RECOVERY" ]; then /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: ${notificationtype}\n\nService: ${servicedesc}\nHost ...etc...

slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Is it possible to have 2 notifications template

Post by slansing »

Thank's cbeattie!
cesar.garza
Posts: 80
Joined: Mon Aug 19, 2013 3:14 pm

Re: Is it possible to have 2 notifications template

Post by cesar.garza »

I tried the cbeattie suggest and it didn't work. what i did is make a script and put the command in the command.cfg like this and also edited the template file for notification option. I got the call but not recovery email.

Code: Select all

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email-test
        command_line    /usr/local/nagios/libexec/messageone_recovery $NOTIFICATIONTYPE$ $SERVICEDESC$ $HOSTALIAS$ $HOSTADDRESS$ $SERVICESTATE$ $LONGDATETIME$ $SERVICEOUTPUT$ $SERVICEACTIONURL$ $CONTACTEMAIL$
        }

Code: Select all

#! /bin/sh

NOTIFICATIONTYPE=$1
SERVICEDESC=$2
HOSTALIAS=$3
HOSTADDRESS=$4
SERVICESTATE=$5
LONGDATETIME=$6
SERVICEOUTPUT=$7
SERVICEACTIONURL=$8
CONTACTEMAIL=$9


if
        [ "${NOTIFICATIONTYPE}" == "RECOVERY" ]; then /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: ${NOTIFICATIONTYPE}\n\nService: ${SERVICEDESC}\nHost: ${HOSTALIAS}\nAddress: ${HOSTADDRESS}\nState: ${SERVICESTATE}\n\nDate/Time: ${LONGDATETIME}\n\nAdditional Info:\n\n${SERVICEOUTPUT}\n\nURL: ${SERVICEACTIONURL}$\n" | /usr/bin/mail -s "** ${NOTIFICATIONTYPE} Service Alert: ${HOSTALIAS}/${SERVICEDESC} is ${SERVICESTATE} **" ${CONTACTEMAIL};

fi
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: Is it possible to have 2 notifications template

Post by slansing »

We're looking into this and will get back to you.
Locked