Page 1 of 1

Modifying/Editing Notifications E-mail Body

PostPosted: Tue Oct 21, 2014 1:33 pm
by dcharles
Hello

I was wondering if it was possible to modify the notification emails that Nagios sends out. What we'd like to do specifically, is add a "Location" line in addition to the standard information that Nagios sends out with its notification.

Here is an example:

***** Nagios *****

Notification Type: RECOVERY

Service: rdp
Host: <hostname>
Address: 192.168.105.124
State: OK
Location: NYC Datacenter

Date/Time: Tue Oct 21 08:10:07 EDT 2014

Additional Info:

TCP OK - 0.002 second response time on port 3389

The line in bold is something we'd like to add. I'm aware that involves editing the /usr/local/nagios/etc/objects/commands,cfg file, however I am not sure what the exact syntax would be, or if this is even possible.

Thanks to anyone that can help!

It is greatly appreciated.

Re: Modifying/Editing Notifications E-mail Body

PostPosted: Tue Oct 21, 2014 2:22 pm
by abrist
Edit the notification handler commands to include the location:
Code: Select all
define command {
       command_name                             notify-host-by-email
       command_line                             /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n\nLocation: NYC Datacenter\n\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}

Code: Select all
define command {
       command_name                             notify-service-by-email
       command_line                             /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\nLocation: NYC Datacenter\n\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

Re: Modifying/Editing Notifications E-mail Body

PostPosted: Tue Oct 21, 2014 3:37 pm
by dcharles
Thank You so much for the quick response. I think I have a better understanding of the syntax based on my reading of macros and the code in your response.

The problem, however, which I forgot to mention in my original post, is that our hosts are spread across several different locations. So I'd need to add in a variable for the Location name, possibly using the $HOSTGROUPNAME$ macro. I will probably need to perform this by creating new host groups and adding each desired host to its respective group.

So the part I'd need to add would look something like this, I'm guessing:

$\n\nLocation: $HOSTGROUPNAME$

Please correct me if I'm wrong.

Also, if a host is a member of multiple host groups, this raises another question, though: which host group does Nagios display when using the $HOSTGROUPNAME$ macro? I'm assuming it's the very first one listed in the host definition?

Thanks again for your help.

Re: Modifying/Editing Notifications E-mail Body

PostPosted: Wed Oct 22, 2014 3:15 pm
by abrist
dcharles wrote:which host group does Nagios display when using the $HOSTGROUPNAME$ macro?

The first defined in the directive. You could also use free variable macros to do this:
http://nagios.sourceforge.net/docs/3_0/ ... tvars.html

Re: Modifying/Editing Notifications E-mail Body

PostPosted: Thu Oct 23, 2014 11:25 am
by dcharles
Thanks sooo much for your help! This worked perfectly!

Since we are using Check_MK in conjunction with Nagios, I had to add a custom config in order for the parameter to show up for our hosts:

extra_host_conf["_location"] = [
( "NYC-Datacenter", ["loc-nyc-datacent"], ALL_HOSTS),
( "Toronto", ["loc-toronto"], ALL_HOSTS),
]

With this, I can add variables for any region we have our hosts located. Hopefully this can help anyone else who would like to add custom content to the body of their Nagios email alerts.

Thanks again for your help, it is much much appreciated!

Re: Modifying/Editing Notifications E-mail Body

PostPosted: Thu Oct 23, 2014 11:32 am
by eloyd
Custom macros/variables are the best thing since Nagios itself. You can use them to specify anything host or service specific (or contact, for that matter) that you want to do that doesn't already have a variable. Just remember - if you use them for a host/service/contact that doesn't have it defined, odd things may result.