|

How to use email id of Gmail for sending nagios email alerts

How to use email id of Gmail for sending nagios email alerts

In this tutorial we will learn about how to use email id of Gmail for sending nagios email alerts.

Requirement:

(1): Create one email id of gmail for sending emails from nagios server eg. nagiosalerts@gmail.com

(2): Using ssmtp package . As per man page of ssmtp, ssmtp is a send-only sendmail emulator for machines which normally pick their mail up from a centralized mailhub (via pop, imap, nfs mounts or other means). It provides the functionality required for humans and programs to send mail via the standard or /usr/bin/mail user agents

(3) sendmail package but service can be stopped.

Question: Why I am using gmail id here ?
Answer: Because the nagios server can send email directly from server also. But there is a possibilities that these email will go to spam box. Gmail,Yahoo,AOL,Hotmail etc. big boys , now a days tightened more security on spamming.
For non spam email, our mail server must follow dkim,spf,reverse dns etc requirement.
This task can also be achieved by relaying the email from MTS config file. But here I want to use free email service i.e gmail

Follow the given below steps to use gmail email id for nagios alerts

Step 1 : Create one gmail id from www.gmail.com ,here I am using nagiosalerts@gmail.com

Step 2: Login into Nagios Server and install ssmtp and sendmail

In CentOS and RedHat

yum install ssmtp sendmail

In Debian and Ubuntu

sudo apt-get install ssmtp sendmail

Step 3: Now I am stoping the sendmail service as well as disabling from runlevel bootup

In CentOS and Red Hat

/etc/init.d/sendmail stop ; chkconfig sendmail off

In Debian and Ubuntu

sudo /etc/init.d/sendmail stop ; sudo update-rc.d -f sendmail disable

Step 4: Now take the backup of ssmtp.conf file and configure it.

cp -p  /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.orig.`date +%F`

> /etc/ssmtp/ssmtp.conf 

(now edit the ssmtp.conf file with vi editor or any editor which you like)

vi /etc/ssmtp/ssmtp.conf 

AuthUser=nagiosalerts@gmail.com
AuthPass=password
FromLineOverride=YES
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

Step 5: Now take the backup of sendmail script. It is good to use which command to find exact path of sendmail script. In my case it is at /usr/sbin/sendmail .Hence I am changing directory to /usr/sbin . It might be different in your case.

(To get absolute path of sendmail script)
which sendmail


(To get absolute path of ssmtp script)
which ssmtp 


(Take backup of sendmail script)
cp -p /usr/sbin/sendmail /usr/sbin/sendmail.orig.`date +%F`

rm /usr/sbin/sendmail

cd /usr/sbin

ln -s /usr/sbin/ssmtp sendmail

Below is reference from my system

[root@Nagios sbin]# ls -l sendmail
lrwxrwxrwx 1 root root 15 Jul 16 10:29 sendmail -> /usr/sbin/ssmtp
[root@Nagios sbin]#

Step 5: Send an test email , replace emailid@example.com with your email id

echo "testing for nagios alerts"|mail -s "test nagiosalerts" emailid@example.com

Check your inbox the email should come from nagiosalerts@gmail.com. You can also check the header of email.

Step 6: Now find the contacts.cfg file of nagios in /etc/nagios

Note: In Red Hat based and Debian Based linux OS, the path of contacts.cfg of nagios is different(If you have installed it with yum or apt-get),hence search the contacts.cfg file.

Now edit the contacts.cfg file of nagios

Replace YourName and youremailid@domain-name.com as per your information.

define contact{
        contact_name                    YourName
        alias                           YourName
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    c,r
        host_notification_options       d,r
        service_notification_commands   notify-by-email
        host_notification_commands      host-notify-by-email
        email                           youremailid@domain-name.com
        }

Step 7: Restart the nagios

In CentOS and Red Hat
/etc/init.d/nagios restart

In Debian and Ubuntu (Nagios version 3.x)
/etc/init.d/nagios3 restart

Step 8: The setup is completed. Now you will receive the email alerts from gmail id. (nagiosalerts@gmail.com)

Similar Posts

29 Comments

  1. Thank you, your tutorial achieved for me to get until sending email into my inbox.But, I dont know how to check either the last step is success or not.

    P/s: I got error “send-mail: Cannot open smtp.gmail.com:587” and fix with “TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt ” inside the ssmtp.conf” and success to received email in gmail inbox.

    1. Hi Noobies,

      Now a days Gmail has increased the SMTP Auth security. Address may have been changed or port number.
      I would recommend you to use some good mail service like Sendgrid.
      This post I have written as a workaround by using gmail account and thought to share.

      If in your organisation you have SMTP server, take help from Mail Admin to provide SMTP address and Credentials.

      Regards
      Sharad

    1. Hi Rahul,

      There is another approach. You can install postfix and alter the mail_from.
      But remember the server should be in whitelisted state otherwise all email will go in spam. Now a days all Mail Provider has increased the security level as per types of attacks happening in internet.

      Now a days , I use AWS SES service which is quite useful.

      Regards
      Sharad

  2. Hi,

    I did the configurations as you mentions and it’s working fine. Only problem which i am facing is – I am getting notification mails in spam folder instead of inbox folder.

    Please suggest what changes I have to made to receive the notifications in inbox ( I am using gmail id’s ).

    1. Hi Shashikant,

      Mail security standards are increased now a days due to lots of spam and malicious activity. It could be various reason your email is dropping in spam inbox.

      1. The sender’s email id is not recognised by receiving MTA i.e gmail, yahoo, hotmail etc.
      2. Sender’s email id do not exist.
      3. email id suffix with domain name is blacklisted one.
      4. Source server may be blacklisted or compromised.
      5. No SPF record for domain name which is used in your email eg. email@example.com
      6. No subject line or blank email is also reason for spam.
      7. Frequency of sending email like Marketing emails also a reason for spamming.

      … .. Any many more reason.

      The Mail topic is vast and require mail standard to follow.

      Regards
      Sharad

  3. Hi Im having this error below

    root@gwc-serv:/usr/sbin# echo “testing for nagios alerts”|mail -s “test nagiosalerts” henry@gulfwc.com
    Cannot start “/usr/sbin/sendmail”: executable not found (adjust *sendmail* variable)
    “/root/dead.letter” 6/140
    … message not sent

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.