{"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<p>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<p>The current state of a service or host being monitored is determined by the <strong><code>status of the service or host<\/code><\/strong> which can be <strong><code>OK, WARNING, UP, DOWN<\/code><\/strong>, etc. and the <strong><code>type&nbsp;of state<\/code><\/strong> the service or host which can <strong><code>hard or soft<\/code><\/strong>.<\/p>\n\n\n\n<p>Read more about notification on <a rel=\"noreferrer noopener\" aria-label=\"Nagios notification (opens in a new tab)\" href=\"https:\/\/assets.nagios.com\/downloads\/nagioscore\/docs\/nagioscore\/4\/en\/notifications.html\" target=\"_blank\">Nagios notification<\/a>.<\/p>\n\n\n\n<p>Before you can proceed, install Nagios and add hosts to be monitored.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-nagios-server-on-centos-8\/\" target=\"_blank\">Install Nagios Server on CentOS 8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/add-hosts-to-nagios-server-for-monitoring\/\" target=\"_blank\">Add Hosts to Nagios Server For Monitoring<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-configure-availability-monitoring-on-alienvault-usm-ossim\/\">Configure Nagios Availability Monitoring<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Nagios Email Notification Using Gmail<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Required Mail Packages<\/h3>\n\n\n\n<p>In this guide, we are going to use Postfix as out Mail Transfer Agent (MTA). Also, by default, Nagios Mail notification is sent using&nbsp;<code>mail<\/code>&nbsp;command. Hence, run the command below to install the required packages.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install postfix cyrus-sasl-plain mailx<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Postfix to Use Gmail Relay<\/h3>\n\n\n\n<p>Enable STARTTLS encryption by changing the line <code>smtp_tls_security_level = may<\/code> to <code><strong>smtp_tls_security_level = encrypt<\/strong><\/code><strong>.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed -i 's\/smtp_tls_security_level = may\/<strong>smtp_tls_security_level = encrypt<\/strong>\/' \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n<p>If the <strong>smtp_tls_security_level<\/strong> option is not set, just insert it;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"<strong>smtp_tls_security_level = encrypt<\/strong>\" &gt;&gt; \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n<p>Define the path to CA certificates. The public root certificates are usually found under <code>\/etc\/pki\/tls\/certs\/ca-bundle.crt<\/code> on RHEL derivatives and <code>\/etc\/ssl\/certs\/ca-certificates.crt<\/code>&nbsp;on Debian\/Ubuntu systems.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"smtp_tls_CAfile = \/etc\/pki\/tls\/certs\/ca-bundle.crt\" &gt;&gt; \/etc\/postfix\/main.cf <\/code><\/pre>\n\n\n\n<p>Next, insert the following lines to the Postfix configuration file to define the Gmail relay host and SASL options.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cat &gt;&gt; \/etc\/postfix\/main.cf &lt;&lt; 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<p>Configure SASL credentials for your gmail account.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n<p>Enter the following content, replacing the <strong>userid<\/strong> and <strong>password<\/strong> accordingly.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><code>[smtp.gmail.com]:587 userid@gmail.com:password<\/code><\/pre>\n\n\n\n<p>Generate Postfix lookup table from the <strong>\/etc\/postfix\/sasl_passwd<\/strong> file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>postmap \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n<p>Change ownership and permission to&nbsp;of the <strong>\/etc\/postfix\/sasl_passwd<\/strong> to <code>root<\/code>&nbsp;and&nbsp;<code>read-write<\/code>&nbsp;only respectively.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chown root:root \/etc\/postfix\/sasl_passwd*\nchmod 600 \/etc\/postfix\/sasl_passwd*<\/code><\/pre>\n\n\n\n<p>Start and enable Postfix<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable postfix --now<\/code><\/pre>\n\n\n\n<p>Test the relay;<\/p>\n\n\n\n<p>First <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/myaccount.google.com\/u\/1\/lesssecureapps\" target=\"_blank\">allow less secure apps access to your gmail account<\/a>.<\/p>\n\n\n\n<p>After that, try to sent a test mail.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"Test Postfix Gmail Relay\" | mail -s \"Postfix Gmail Relay\" userid@gmail.com<\/code><\/pre>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>tail -f \/var\/log\/maillog<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Oct 19 15:01:44 dev-server postfix\/smtp[5109]: C7E8C3B5AD: to=<a href=\"mailto:mpolekip@gmail.com\">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, <strong>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<h3 class=\"wp-block-heading\">Create Nagios Contact Object Definition<\/h3>\n\n\n\n<p>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<p>Nagios comes with a default contact group, <code>contacts.cfg<\/code>, located on the default objects definition configurations directory, <code><strong>\/usr\/local\/nagios\/etc\/objects<\/strong><\/code>.<\/p>\n\n\n\n<p>You can modify the default contacts definition configuration file or create your own.<\/p>\n\n\n\n<p>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<p>For example in the above case, insert the line, <code>cfg_dir=\/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/code> on the <strong>Object definitions file<\/strong> section of the <code>\/usr\/local\/nagios\/etc\/nagios.cfg<\/code>.<\/p>\n\n\n\n<p>In this guide, we create a custom contacts definition configuration under a custom directory, <code>\/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/contacts.cfg<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>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<p>Modify the contact configuration above accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Verify Nagios Configuration file<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/usr\/local\/nagios\/bin\/nagios -v \/usr\/local\/nagios\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n<p>If there is no syntax error, restart Nagios service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nagios<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing Nagios Mail Alerts Notification<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"1347\" height=\"361\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/10\/host-down.png\" alt=\"\" class=\"wp-image-4478\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/10\/host-down.png?v=1571512993 1347w, https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/10\/host-down-768x206.png?v=1571512993 768w\" sizes=\"(max-width: 1347px) 100vw, 1347px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Reschedule the next check for the host state. This will automatically sent out an email alert on host DOWN.<\/li>\n<\/ul>\n\n\n\n<p>If you encounter the error below when rescheduling checks, <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>Error: Could not open command file '\/usr\/local\/nagios\/var\/rw\/nagios.cmd' for update!<\/strong><\/code><\/pre>\n\n\n\n<p>This is due to SELinux. To fix, run <code>journactl -xe<\/code>. It should show some SELinux commands to execute to fix this. The commands below is what i run myself.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ausearch -c 'cmd.cgi' --raw | audit2allow -M my-cmdcgi\nsemodule -X 300 -i my-cmdcgi.pp<\/code><\/pre>\n\n\n\n<p>Also, you may encounter the error;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>Could not open command file '\/usr\/local\/nagios\/var\/rw\/nagios.cmd'<\/strong><\/code><\/pre>\n\n\n\n<p>Run the command below to fix it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chcon -R -t httpd_sys_script_rw_t \/usr\/local\/nagios\/var\/rw<\/code><\/pre>\n\n\n\n<p>You should be able to manually reschedule Nagios host or service checks.<\/p>\n\n\n\n<p>You should now get the email alert on the host being down.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>** PROBLEM Host Alert: web01.kifarunix-demo.com is DOWN **\n\nusername@gmail.com\n\n***** Nagios *****\n\nNotification Type: PROBLEM\nHost: web01.kifarunix-demo.com\n<strong>State: DOWN<\/strong>\nAddress: 192.168.56.18\nInfo: CRITICAL - Host Unreachable (192.168.56.18)\n\nDate\/Time: Sat Oct 19 15:16:09 EDT 2019<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>** RECOVERY Host Alert: web01.kifarunix-demo.com is UP **\n\nusername@gmail.com\n\n***** Nagios *****\n\nNotification Type: RECOVERY\nHost: web01.kifarunix-demo.com\n<strong>State: UP<\/strong>\nAddress: 192.168.56.128\nInfo: PING OK - Packet loss = 0%, RTA = 1.36 ms\n\nDate\/Time: Sat Oct 19 15:31:13 EDT 2019<\/code><\/pre>\n\n\n\n<p>You should be able to receive alerts for service\/host state changes.<\/p>\n\n\n\n<p>You may also be interested in checking the following tutorials.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/nagios-snmp-monitoring-of-linux-hosts-on-alienvault-usm-ossim\/\" target=\"_blank\">Nagios SNMP Monitoring of Linux Hosts on AlienVault USM\/OSSIM<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/monitor-linux-hosts-using-nagios-check_by_ssh-plugin\/\" target=\"_blank\">Monitor Linux Hosts using Nagios check_by_ssh Plugin<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-install-nagios-nrpe-agent-on-rhel-centos-oracle-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">How to Install Nagios NRPE Agent on RHEL\/CentOS\/Oracle Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,72,73],"tags":[75,1163,145,1164],"class_list":["post-4477","post","type-post","status-publish","format-standard","hentry","category-howtos","category-monitoring","category-nagios","tag-nagios","tag-nagios-email-notification","tag-nagios-monitoring","tag-postfix-gmail-relay","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4477"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4477"}],"version-history":[{"count":5,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4477\/revisions"}],"predecessor-version":[{"id":21221,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4477\/revisions\/21221"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}