Mail configuration in Nagios Monitoring Server

Install and Configure Postfix

Postfix is a free and open-source mail transfer agent (MTA) that routes and delivers
electronic mail. It is a popular choice for a mail server due to its flexibility, and it’s
relatively straightforward to configure.

Update the System

# dnf update

Install Postfix

# dnf install postfix

The installation is complete, start and enable Postfix

# systemctl start postfix
# systemctl enable postfix
# systemctl status postfix

Configure Postfix

# vim /etc/postfix/main.cf
myhostname = localhost.bitscentric.com
mydomain = localhost.bitscentric.com
myorigin = $myhostname
inet_interfaces = all
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks = 192.168.1.4/24, 127.0.0.0/8
home_mailbox = Maildir/
# systemctl reload postfix

Testing Postfix

[root@localhost ~]# hostname
localhost.bitscentric.com
[root@localhost ~]# mail nagios@localhost.bitscentric.com
Subject: This is My First Email.
To: nagios@localhost.bitscentric.com
Hi, how are you?
^D
-------
(Preliminary) Envelope contains:
To: nagios@localhost.bitscentric.com
Subject: This is My First Email.
Send this message [yes/no, empty: recompose]? yes
[root@localhost ~]#

Check the receive email

[root@localhost new]# pwd
/home/nagios/Maildir/new
[root@localhost new]# cat
1735886471.V10302I60a6516M612111.localhost.bitscentric.com
Return-Path: <root@localhost.bitscentric.com>
X-Original-To: nagios@localhost.bitscentric.com
Delivered-To: nagios@localhost.bitscentric.com
Received: by localhost.bitscentric.com (Postfix, from userid 0)
 id 8DEC060A5A9B; Fri, 3 Jan 2025 12:11:11 +0530 (IST)
Date: Fri, 03 Jan 2025 12:11:11 +0530
To: nagios@localhost.bitscentric.com
Subject: This is My First Email.
User-Agent: s-nail v14.9.22
Message-Id:
<20250103064111.8dec060a5a9b@localhost.bitscentric.com>
From: root <root@localhost.bitscentric.com>
Hi, how are you?
[root@localhost new]#

How to setup email notification in Nagios

# cd /usr/local/nagios/etc/objects/
vim contacts.cfg
define contact {
 contact_name nagiosadmin 
 use generic-contact 
 alias Nagios Admin 
 email nagios@localhost.bitscentric.com ; 
}

Notification Setting

vim vim templates.cfg
define service {
 name generic-service ;
The 'name' of this service template
 active_checks_enabled 1 ;
Active service checks are enabled
 passive_checks_enabled 1 ;
Passive service checks are enabled/accepted
 parallelize_check 1 ;
Active service checks should be parallelized (disabling this
can lead to major performance problems)
 obsess_over_service 1 ;
We should obsess over this service (if necessary)
 check_freshness 0 ;
Default is to NOT check service 'freshness'
 notifications_enabled 1 ;
Service notifications are enabled
 event_handler_enabled 1 ;
Service event handler is enabled
 flap_detection_enabled 1 ;
Flap detection is enabled
 process_perf_data 1 ;
Process performance data
 retain_status_information 1 ;
Retain status information across program restarts
 retain_nonstatus_information 1 ;
Retain non-status information across program restarts
 is_volatile 0 ;
The service is not volatile
 check_period 24x7 ;
The service can be checked at any time of the day
 max_check_attempts 3 ;
Re-check the service up to 3 times in order to determine its
final (hard) state
 check_interval 10 ;
Check the service every 10 minutes under normal conditions
 retry_interval 2 ;
Re-check the service every two minutes until a hard state can
be determined
 contact_groups admins ;
Notifications get sent out to everyone in the 'admins' group
 notification_options w,u,c,r ;
Send notifications about warning, unknown, critical, and
recovery events
 notification_interval 60 ;
Re-notify about service problems every hour
 notification_period 24x7 ;
Notifications can be sent out at any time
 register 0 ;
DON'T REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A
TEMPLATE!
}
w: Notify on WARNING service states
u: Notify on UNKNOWN service states
c: Notify on CRITICAL service states
r: Notify on service RECOVERY (OK states)
f: Notify when the service starts and stops FLAPPING
n (none): Do not notify the contact on any type of service
notifications
d: Notify on DOWN host states
u: Notify on UNREACHABLE host states
r: Notify on host RECOVERY (UP states)
f: Notify when the host starts and stops FLAPPING
s: Send notifications when host or service scheduled downtime
starts and ends
n (none): Do not notify the contact on any type of host
notifications.

Restart the Nagios server

# systemctl restart nagios.service
# systemctl status nagios.service

Now, access the Nagios web interface at “http://Your-server-IP-address/nagios” or
“http://FQDN/nagios.” Log in using the username nagiosadmin and your password,
then verify that the remote Windows host is added and being monitored.

# http://192.168.1.4/nagios/

Now, check the email message in server machine

# cd /home/nagios/Maildir/
# cd new/
# ll
# cat 1735895246.V10302I6127fe4M125449.localhost.bitscentric.com

Leave a Reply

Your email address will not be published. Required fields are marked *