NDC & PKI

[Day 11] 4. Nagios Lab with Email Alert

D
DITISS Admin
••8 min read

Objective

Ensure an email is sent whenever the server fails, or an alert is generated for power consumption or CPU utilization.


Pre-requisites

  1. A properly functioning mail server.
  2. Nagios configured and set up to monitor a Linux machine remotely.

Steps

Step 1: Install Swaks

Swaks
(Swiss Army Knife SMTP) is a command-line utility for testing SMTP servers.

bash
sudo apt-get install swaks

Step 2: Verify Email Functionality

Use the same user accounts created in the PKI Lab for the mail server. Empty both the inbox and outbox for clarity during testing.

Command to send a test email
bash
swaks --to usertwo@shuharilabs.local --from userone@shuharilabs.local --auth --auth-user=userone --auth-password=toor --server sunbeam.shuharilabs.com
  • Sends an email from
    userone
    to
    usertwo
    .
  • Authenticates
    userone
    with the password
    toor
    .
  • Uses the SMTP server
    sunbeam.shuharilabs.com
    .

Step 3: Configure Nagios Email Notifications

Edit Contacts File

Modify the

contacts.cfg
file in Nagios to specify the email address for notifications.

bash
sudo vim /usr/local/nagios/etc/objects/contacts.cfg

In the

define contact
section, update the email field with the recipient's email:

plaintext
email usertwo@shuharilabs.local
Add Notification Options

Add the following options to define when notifications should be sent:

plaintext
host_notification_period      24x7
host_notification_options     d,u,r,f,s
host_notification_commands    notify-host-by-email

Explanation of options:

  • d: DOWN (host is down).
  • u: UNREACHABLE (host is unreachable).
  • r: Host recoveries (UP states).
  • f: Host starts or stops flapping.
  • s: Scheduled downtime starts or ends.

notify-host-by-email
: The command to send the email, defined in
commands.cfg
.


Step 4: Configure Resource File

Set up usernames, passwords, SMTP server details, and sender/recipient email IDs.

Open the

resource.cfg
file:

bash
sudo vim /usr/local/nagios/etc/resource.cfg

Add the following at the end of the file:

plaintext
# Store some usernames and passwords (hidden from the CGIs)

# User ID
$USER5$=userone  

# Password
$USER6$=toor  

# SMTP Server and Port
$USER7$=192.168.0.52:25  

# Sender Email Address
$USER8$=userone@shuharilabs.local

Step 5: Update Server Configuration

To enable notifications and define notification behavior for a specific server, update its configuration.

Edit the server-specific configuration file:

bash
sudo vim /usr/local/nagios/etc/servers/deb_12-1.cfg

Add the following lines after the

max_check_attempts
line:

plaintext
notifications_enabled      1
notification_period        24x7
notification_interval      1
check_interval             1
notification_options       d,u,r,f,s
contacts                   nagiosadmin

Explanation:

  • notifications_enabled: Enables or disables notifications.
  • notification_period: Specifies when notifications are allowed (24x7 = always).
  • notification_interval: Time (in minutes) between repeated notifications.
  • check_interval: Frequency (in minutes) to check the host's status.
  • notification_options: Defines events triggering notifications (same as earlier: d, u, r, f, s).
  • contacts: Specifies the contact(s) to be notified, in this case,
    nagiosadmin
    .

Step 6: Test Email with Subject and Body

To add a subject to the email sent using the

swaks
command, use the following syntax:

Command with Subject and Body
bash
swaks --to usertwo@shuharilabs.local --from userone@shuharilabs.local --auth --auth-user=userone --auth-password=toor --server sunbeam.shuharilabs.com --h-Subject "Your Mail Subject" --body "Body Message"
Command with Custom Body Message via Echo

If you prefer to write the body message separately:

bash
echo "Hello, this is a body message" | swaks --to usertwo@shuharilabs.local --from userone@shuharilabs.local --auth --auth-user=userone --auth-password=toor --server sunbeam.shuharilabs.com --h-Subject "Your Mail Subject" --body -
Example of a Detailed Body Message
bash
echo "[GABBAR IS HERE]\n\nFull Name: Gabbar Singh\nAge: 30\nOccupation: Daku\n\n\n--------Thank You :)--------" | swaks --to usertwo@shuharilabs.local --from userone@shuharilabs.local --auth --auth-user=userone --auth-password=toor --server sunbeam.shuharilabs.com --h-Subject "Your Mail Subject" --body -

Step 7: Modify the
notify-host-by-email
Command

Edit the

commands.cfg
file to customize the
notify-host-by-email
command:

plaintext
/usr/bin/echo -e "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIMES$\n" | /usr/bin/swaks --to $CONTACTEMAIL$ --from $USER8$ --server $USER7$ --auth LOGIN --auth-user $USER5$ --auth-password $USER6$ --h-Subject "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" --body -

Explanation of Variables:

  • $NOTIFICATIONTYPE$
    : Type of notification (e.g., PROBLEM, RECOVERY).
  • $HOSTNAME$
    : Hostname of the machine being monitored.
  • $HOSTSTATE$
    : Current state of the host (e.g., UP, DOWN).
  • $HOSTADDRESS$
    : IP address of the host.
  • $HOSTOUTPUT$
    : Additional information about the host's status.
  • $LONGDATETIMES$
    : Timestamp of the notification.
  • $CONTACTEMAIL$
    : Email address of the contact receiving the notification.
  • $USER5$, $USER6$, $USER7$, $USER8$
    : Credentials and server details configured earlier in
    resource.cfg
    .

Flow of the Lab: How It Works

  1. Email Setup:

    • Use
      swaks
      to test email functionality. This ensures that the email server is correctly configured and can send messages.
  2. Nagios Configuration:

    • Configure
      contacts.cfg
      to set up recipient details and notification options.
    • Update server-specific configuration files (e.g.,
      deb_12-1.cfg
      ) to enable notifications and define when and how alerts are sent.
  3. Command Definition:

    • The
      notify-host-by-email
      command in
      commands.cfg
      uses
      swaks
      to send emails dynamically. Variables like
      $NOTIFICATIONTYPE$
      and
      $HOSTNAME$
      are populated at runtime based on the alert.
  4. Resource File (

    resource.cfg
    ):

    • Securely stores sensitive credentials and server details used by Nagios to send email notifications.
  5. Linking:

    • Alerts generated by Nagios trigger the
      notify-host-by-email
      command.
    • This command uses credentials and settings from
      resource.cfg
      to send detailed notifications via email.

Read Next

8/26/2026

[Day 11] 5. Extra

The **ISAKMP (Internet Security Association and Key Management Protocol)** is a protocol used in the context of **IPSec** (Internet Protocol Security)...

8/26/2026

[Day 12] 1. Adding another client PKI Lab

To create and configure a new client with your Root CA and Sub CA in a Public Key Infrastructure (PKI) setup, follow the steps below: ### Step 1: C...