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)
Leave a Reply