Linux Monitor Memory Usage Alert Script

Automating Linux monitor memory usage script

Daily monitoring of memory usage is a crucial task for the Linux Monitor Memory Usage Script. This post provides a bash script that monitors memory usage and alerts when the threshold limit is exceeded. This script is handy for administrators, as it automates the process and saves time.

Linux Monitor Memory Usage Objective

Linux Monitor Memory Usage alert (warning and critical)

Prerequisites

  • Basic knowledge of bash script
  • Access the UNIX-based system eg LINUX
  • Install sendmail services 

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.

The Example Output Mails Warning and Critical 

Critical  Alert
Critical  Alert
Warning Alert
Warning Alert

Script 

#!/bin/bash
############################################################
# Date     : 30-may-2024
# Author   : Krishna Tummeti
# Website  : Tech Base Hub
# Purpose  : Memory usage Alert Warning and Critical state
############################################################
# Check the memory usage in the percentage 
memory_usage=$(free -m | awk 'NR==2 {print int($3*100/$2)}')

# Set the threshold limit 
critical_threshold=25
warning_threshold=20

# Enter your email id
recipient="Your_email@gmail.com,Your_email@outlook.com"

# check the memory usage details information
memory_info=$(free -g | awk 'NR==2' | awk '{print "Available: " $7 " GB, Total: " $2" GB, Free : " $4" GB, Used: " $3 " GB"}')

# If you want debug the script uncomment it below line 
# set -x

# Using the if condition to detemind the critical warning threshold
if [[ "$memory_usage" -gt "$critical_threshold" ]]
then
      notification="Critical Alert Memory usage"
      status="<span class="critical">
                 <i><b>Memory usage: $memory_usage%</b></i>
                 </span>"
      subject="Memory usage Critical Alert $(hostname)"
elif [[ "$memory_usage" -gt "$warning_threshold" ]]
then
      notification="Warning Alert Memory usage"
      status="<span class="warning">
                 <i><b>Memory usage: $memory_usage%</b></i>
                 </span>"
      subject="Memory usage Warning Alert $(hostname)"
else
      exit 1 # exit if memory usage is below the threshold
fi

# html formate mail body 
body=$(echo -e "
<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 Addres:</th>
                            <td nowrap>$(hostname -I | awk '{print $1}')</td>
                        </tr>
                        <tr>
                            <th nowrap>Status :</th>
                            <td nowrap>"$status" ($memory_info)</td>
                        </tr>
                        <tr>
                            <th nowrap>Rule Owner :</th>
                            <td nowrap>System Genrated</td>
                        </tr>
           </body>
</html>")

# Mail header
to_mail="To: $recipient"

# sendmail
echo -e "subject:$subject\nContent-type: text/html\n$to_mail\n$body" | sendmail -t

Leave a Comment