Configure Nagios To Use Sendmail

I’ve been setting up Nagios for a client of mine, and one of the issues I faced was configuring Nagios to use sendmail to send notifications. It was attempting to use /bin/mail or /usr/bin/mailx to send email, neither of which were present on the CentOS box.

There wasn’t any information when I Google’d it, so I played around with syntax a bit, got it working, and decided to add it to my blog in-case I forgot about it or anyone else needs it.

The mail command itself is defined in the commands.cfg file, which for me is located at /opt/nagios/etc/objects/commands.cfg, but for you, it’s probably /etc/nagios/objects/commands.cfg.

At the top, you’ll see the two relevant command definitions, ‘notify-host-by-email’ and ‘notify-service-by-email’. Here’s my configuration:

# 'notify-host-by-email' command definition
define command{
    command_name notify-host-by-email
    command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/sbin/sendmail -vt $CONTACTEMAIL$
    }

# 'notify-service-by-email' command definition
define command{
    command_name notify-service-by-email
    command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\n\n***** 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/sbin/sendmail -vt $CONTACTEMAIL$
    }

You’ll note that I didn’t simply swap out the sendmail binary path for the previous path. Sendmail takes it’s arguments slightly differently, so I had to move the subject line inside of the printf statement and tell sendmail to get it’s mail headers from the input.

12 Replies to “Configure Nagios To Use Sendmail”

  1. Worked fine in Ubuntu 16.04.1 LTS with Nagios 4.2.4.
    Thank you.

  2. Thanks, does the trick quite nicely!

  3. It’s appropriate time to make a few plans for the long run and it’s time to be happy.
    Ihave learn this post and if I may I wish to counsel yyou some fascinating things or advice.
    Maybe you can write next articles regarding this article.
    I desire to ledarn even more things approximately it!

  4. you could add “To: $CONTACTEMAIL$ Subject: ……”

    so you don’t get the undesclosed recepients 🙂

  5. Helpful information. Fortunate me I discovered your website by accident,
    and I am stunned why this accident didn’t took
    place earlier! I bookmarked it.

  6. I had to change a little parameter to make it work with my configuration :

    at the end, replace :

    -vt $CONTACTEMAIL$

    by

    -v $CONTACTEMAIL$

    1. By the way, thanks for sharing. You saved me hours.

  7. THANKS!!!

  8. Thank you, worked for me too.

  9. Thank you!

  10. Thank you so much. This helps me a lot.

    I have a question in regards on “TO:” for a header, Is it possible to put that as a header too?

  11. Hi,

    Thank you. Worked like a charm.

    g.

Leave a Reply