Monday, September 26, 2011

Changing the from e-mail address in Nagios

This is going to be another short one.  I recently had a need to change the FROM address for e-mail from our Nagios installation. E-mail was coming from nagios@host.domain.com, which is non-route-able  from outside our network.

The change is very simple. Change the two command lines in your Nagios commands.cfg dealing with notify by e-mail. The command names are "notify-host-by-email" and "notify-service-by-email".

By default these lines read:
 /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/        Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

and

/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$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

To change  the from address you append " -- -f nagios@domain.com", that is without the quotes of course.  So the new lines look like:

/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/        Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ -- -f nagios@domain.com

and

/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$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$  -- -f nagios@domain.com

To explain it a little what we appended was a space followed by two dashes, which forces mail to pass those along to sendmail. Send mails sees the addition of  a dash followed by the letter f and then another space and the e-mail address you want to send from.

On my system this is an alias for myself so that all replies come to my inbox.

All of this works on CentOS which is what I'm currently running.  It should work on other distributions as well, but I haven't the time to verify that.

5 comments:

Anonymous said...

that didn't work for me...
===========================
but this worked...

# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** System Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mailx -r myemailaddres@domain.com -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}

Anonymous said...

Great, post of Anonymous above worked also for me.

Anonymous said...

Excellent, thanks. Worked for me. Nagios on Ubuntu.

zzapper said...

yep worked for me out of the box centos 5 thnx

Anonymous said...

None of that is what I see at all, this is what I see:

#
# Generic Notification Commands
#
#
define command {
command_name host-notify-by-email
command_line /usr/bin/perl $USER1$/notify-by-email.pl \
--template=$USER4$/etc/mail-templates/notify-by-email.host.tpl \
-o NOTIFICATIONTYPE='$NOTIFICATIONTYPE$' \
-o NOTIFICATIONCOMMENT='$NOTIFICATIONCOMMENT$' \
-o HOSTNAME='$HOSTNAME$' \
-o HOSTSTATE='$HOSTSTATE$' \
-o CONTACTEMAIL='$CONTACTEMAIL$' \
-o HOSTADDRESS='$HOSTADDRESS$' \
-o SHORTDATETIME='$SHORTDATETIME$' \
-o HOSTOUTPUT='$HOSTOUTPUT$' \
-o LONGHOSTOUTPUT='$LONGHOSTOUTPUT$' \
-o HOSTPERFDATA='$HOSTPERFDATA$' \
-o ACKAUTHOR='$HOSTACKAUTHOR$' \
-o ACKCOMMENT='$HOSTACKCOMMENT$' \
-o DURATION='$HOSTDURATION$' \
-o HOSTEVENTID='$HOSTEVENTID$' \
-o LASTHOSTEVENTID='$LASTHOSTEVENTID$'
}

define command {
command_name service-notify-by-email
command_line /usr/bin/perl $USER1$/notify-by-email.pl \
--template=$USER4$/etc/mail-templates/notify-by-email.service.tpl \
-o NOTIFICATIONTYPE='$NOTIFICATIONTYPE$' \
-o NOTIFICATIONCOMMENT='$NOTIFICATIONCOMMENT$' \
-o HOSTNAME='$HOSTNAME$' \
-o HOSTSTATE='$HOSTSTATE$' \
-o CONTACTEMAIL='$CONTACTEMAIL$' \
-o HOSTADDRESS='$HOSTADDRESS$' \
-o SHORTDATETIME='$SHORTDATETIME$' \
-o SERVICEDESC='$SERVICEDESC$' \
-o SERVICESTATE='$SERVICESTATE$' \
-o SERVICEOUTPUT='$SERVICEOUTPUT$' \
-o LONGSERVICEOUTPUT='$LONGSERVICEOUTPUT$' \
-o SERVICEPERFDATA='$SERVICEPERFDATA$' \
-o ACKAUTHOR='$SERVICEACKAUTHOR$' \
-o ACKCOMMENT='$SERVICEACKCOMMENT$' \
-o DURATION='$SERVICEDURATION$' \
-o SERVICEEVENTID='$SERVICEEVENTID$' \
-o LASTSERVICEEVENTID='$LASTSERVICEEVENTID$'
}


-----------------


What do I need to change?