How to enable email notifications for the Nagios alerts
Introduction
Nagios is an open-source IT system monitoring tool. It sends alerts when critical infrastructure components fail and recover, providing administrators with notice of important events
Prerequisites
- SSH (sudo ) access to the Nagios host server
- The Nagios host should have an SMTP service (postfix – default) in the active state
Implementation
(1) Define email notification commands
$ vi /usr/local/nagios/etc/objects/commands.cfg
========
Add /edit the below lines
# ‘notify-host-by-email’ command definition
define command {
command_name notify-host-by-email command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n” | mail -s “** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **” $CONTACTEMAIL$}
# ‘notify-service-by-email’ command definition
define command {
command_name notify-service-by-email command_line /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” | mail -s “** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **” $CONTACTEMAIL$}
=========
(2) Define email contacts and groups
$ vi /usr/local/nagios/etc/objects/contacts.cfg
Add/edit the below lines
define contact {
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email user@email.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
############### NEW CONTACT ###################
define contact
{
contact_name contact-name
use generic-contact
alias Contact Name
email username1@email.com
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
service_notification_period 24×7
host_notification_period 24×7
service_notification_options w,u,c,r,f
host_notification_options d,u,r,f
}
############### ###################
Modify the “contact_name” and “email” addresses as per the requirement
w = notify on warning states
c = critical states
r = recovery
f = start/stop of flapping
d = notify on down states
u = notify on unreachable states
s = notify on stopped states
(3) Define the email group to which the mail needs to be sent
$ vi /usr/local/nagios/etc/objects/contacts.cfg
Add/edit the below lines
############# NEW CONTACT GROUPS ##############
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members contact-name,mailadmin,nagiosadmin
}
###############################################
(4) Define contact details in the host and service configuration
$ vi /usr/local/nagios/etc/hosts/cl2.cfg
# Host configuration Section
define host
{
use linux-server
host_name hostname
alias alias-name
Server address IP
register 1
contact_groups admins
}
(5) Verify the Nagios configuration and check for error
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
(6) Restart Nagios server
$ systemctl restart nagios