Thursday, May 2, 2013

Nagios 3.5 Notification Configuration via Postfix ( using Gmail SMTP )


What I needed was to make Nagios send Notifications to any party through SMTP using google.This task was not so easy since communicating with gmail through a linux server was requiring permissions and certifications.

Then I found this great guide by rtCamp on how to set up postfix and other needed tools to setup Gmail with sending mail through SMTP.



Now here is what I did,

First,I installed all necessary packages:

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
__________________________________________________________________
# before that I needed to get some updating to the package manager by using 
sudo apt-get update 
__________________________________________________________________ 

When installing postfix I selected  "Internet Site" from the menu and tab OK.For the FQDN use something like mail.example.com.

Then I opened the postfix config file:

/etc/postfix/main.cf
 
And Appened the following line to the end of it 

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
 
Then I created a new file for storing the Gmail usename and password 
/etc/postfix/sasl_passwd

You have to add the following details to that file.
[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

If you want to use your Google App’s domain, you can replace @gmail.com with your @domain.com

Then I fixed permission and updated postfix config to use sasl_passwd file

sudo chmod 400 /etc/postfix/sasl_passwd sudo postmap /etc/postfix/sasl_passwd

Then I validate certificates to avoid running into error. Just running following commands
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

You can reload postfix config for changes to take effect
sudo /etc/init.d/postfix reload

Then to test the SMTP connection you can use the following command to send a test mail
echo "Test mail from postfix" | mail -s "Test Postfix" you@example.com

Note: Gmail’s SMTP server has a limit of 500 emails per day

If it is successfyully sent the mail will arrive in the destination mailbox as well as will be visible in your emails sent mail section.

Now to start with Configuring Nagios to send Notifications to contacts.



For this we need to do the following:


   1.       Create the contacts to be notified 
   2.       Create contact groups to be notified 
   3.       Enable Nagios Notifications
   4.       Define Host and Service notifications
   5.       Restart Nagios
   
   Now lets see how they are done...

   1.       Create the contacts to be notified  
   
   For this you need to edit the contacts.cfg in your Nagios folder and append the contact information of the parties that you want to send notifications of service or host states.you can find the file at

   /usr/local/nagios/etc/objects/contacts.cfg


You can use the template given in the templates.cfg


2.       Create contact groups to be notified

Edit the contacts.cfg  in  nagios/etc/objects
You can use the template given in the templates.cfg




3.       Enable Nagios Notifications


For this you can change the enable_notifications value to true (1) in theNagios main configuration file nagios.cfg.


4.       Define Host and Service notifications

You can change each host/service definition by referring to the template provided for sending notifications to contacts and contact groups.



5.       Restart Nagios

You must restart Nagios for the changes to take effect.




Note: Please change the command definition at commands.cfg for "notify-host-by-email" and "notify-service-by-email". /bin/mail   in to /usr/bin/mailx

dNow that everything is worked out fine you can check the state of your notifications from the Nagios interface Notifications section whether they are being sent or not.




Finding all these was not so easy unless for the generous spirit of the following people who had their ideas shared over the netand I owe a thanks to them as well ! 


http://www.telnetport25.com/2012/02/configuring-e-mail-notifications-in-nagios-core/

2 comments:

  1. Thanks you contributors. This tutorial works flawlessly on Nagios Core 4.1.1. I had no issues following this guide. My Nagios server is sending alerts to gmail perfectly!

    ReplyDelete
  2. Using the Multiple Sign-in features enables you to access more than one account for Gmail, Voice, Sites, Reader, Calendar, and Code from within the same web browser. To turn on Multiple Sign-in, go to the Settings page for your main Google account. https://www.buzzfeed.com/jiprinojit/gmail-support-services-to-resolve-2hmtt


    ReplyDelete