[Day 11] 4. Nagios Lab with Email Alert
Objective
Ensure an email is sent whenever the server fails, or an alert is generated for power consumption or CPU utilization.
Pre-requisites
- A properly functioning mail server.
- Nagios configured and set up to monitor a Linux machine remotely.
Steps
Step 1: Install Swaks
Swaksbashsudo 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
bashswaks --to usertwo@shuharilabs.local --from userone@shuharilabs.local --auth --auth-user=userone --auth-password=toor --server sunbeam.shuharilabs.com
- Sends an email from to
userone.usertwo - Authenticates with the password
userone.toor - Uses the SMTP server .
sunbeam.shuharilabs.com
Step 3: Configure Nagios Email Notifications
Edit Contacts File
Modify the
contacts.cfgbashsudo vim /usr/local/nagios/etc/objects/contacts.cfg
In the
define contactplaintextemail usertwo@shuharilabs.local
Add Notification Options
Add the following options to define when notifications should be sent:
plaintexthost_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
commands.cfgStep 4: Configure Resource File
Set up usernames, passwords, SMTP server details, and sender/recipient email IDs.
Open the
resource.cfgbashsudo 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:
bashsudo vim /usr/local/nagios/etc/servers/deb_12-1.cfg
Add the following lines after the
max_check_attemptsplaintextnotifications_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
swaksCommand with Subject and Body
bashswaks --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:
bashecho "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
bashecho "[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
notify-host-by-emailEdit the
commands.cfgnotify-host-by-emailplaintext/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:
- : Type of notification (e.g., PROBLEM, RECOVERY).
$NOTIFICATIONTYPE$ - : Hostname of the machine being monitored.
$HOSTNAME$ - : Current state of the host (e.g., UP, DOWN).
$HOSTSTATE$ - : IP address of the host.
$HOSTADDRESS$ - : Additional information about the host's status.
$HOSTOUTPUT$ - : Timestamp of the notification.
$LONGDATETIMES$ - : Email address of the contact receiving the notification.
$CONTACTEMAIL$ - : Credentials and server details configured earlier in
$USER5$, $USER6$, $USER7$, $USER8$.resource.cfg
Flow of the Lab: How It Works
-
Email Setup:
- Use to test email functionality. This ensures that the email server is correctly configured and can send messages.
swaks
- Use
-
Nagios Configuration:
- Configure to set up recipient details and notification options.
contacts.cfg - Update server-specific configuration files (e.g., ) to enable notifications and define when and how alerts are sent.
deb_12-1.cfg
- Configure
-
Command Definition:
- The command in
notify-host-by-emailusescommands.cfgto send emails dynamically. Variables likeswaksand$NOTIFICATIONTYPE$are populated at runtime based on the alert.$HOSTNAME$
- The
-
Resource File (
):resource.cfg- Securely stores sensitive credentials and server details used by Nagios to send email notifications.
-
Linking:
- Alerts generated by Nagios trigger the command.
notify-host-by-email - This command uses credentials and settings from to send detailed notifications via email.
resource.cfg
- Alerts generated by Nagios trigger the
Read Next
[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)...
[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...