How To Send Alerts From Nagios Core Using Gmail And Yahoo

Introduction

One of the most important feature of the monitoring platform is the notification system; by default  they use the local mail daemon (postfix,mail,exim) installed on the server to send the notifications.

Using local mail daemon has many issues :

  • You need to install and configure your smtp server.
  • Maybe your mail server go down so you can’t receive the alert.
  • Maybe this kind of emails from a not verified source like nagios can be filtered by the same company antispam system.

In order to avoid all issues, In this article I am going to explain how to send alert from monitoring tool Nagios Core using an external smtp server with authentication like Gmail and Yahoo.

This task was not so easy since communicating with Gmail through a linux server was requiring permissions and certifications but with sendEmail you can do it easily.

Installation and Configuration Of SendEmail

SendEmail is an email program for sending SMTP mail from a command line. SendEmail is a lightweight program written in Perl and is unique in that it requires NO MODULES. It has an intuitive and flexible set of command-line options, making it very easy to learn and use.

SendEmail is licensed under the GNU GPL and supported many platforms like: Linux, BSD, OS X, Windows 98, Windows NT, Windows 2000, & Windows XP

System Requirements

SendEmail requires certain technical requirements to be met for the store to operate properly. You should check to see that these server requirements are provided and installed on your monitoring servers.

The following commands should be run as root user privileges.

To install those requirements use this command:

On Ubuntu:

apt-get install libio-socket-ssl-perl libnet-ssleay-perl perl

On CentOS:

yum install libio-socket-ssl-perl libnet-ssleay-perl perl

In order to send email via gmail you also need additional libraries “Net::SSLeay” and “IO::Socket::SSL” which are needed for authentication.

To install those libraries use this command:

On Ubuntu:

apt-get install 'perl(Net::SSLeay)' 'perl(IO::Socket::SSL)'

On CentOS:

yum install 'perl(Net::SSLeay)' 'perl(IO::Socket::SSL)'

Download and Installation of sendEmail

In order to start the installation , download the sendEmail tar archive:

wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

Then extract it:

tar -zxvf sendEmail-v1.56.tar.gz

Then copy the sendEmail script to /usr/local/bin. Make use that you have root permission.

sudo cp -a sendEmail-v1.XX/sendEmail /usr/local/bin

The script needs to be executable, so go make it.

sudo chmod +x /usr/local/bin/sendEmail

Then, go ahead and run:

sendEmail

If everything work fine, you will see a usage summary of sendEmail.

send

In order to troubleshoot any eventual problem, you can  create a log file fro senEmail.

touch /var/log/sendEmail
chmod 666 /var/log/sendEmail

In order to test sending an email via Gmail, the command should look like this:

sendEmail -v -f $username -s $host -xu $username -xp $password -t $recipient -o tls=yes -u $subject -m $content

Configure Nagios Core to Send notification Using Gmail

In order to configure Nagios core to send alert using sendEmail and external smtp server like Gmail, you need to flow those steps:

Start by editing the resource.cfg file located in /usr/local/nagios/etc/resource.cfg :

vi /usr/local/nagios/etc/resource.cfg

Add the following lines with your particular smtp configuration:

$USER5$=email@address.tld
$USER7$=smtp.server.tld:25
#If you don’t use smtp authentication skip this variables and use the version B) of commands.cfg
$USER9$=username
$USER10$=password
On the case of Gmail smtp you can see this example:
$USER5$=email@gmail.com
$USER7$=smtp.gmail.com:587
$USER9$=email
$USER10$=password
Save the file resource.cfg and exit.
After this go under /usr/local/nagios/etc/objects/ and edit the file commands.cfg but that perform a backup of the config file:
cd /usr/local/nagios/etc/objects/
cp commands.cfg commands.cfg.old
Edit the commands.cfg file and you will find two commands ‘notify-host-by-email’ and ‘notify-service-by-email’
A – SMTP AUTHENTICATION COMMANDS
Now, if you use SMTP authentication replace the two commands with 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/local/bin/sendEmail -s $USER7$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" }
# '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$" | /usr/local/bin/sendEmail -s $USER7$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" }
B- NO SMTP-AUTHENTICATION 
This is pretty much the same but without passing the authentication triggers to sendemail:
# '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/local/bin/sendEmail -s $USER7$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" }
# '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$" | /usr/local/bin/sendEmail -s $USER7$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" }
Now the configuration is finished. Before applying the new configuration, run the following command to check for any error or mistake:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If there are no errors, restart the nagios service:

service nagios restart

To troubleshoot the sendemail command run the following to check in the log file:

tail -f /var/log/sendEmail

That all!.

Thanks for reading my article.