This guide will take you through how to configure Nagios Email Notification using Gmail. Nagios can be configured to sent out alerts on the state of the host or host service being monitored via email.
The current state of a service or host being monitored is determined by the status of the service or host which can be OK, WARNING, UP, DOWN, etc. and the type of state the service or host which can hard or soft.
Table of Contents
Configuring Nagios Email Notification Using Gmail
Install Nagios and Add Hosts/Services for Monitoring
Before you can proceed, install Nagios and add hosts to be monitored.
Install Nagios Server on CentOS 8
Add Hosts to Nagios Server For Monitoring
Configure Nagios Availability Monitoring
Install Required Mail Packages for Email Notifications
In this guide, we are going to use Postfix as out Mail Transfer Agent (MTA). Also, by default, Nagios Mail notification is sent using mail command. Hence, run the command below to install the required packages.
on RHEL distros, run:
sudo yum install postfix cyrus-sasl-plain mailx
On Debian distros, run:
sudo apt update
sudo apt install postfix sasl2-bin mailutils
Configure Postfix to Use Gmail Relay
Enable STARTTLS Encryption
Enable STARTTLS encryption by changing the line smtp_tls_security_level = may to smtp_tls_security_level = encrypt.
sudo sed -i 's/smtp_tls_security_level = may/smtp_tls_security_level = encrypt/' /etc/postfix/main.cf
If the smtp_tls_security_level option is not set, just insert it;
sudo grep -q '^smtp_tls_security_level' /etc/postfix/main.cf || echo 'smtp_tls_security_level = encrypt' | sudo tee -a /etc/postfix/main.cf
Define the path to CA certificates
The public root certificates are usually found under /etc/pki/tls/certs/ca-bundle.crt on RHEL derivatives and /etc/ssl/certs/ca-certificates.crt on Debian/Ubuntu systems.
Hence, on RHEL node, define the CA path as follows;
echo "smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt" | sudo tee -a /etc/postfix/main.cf
on Debian/Ubuntu distros;
echo "smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt" | sudo tee -a /etc/postfix/main.cf
Define Gmail SMTP Relay Host Configurations
Next, insert the following lines to the Postfix configuration file to define the Gmail relay host and SASL options.
sudo tee -a /etc/postfix/main.cf << EOF
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
EOF
Configure Gmail Account Credentials
Configure SASL credentials for your Gmail account. First of all, you need to generate Gmail account app password as shown in the guide below;
Generate Gmail App Password for Postfix MTA
Once you have the Gmail account app password, proceed to define those credentials.
sudo vim /etc/postfix/sasl_passwd
Enter the following content, replacing the userid and password accordingly.
[smtp.gmail.com]:587 [email protected]:app-password
Save and exit the configuration once done.
Generate Postfix lookup table from the /etc/postfix/sasl_passwd file.
sudo postmap /etc/postfix/sasl_passwd
Change ownership and permission to of the /etc/postfix/sasl_passwd to root and read-write only respectively.
sudo chown root:root /etc/postfix/sasl_passwd*
sudo chmod 600 /etc/postfix/sasl_passwd*
Start Postfix Service
Start and enable Postfix
sudo systemctl enable postfix --now
Test the Postfix Email Delivery via Gmail Relay;
After the configuration, try to sent a test mail.
echo "Test Postfix Gmail Relay" | mail -s "Postfix Gmail Relay" [email protected]
You should be able to receive the mail on your inbox. You can also check the mail logs. The log filename may be different for your case.
On RHEL nodes;
sudo tail -f /var/log/maillog
On Ubuntu/Debian;
sudo tail -f /var/log/mail.log
Sample mail log when all is good.
Oct 19 15:01:44 dev-server postfix/smtp[5109]: C7E8C3B5AD: to=[email protected], relay=smtp.gmail.com[74.125.200.109]:587, delay=18, delays=0.04/0.02/16/2.1, dsn=2.0.0, status=sent (250 2.0.0 OK 1571511704 h8sm11800598pfo.64 - gsmtp)
Oct 19 15:01:44 dev-server postfix/qmgr[4574]: C7E8C3B5AD: removed
Create Nagios Contact Object Definition
The first step in configuring is to create a Nagios contacts group that defines who should be notified on the state of a monitored service or host.
Nagios comes with a default contact group, contacts.cfg, located on the default objects definition configurations directory, /usr/local/nagios/etc/objects.
You can modify the default contacts definition configuration file or create your own.
Note that, if you are not using non-default Nagios object definitions directory, you need to configure Nagios to process object definitions in that directory.
For example in the above case, insert the line;
cfg_dir=/usr/local/nagios/etc/objects/kifarunix-demo
on the Object definitions file section of the /usr/local/nagios/etc/nagios.cfg.
In this guide, we create a custom contacts definition configuration under a custom directory, /usr/local/nagios/etc/objects/kifarunix-demo.
sudo vim /usr/local/nagios/etc/objects/kifarunix-demo/contacts.cfg
define contact{
name kifarunix-demo-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
register 0
}
# Define Individual Contacts
define contact{
contact_name johndoe
use kifarunix-demo-contact
alias John Doe-Oracle DBA
email [email protected]
}
define contact{
contact_name janedoe
use kifarunix-demo-contact
alias Jane Doe-System Admin
email [email protected]
}
# CONTACT GROUP DEFINITION allows multiple contacts to receive alerts
define contactgroup{
contactgroup_name kifarunix-demo-admins
alias Sys-DB Admins
members johndoe,janedoe
}
Modify the contact configuration above accordingly.
Verify Nagios Configuration file
Run the command below to check the Nagios configuration for syntax errors:
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
If there is no syntax error, restart Nagios service.
sudo systemctl restart nagios
Testing Nagios Mail Alerts Notification
To test if mail notification works, first change the IP address of one of the hosts to an IP that is unreachable such that it looks like the host is down.

Reschedule the next check for the host state. This will automatically sent out an email alert on host DOWN.
If you encounter the error below when rescheduling checks (on RHEL nodes);
Error: Could not open command file '/usr/local/nagios/var/rw/nagios.cmd' for update!
This is due to SELinux.
To fix, run journactl -xe. It should show some SELinux commands to execute to fix this. The commands below is what i run myself.
sudo ausearch -c 'cmd.cgi' --raw | audit2allow -M my-cmdcgi
sudo semodule -X 300 -i my-cmdcgi.pp
Also, you may encounter the error;
Could not open command file '/usr/local/nagios/var/rw/nagios.cmd'
Run the command below to fix it.
sudo chcon -R -t httpd_sys_script_rw_t /usr/local/nagios/var/rw
You should be able to manually reschedule Nagios host or service checks.
You should now get the email alert on the host being down.
** PROBLEM Host Alert: web01.kifarunix-demo.com is DOWN **
[email protected]
***** Nagios *****
Notification Type: PROBLEM
Host: web01.kifarunix-demo.com
State: DOWN
Address: 192.168.56.18
Info: CRITICAL - Host Unreachable (192.168.56.18)
Date/Time: Sat Oct 19 15:16:09 EDT 2019
Put back the right server IP and reschedule the check to now. You should be able to get the host status UP alert on the mail.
** RECOVERY Host Alert: web01.kifarunix-demo.com is UP **
[email protected]
***** Nagios *****
Notification Type: RECOVERY
Host: web01.kifarunix-demo.com
State: UP
Address: 192.168.56.128
Info: PING OK - Packet loss = 0%, RTA = 1.36 ms
Date/Time: Sat Oct 19 15:31:13 EDT 2019
You should be able to receive alerts for service/host state changes.
You may also be interested in checking the following tutorials.
Nagios SNMP Monitoring of Linux Hosts on AlienVault USM/OSSIM
Monitor Linux Hosts using Nagios check_by_ssh Plugin
How to Install Nagios NRPE Agent on RHEL/CentOS/Oracle Linux
Further Reading
Read more about notification on Nagios notification.
