Configuring e-Mail Notifications in Nagios Core…

by Andy Grogan on February 24, 2012 · 15 comments

in Exchange 2010 (General), Linux, Linux with Exchange, Monitoring, Nagios, Ubuntu

Linked to my multi part series on using Nagios Core with Exchange server, I would like to explain how you can configure mail notifications to recipients when status changes occur within the Nagios monitoring environment.

If you are just joining us here, I recommend that you have a look at the series in its entirety before reading any further, as there are a few concepts used in this article which were covered in-depth within previous four parts. You can view them here:

  • In Part 1
    I covered how you can install Nagios Core 3.3.1 onto an existing Ubuntu 11.10 server within your environment
  • In Part 2
    I covered how you can setup basic monitoring on your first Windows Server – making use of the NSClient++, I also covered how you could setup a basic “check_nt” monitoring service
  • In Part 3
    I covered how you can install the NRPE daemon onto your Nagios Server – and then use the NSClient to execute a basic Powershell script and report the output back into the Nagios interface
  • In Part 4
    I covered how you can build some custom PowerShell scripts to monitor you Exchange environment

Making Sure that you DNS Configuration is correct

Before you begin to setup the tools which will enable mail notifications to be sent from Nagios, you should ensure that you destination mail domain can be resolved from your Ubuntu server. I have assumed that you will be sending notifications to a mailbox within your Exchange infrastructure which naturally is separate to the Nagios implementation.

In my example, my destination domain is “prepad.com” – therefore in order to check if the Nagios server can lookup the MX record for that domain you can use the “dig” command like so:

dig prepad.com mx

If everything is ok, you should receive an output to the command similar to that below:

nagNotify006

Installing POSTFIX

Postfix is a free MTA for Linux which is designed to be easier to setup and configure than the more traditional SendMail offering. Combined with Heirloom-MailX (see next section) Nagios can be configured to use the two products to send e-mail notifications to a designated set of contacts in the event of an alert being generated.

From the perspective of using Postfix to send e-mail from Nagios – the setup is very simple (e.g. I am not going to go into huge amounts of detail on how you can configure Postfix).

In order to install Postfix open an SSH session to your Nagios Ubuntu Server using PuTTY and type in the following command:

sudo apt-get install postfix

nagNotify001

You will then be presented with a two step setup Wizard – the first page will ask you what type of POSTFIX SMTP Server you would like to use – ensure that you have selected the “Internet Site” option, when done navigate (using TAB) to the “OK” button – see below

nagNotify002

You will then be asked to supply a Domain Name which the source e-mails will be sent from – in my example I have used “nagios.notify” – when you are done tab to the “OK” button – see below

nagNotify003

This will then complete the configuration of Postfix.

Installing Heirloom-MailX

Heirloom-MailX is a small binary program that is used by Nagios to send mail to the Postfix mail server – it is very easy to install.
From the SSH console, type in the following command:

sudo apt-get install heirloom-mailx

nagNotify004

Configuring Exchange to accept messages from your Nagios Server

There are better ways to do this, however if you want to ensure that your Exchange Hub transport servers are accepting mail from your Nagios server, you should ensure that the Default Receive Connector on your HT is configured to accept anonymous connections – you can do this by opening up the Exchange Management Console and navigating to [ Microsoft Exchange On-Premises –> Server Configuration –> Hub Transport ], then select your HT and then from the low pane (Receive Connectors) right click on the “Default <Your HT>” – from the context menu that appears choose “Properties” – see below

nagNotify007

From the properties box that appears, click on the “Permission Groups” tab, and then tick the “Anonymous Users” tick box, and then click on the “OK” button – see below

nagNotify008

In the real world, you should probably consider creating a dedicated Receive Connector for the Nagios instance.

Configuring Nagios to Send Mail Notifications

Now that you have installed the required components to enabled Nagios to send mail notifications, you will need to download two configurations files from your Nagios Server (using FileZilla and edit them using Notepad++ – if you need to understand how to do this, please review part 2 of my installing Nagios for Exchange series).

These files are:

  1. commands.cfg
  2. contacts.cfg

Which are located in:

/usr/local/nagios/etc/objects

Open the commands.cfg file in Notepad++ and modify the following two lines:

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

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

To look like the following:

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

# '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/mailx -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
    }

In essence you are changing the /bin/mail part of each command to /usr/bin/mailx

When you are done, save the file and then using FileZilla upload it back to your Nagios Server.

