Swap Monitoring: Keep Your System Performance in Check

How to Swap Monitoring: Keep Your System Performance in Check

Swap Monitoring your system’s resources is important to keep it running well. In this script we monitor the swap usage, You’ll get notifications for warning and critical levels to help maintain system performance.

Objective :

  • Monitor swap usage percentage.
  • Send email alerts for high swap usage.
  • Provide detailed swap information in the email.
  • Generate HTML formatted alerts for better readability.

Prerequisites :

  • A Linux-based system with a Bash shell.
  • ‘Sendmail’ installed for sending emails.
  • Access to the terminal with sufficient permissions.

Notes :

  • Please change the threshold values according to your requirements.
  • Update the email addresses of your desired recipients.
  • Customize the HTML and color codes as needed.
  • Understand each command before executing the script to ensure it fits your environment and requirements

ALSO READ: 

Example output :

swap Critical  Alert
Critical Alert
swap Warning Alert
Warning Alert

Script :

#!/bin/bash
#################################################################
# Date: 15-July-2024
# Author: Krishna Tummeti
# Website: Tech Base Hub
# Purpose: Swap Monitoring: Keep Your System Performance in Check
#################################################################
# Check the swap usage in percentage 
swap_usage=$(free -m | awk 'NR==3 {print int($3*100/$2)}') 
# Set the threshold limits 
critical_threshold=25 
warning_threshold=20 
# Enter your email IDs 
recipient="krishnatummeti@gmail.com,krishnatummeti@outlook.com" 
# Check swap usage details 
swap_info=$(free -g | awk 'NR==3 {print "Total: " $2" GB, Free: " $4" GB, Used: " $3 " GB"}') 
# Determine the alert level based on swap usage 
if [[ "$swap_usage" -gt "$critical_threshold" ]] 
then 
  notification="Critical Alert: Swap usage" status="<span class=\"critical\"> <i><b>Swap usage: $swap_usage%</b></i> </span>"
  subject="Swap Usage Critical Alert $(hostname)" 
elif [[ "$swap_usage" -gt "$warning_threshold" ]] 
then 
  notification="Warning Alert: Swap usage" status="<span class=\"warning\"> <i><b>Swap usage: $swap_usage%</b></i> </span>" 
  subject="Swap Usage Warning Alert $(hostname)" 
else 
  exit 1 # Exit if swap usage is below the threshold 
fi 
# HTML formatted email body 
body=$(cat <<EOF 
<html> 
  <style> 
  body { background-color: #F5F5F5; } 
  .critical { color: #FF0000; } 
  .warning { color: #FFA500; } 
  th { background-color: #DCDCDC; } 
  td { background-color: #DCDCDC; } 
</style>
  <body>
    <h3>Notification Type: $notification</h3>
    <table border='1' width='80%'> 
    <tr> 
      <th nowrap>Reported date:</th>
      <td nowrap>$(date +"%Y-%b-%d %H:%M:%S")</td>
    </tr>
    <tr>
      <th nowrap>Host Name:</th>
      <td nowrap>$(hostname)</td>
    </tr>
    <tr>
      <th nowrap>IP Address:</th>
      <td nowrap>$(hostname -I | awk '{print $1}')</td> </tr> <tr> <th nowrap>Status:</th>
      <td nowrap>$status ($swap_info)</td>
    </tr>
    <tr>
      <th nowrap>Rule Owner:</th>
      <td nowrap>System Generated</td>
     </tr>
  </table>
</body>
</html> EOF ) 
# Mail header
to_mail="To: $recipient" 
# Send email 
echo -e "Subject: $subject\nContent-type: text/html\n$to_mail\n\n$body" | sendmail -t
# Save the body to a text file
echo "$body" > /home/oracle/krishna/swap_alert.txt
 
Make the Script Executable :
chmod +x swap_alert.sh
Schedule the Script  :
crontab -e
Add the following line to run it every hour/
0 * * * * /path/to/swap_alert.sh
Now we can successfully ‘Swap Monitoring‘ the usage every hour.
 
Thank You
 

Leave a Comment