Page 1 of 1

Need URL in the email alert

Posted: Tue Nov 05, 2013 5:11 pm
by cesar.garza
Hey Gurus,
I need a small help to figure it out that how can i setup a nagios individual service URL in the email notification. Right now i am getting email alert with XXXX information but i need the email notification with the URL that take you straight to nagios service information page.
Thanks

Code: Select all

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line    /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" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 11:30 am
by abrist
You will have to include the url with the macros in your message body:

Code: Select all

http://<nagios server ip>/nagios/cgi-bin/extinfo.cgi?type=2&host=$HOSTNAME$&service=$SERVICEDESC$
Obviously change <nagios server ip> to your server's ip.

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 1:35 pm
by cesar.garza
Thanks abrist for your reply, but i am pretty new in nagios world so quite slow to understand. The suggestion you gave me, where i need to add that. my configuration is something like that... and do i also need to add anything in the (notification of email) too ?..

Service.cfg

Code: Select all

define service {
        hostgroup_name                  api,EPP,generic-api,generic-svc,PCE,sam
        service_description             Disk Space
        check_command                   check_nrpe_disk_windows
        use                             generic-service
        notification_interval           0
        }
command.cfg

Code: Select all

define command{
        command_name check_nrpe_disk_windows
        command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c alias_disk -a 20% 10%
        }

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 2:24 pm
by scottwilkerson
You would need to add it to the notify-service-by-email command definition (which creates the email)

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 2:53 pm
by abrist
Edit you service command:

Code: Select all

/usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n\nRapid Response:\n$$(echo "http://<nagios server ip>/nagios/cgi-bin/extinfo.cgi?type=2&host=$HOSTNAME$&service=$SERVICEDESC$" | sed 's/ /%20/g')" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
A little explanation of the addition:

Code: Select all

$$(echo "http://<nagios server ip>/nagios/cgi-bin/extinfo.cgi?type=2&host=$HOSTNAME$&service=$SERVICEDESC$" | sed 's/ /%20/g')
Two $$ because we need to escape the $ used for nagios macros.
The reason to echo the url through sed is so that we can replace any spaces in the url (spaces in the service or hostname) with the url encoded %20. This code should add a link to the bottom of your email. Make sure to replace <nagios server ip> with your server's actual ip and to change http to https if needed (like if you force https through rewrite rules).

EDIT: The sed above only url encodes spaces. If your object names have any other characters that need to be url encoded, you will need to extend the sed logic to compensate for them.

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 3:38 pm
by cesar.garza
I edited """notify-service-by-email""" but it is coming as BLANK. I have attached the screenshot.

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 3:46 pm
by abrist
Try running it from the cli. It will have a bunch of $ in the fields, but we should see the url in the email. Replace <your email> with your address and try running this monster one liner from the cli::

Code: Select all

/usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n\nRapid Response:\n$$(echo "http://10.5.56.62/nagios/cgi-bin/extinfo.cgi?type=2&host=$HOSTNAME$&service=$SERVICEDESC$" | sed 's/ /%20/g')" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" <your email>

Re: Need URL in the email alert

Posted: Wed Nov 06, 2013 8:49 pm
by cesar.garza
Thanks for your help. It worked out.