A Guide to Monitoring Servers/Services with Nagios — Part 4

Kelom
5 min readSep 10, 2020

--

(NOTIFICATION (EMAIL ALERT) CONFIGURATION)

In part 1, we installed & configured Nagios Core on Oracle Linux 7, (read it here)
In part 2, we added a windows machine for monitoring. (read it here)
In part 3, we added Linux machine with MySQL database and monitor the MySQL database. (read it here)

Gradually, we have come to the end of this series, and in this concluding part, we will look at notification (email alert) configuration.

Prerequisite:

  • Oracle Linux 7 environment.

On the main Nagios server (the server we installed and configured in part 1(read it here))

Step 1: download and extract sendEmail from https://github.com/mogaal/sendemail

Step 2: Extract and Copy the sendEmail to /usr/bin

cp -a sendEmail-master/sendEmail /usr/bin

Content of /usr/bin

Step 3: Make sure sendEmail is executable, change directory to /usr/bin, and run this command

chmod +x /usr/bin/sendEmail

Step 4: Install the packages needed by sendemail:

sudo yum install ‘perl(Net::SSLeay)’ –y
sudo yum install ‘perl(IO::Socket::SSL)’ –y

Step 5: hiding the password to the email for the alert (where email_password = your email password)

echo “email_password” >> /home/nagios/.sendemail.key

Step 6: open the sendemail file located at /usr/bin/ in a text editor, go to line: 1906

Step 7: change the ssl_version to: SSLv23:!SSLv2

Step 8: send a test mail, open terminal and type:

/usr/bin/sendEmail -f sender@email.com -t receiver@emai.com -s sender_smtp:port -u “email subject” -xu smtp_username -xp `head /home/nagios/.sendemail.key` -m ‘email body’ -o tls=yes -l /email.log

Change the following parameters to suit your email config:

sender@email.com
receiver@emai.com
sender_smtp:port
smtp_username

check out the documentation for SendEmail on https://github.com/mogaal/sendemail

Email successfully sent:

We are now good to configure Nagios to send email alerts.

Step 9: open the contacts.cfg file located at /usr/local/nagios/etc/objects

vi /usr/local/nagios/etc/objects/contacts.cfg
  • create a contact called nagiosadmin (this will be the recipient/receivers of the email alerts from Nagios, you can give it any name you like)
  • add a valid email address.
  • and be sure to add these 2 lines:

host_notification_options d,u,r,f;
host_notification_commands notify-host-by-email

Step 10: create a contact group in the same file (contacts.cfg) and add nagiosadmin as members

Also, Notification should have been enabled for the service by adding these parameters to the host and service definitions:

// for service definition

contact_groups admins
notifications_enabled 1
notification_period 24x7
notification_options w,u,c,r,f,s
notification_interval 1
max_check_attempts 3
check_period 24x7

// for host definition

check_interval 1
retry_interval 1
contact_groups admins
notifications_enabled 1
notification_period 24x7
notification_options d,u,r,f,s
notification_interval 1

For more info about Nagios parameters read the doc: https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/objectdefinitions.html

Step 11: modify the commands.cfg located at /usr/local/nagios/etc/objects

vi /usr/local/nagios/etc/objects/ commands.cfg
  • look for notify-host-by-email and change command_line to:
/usr/bin/printf “%b” “***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n” | /usr/bin/sendEmail sendEmail -f sender@email.com -t $CONTACTEMAIL$ -s sender_smtp:port -xu smtp_username -xp `head /home/nagios/.sendemail.key` -u “** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **” -m “** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **” -o tls=yes

Change the following parameters to suit your email config before saving the notify-host-by-email command_line:

sender@email.com
sender_smtp:port
smtp_username

  • look for notify-service-by-email and change command_line to:
/usr/bin/printf “%b” “***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress:$HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n” | /usr/bin/sendEmail -f sender@email.com -s sender_smtp:port -xu smtp_username -xp `head /home/nagios/.sendemail.key` -o tls=yes -l /email.log -u “** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **” -t $CONTACTEMAIL$

Change the following parameters to suit your email config before changing the notify-service-by-email command_line:

sender@email.com
sender_smtp:port
smtp_username

Step 13: Restart the Nagios service, command:

systemctl restart nagios.service

To test the email notification, temporarily disable/disconnect the network on the linux or windows box from previous tutorials

Wait for a while and you should see the state of those hosts change.

Check your mail and you should see an email alert from Nagios

Reconnect the network on the boxes and after some time, Nagios should alert you.

We have successfully configured nagios to send email alerts.

--

--