Next open the contacts.cfg file and change the “email” property to that of the account where you would like the notifications to be sent – see below

define contact{
        contact_name                    nagiosadmin        ; Short name of user
        use                                generic-contact        ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin        ; Full name of user
        email                           administrator@prepad.com    ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        }

When you are done, save the file, and upload it back to your Nagios server.

To finish up the configuration, from a PuTTY session type in the following command:

sudo /etc/init.d/nagios restart

For the configuration changes to take effect.

When there is now a problem with either a service or a host, the chosen recipient should receive a mail notification like the following:

nagNotify009

Social

{ 14 comments… read them below or add one }

hi March 15, 2012 at 10:11 am

please give me a complete procedure about how to configure email notification in nagios3…………………..

Reply

Andy Grogan March 17, 2012 at 11:22 am

The article does cover the complete procedure – have you actually read it?

Reply

hi March 21, 2012 at 3:46 am

what about escalation procedure???????????

Reply

Andy March 21, 2012 at 7:07 am

That wasn’t the focus of this part, I may cover it in a later article.

Reply

Todd March 22, 2012 at 6:27 pm

I want to start by saying that your blog has been awseom helping me get Nagios up and running! I followed it to the T and alls good! Its great, I ahve all my firewalls and VMware hosts being monitored. But Im stuck here at the email notification. I followed the instructions here and still dont get notifications. I am a Linux noob (even though Ive been in the industry 25 years) and Im sure I have everything set up right except postfix. When I installed postfix it asked a lot more questions then you noted above. I must be missing something. Im specifically doing this so I can learn Linux a bit better to start trying to use it more.

Reply

Todd March 26, 2012 at 3:51 pm

Update! I did get mail working on Ubuntu using Sendmail. However, Nagios is not sending emails to it. Googled and Googled and Googled with no luck yet. My general feeling is that its the ‘notify-service-by-email’ command but I’ve tried a lot with no luck. I cna send email from the command line with no problem.

Reply

sudhir March 29, 2012 at 6:18 pm

PROBLEM Service Alert: My Windows Server/Explorer is CRITICAL **” sudhirayrota@gmail.com” resulted in a return code of 127
i am getting this error is there a solution for this.

Reply

Ganesh April 9, 2012 at 2:01 pm

Thanks.., I hope it will work i have same issue but the OS is Fedora 16 and its a VM image with Nagios version of 3.3.1

Reply

Trung Le August 19, 2012 at 5:57 pm

Many Thanks

Reply

Joey February 7, 2013 at 7:00 pm

Great tutorial, Thanks.
I wasn’t receiving mail.. checked log (tail /var/log/mail.log -f)
received…
postdrop: warning: unable to look up public/pickup: No such file or directory.

Turns out that sendmail was previously installed and that was messing things up. I had to stop sendmail and make the appropriate directory and restart postfix.

Specifically:

mkfifo /var/spool/postfix/public/pickup
ps aux | grep mail
kill (pid)
sudo /etc/init.d/postfix restart

thought I’d pass along

Reply

SHinju March 22, 2013 at 1:38 pm

Hi!
How to change Nagios@notify by Noreply@notify?
THx

Reply

hari July 18, 2013 at 6:06 pm

Hi,

I have configured Nagios Core 3.5.

When i check log. getting error.

please see the error.

tail -f /var/log/messages

Jul 18 13:48:34 NagiosHub nagios: HOST NOTIFICATION: nagiosadmin;CCI-SW-03;DOWN;notify-host-by-email;(Host Check Timed Out)
Jul 18 13:48:34 NagiosHub nagios: Warning: Attempting to execute the command “/usr/bin/printf “%b” “***** Nagios *****\n\nNotification Type: PROBLEM\nHost: CCI-SW-03\nState: DOWN\nAddress: 192.168.0.211\nInfo: (Host Check Timed Out)\n\nDate/Time: Thu Jul 18 13:48:34 EDT 2013\n” | /usr/bin/mail -s “** PROBLEM Host Alert: CCI-SW-03 is DOWN **” saiwale@coreitx.com” resulted in a return code of 127. Make sure the script or binary you are trying to execute actually exists…

Reply

Dan March 21, 2014 at 4:28 pm

How do I set the FROM user that the nagios uses to send email?

Reply

avraham shir-el August 21, 2014 at 1:29 pm

for purposes of nagios, what does mailx do that /bin/mail doesn’t?

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: