{"id":4477,"date":"2019-10-19T22:51:11","date_gmt":"2019-10-19T19:51:11","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4477"},"modified":"2024-03-12T22:12:01","modified_gmt":"2024-03-12T19:12:01","slug":"configure-nagios-email-notification-using-gmail","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-nagios-email-notification-using-gmail\/","title":{"rendered":"Configure Nagios Email Notification Using Gmail"},"content":{"rendered":"\n

Nagios can be configured to sent out alerts on the state of the host or host service being monitored via email. This guide will therefore take you through how to configure Nagios Email Notification using Gmail.<\/p>\n\n\n\n

The current state of a service or host being monitored is determined by the status of the service or host<\/code><\/strong> which can be OK, WARNING, UP, DOWN<\/code><\/strong>, etc. and the type of state<\/code><\/strong> the service or host which can hard or soft<\/code><\/strong>.<\/p>\n\n\n\n

Read more about notification on Nagios notification<\/a>.<\/p>\n\n\n\n

Before you can proceed, install Nagios and add hosts to be monitored.<\/p>\n\n\n\n

Install Nagios Server on CentOS 8<\/a><\/p>\n\n\n\n

Add Hosts to Nagios Server For Monitoring<\/a><\/p>\n\n\n\n

Configure Nagios Availability Monitoring<\/a><\/p>\n\n\n\n

Configuring Nagios Email Notification Using Gmail<\/h2>\n\n\n\n

Install Required Mail Packages<\/h3>\n\n\n\n

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<\/code> command. Hence, run the command below to install the required packages.<\/p>\n\n\n\n

dnf install postfix cyrus-sasl-plain mailx<\/code><\/pre>\n\n\n\n

Configure Postfix to Use Gmail Relay<\/h3>\n\n\n\n

Enable STARTTLS encryption by changing the line smtp_tls_security_level = may<\/code> to smtp_tls_security_level = encrypt<\/strong><\/code>.<\/strong><\/p>\n\n\n\n

sed -i 's\/smtp_tls_security_level = may\/smtp_tls_security_level = encrypt<\/strong>\/' \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n

If the smtp_tls_security_level<\/strong> option is not set, just insert it;<\/p>\n\n\n\n

echo \"smtp_tls_security_level = encrypt<\/strong>\" >> \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n

Define the path to CA certificates. The public root certificates are usually found under \/etc\/pki\/tls\/certs\/ca-bundle.crt<\/code> on RHEL derivatives and \/etc\/ssl\/certs\/ca-certificates.crt<\/code> on Debian\/Ubuntu systems.<\/p>\n\n\n\n

echo \"smtp_tls_CAfile = \/etc\/pki\/tls\/certs\/ca-bundle.crt\" >> \/etc\/postfix\/main.cf <\/code><\/pre>\n\n\n\n

Next, insert the following lines to the Postfix configuration file to define the Gmail relay host and SASL options.<\/p>\n\n\n\n

cat >> \/etc\/postfix\/main.cf << EOF\nrelayhost = [smtp.gmail.com]:587\nsmtp_sasl_auth_enable = yes\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\nsmtp_sasl_security_options = noanonymous\nEOF<\/code><\/pre>\n\n\n\n

Configure SASL credentials for your gmail account.<\/p>\n\n\n\n

vim \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n

Enter the following content, replacing the userid<\/strong> and password<\/strong> accordingly.<\/p>\n\n\n\n

[smtp.gmail.com]:587 userid@gmail.com:password<\/code><\/pre>\n\n\n\n

Generate Postfix lookup table from the \/etc\/postfix\/sasl_passwd<\/strong> file.<\/p>\n\n\n\n

postmap \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n

Change ownership and permission to of the \/etc\/postfix\/sasl_passwd<\/strong> to root<\/code> and read-write<\/code> only respectively.<\/p>\n\n\n\n

chown root:root \/etc\/postfix\/sasl_passwd*\nchmod 600 \/etc\/postfix\/sasl_passwd*<\/code><\/pre>\n\n\n\n

Start and enable Postfix<\/p>\n\n\n\n

systemctl enable postfix --now<\/code><\/pre>\n\n\n\n

Test the relay;<\/p>\n\n\n\n

First allow less secure apps access to your gmail account<\/a>.<\/p>\n\n\n\n

After that, try to sent a test mail.<\/p>\n\n\n\n

echo \"Test Postfix Gmail Relay\" | mail -s \"Postfix Gmail Relay\" userid@gmail.com<\/code><\/pre>\n\n\n\n

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.<\/p>\n\n\n\n

tail -f \/var\/log\/maillog<\/code><\/pre>\n\n\n\n
Oct 19 15:01:44 dev-server postfix\/smtp[5109]: C7E8C3B5AD: to=userid@gmail.com<\/a>, 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<\/strong>  1571511704 h8sm11800598pfo.64 - gsmtp)\nOct 19 15:01:44 dev-server postfix\/qmgr[4574]: C7E8C3B5AD: removed<\/code><\/pre>\n\n\n\n

Create Nagios Contact Object Definition<\/h3>\n\n\n\n

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.<\/p>\n\n\n\n

Nagios comes with a default contact group, contacts.cfg<\/code>, located on the default objects definition configurations directory, \/usr\/local\/nagios\/etc\/objects<\/strong><\/code>.<\/p>\n\n\n\n

You can modify the default contacts definition configuration file or create your own.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

For example in the above case, insert the line, cfg_dir=\/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/code> on the Object definitions file<\/strong> section of the \/usr\/local\/nagios\/etc\/nagios.cfg<\/code>.<\/p>\n\n\n\n

In this guide, we create a custom contacts definition configuration under a custom directory, \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/code>.<\/p>\n\n\n\n

vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/contacts.cfg<\/code><\/pre>\n\n\n\n
define contact{\n        name                            kifarunix-demo-contact\n        service_notification_period     24x7\n        host_notification_period        24x7\n        service_notification_options    w,u,c,r\n        host_notification_options       d,r\n        service_notification_commands   notify-service-by-email\n        host_notification_commands      notify-host-by-email\n        register                        0\n        }\n# Define Individual Contacts\ndefine contact{\n        contact_name                    johndoe\n        use                             kifarunix-demo-contact\n        alias                           John Doe-Oracle DBA\n        email                           djoe@kifarunix-demo.com\n        }\ndefine contact{\n        contact_name                    janedoe\n        use                             kifarunix-demo-contact\n        alias                           Jane Doe-System Admin\n        email                           djane@kifarunix-demo.com\n        }\n# CONTACT GROUP DEFINITION allows multiple contacts to receive alerts\ndefine contactgroup{\n        contactgroup_name    kifarunix-demo-admins\n        alias                Sys-DB Admins\n        members              johndoe,janedoe\n       }<\/code><\/pre>\n\n\n\n

Modify the contact configuration above accordingly.<\/p>\n\n\n\n

Verify Nagios Configuration file<\/h3>\n\n\n\n
\/usr\/local\/nagios\/bin\/nagios -v \/usr\/local\/nagios\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n

If there is no syntax error, restart Nagios service.<\/p>\n\n\n\n

systemctl restart nagios<\/code><\/pre>\n\n\n\n

Testing Nagios Mail Alerts Notification<\/h3>\n\n\n\n