Centos 6 – Script for Automatic Email Alerts notifying for low CPU – RAM – Hard Drive resources.

The script does the following:

– Checks for historical data for when a CPU has reached 50% usage.

– Checks if RAM usage has gone beyond 85%

– Checks if Hard Drive space in all of your partitions has gone beyond 85%

This is a first release of a script I am working on. This is not a finished “product”.  More automation will be added to new releases. This is just a test run. A v0.1 release if you will. You can download the script and place it which ever directory you wish. You can either automatically run it by inserting a CRON job or run it manually. Please note that if you do not receive any emails it is either because you do not have MAILX installed or your ISP (wisely) has blocked unauthorized SMTP emails.

Please note that the RAM notification usage alarm is something to be read with caution because the Operating System will use as much RAM as it needs and release it accordingly. For example if you are running a Database Engine on any Operating System, the Database Engine will request and receive an enormous amount of RAM. If the Operating System sees that it needs part of that allocated RAM for another process is will allocate it to that process. Do not see high usage of RAM as an alarming event. Use your judgement.

 

The script does not make any modifications to your system what so ever.

 

Download Link:

http://www.sfentona.net/monitor_functions.sh

 

 

############################################
#This script is broken down into functions #
#which are called by an outside script #
############################################
############################################
#Memory Usage Check Function
#Captures Memory usage in precentage
#Send an email alert if memory usage exceeds 85%
################################################
memory_check(){
total_ram=`cat /proc/meminfo | grep ‘MemTotal’ | awk ‘{print $2}’`;
free_ram=`cat /proc/meminfo | grep ‘MemFree’ | awk ‘{print $2}’`;
used_ram=$[ $total_ram – $free_ram ] mem_precent=$[ $used_ram * 100 / $total_ram] if [ $mem_precent -gt 85 ] then
echo “Memory Usage Exceeds $used_ram. Used Memory is at $mem_precent%” > memory_alert
mailx -s “MEMORY RESOURCE ALARM” ENTER_YOUR_EMAIL < memory_alert
else
exit;
fi
exit;
}
########################################
#CPU Usage Check Function
#Captures Historical CPU Data
#Sends an email alert if CPU exceeds 50%
########################################
cpu_usage(){
user_cpu_usage=`sar -u | awk ‘{print $4}’`
system_cpu_usage=`sar -u | awk ‘{print $5’`
if [ $user_cpu_usage -gt 50 II $system_cpu_usage -gt 50 ]] then
sar -u | awk ‘$4>49 || $5>49 {print $1,$2,$4,$5,$9}’ > cpu_usage_report
mailx -s “CPU RESOURCE ALARM” ENTER_YOUR_EMAIL < cpu_usage_report
else
exit;
fi
exit;
}
########################################
#Hard Drive Usage check Function
#Searches for all Hard Drives for space
#Sends an email alert if used space exceeds 85%
#######################################
hd_usage(){
#hd_usedspace_precentage=`df -h | grep ‘[0-9]%’ | grep ‘95%’`
hd_usedspace_precentage=`df -h | egrep “(100%|[80][5-9]%|[90][0-9]%)”`
if [ $? -eq 0 ] then
echo $hd_usedspace_precentage > hd_used_precentage_report
mailx -s “HARD DRIVE RESOURCE ALARM” ENTER_YOUR_EMAIL < hd_used_precentage_report
else
exit;
fi
exit;
}
#Function Calling
memory_check
cpu_usage
hd_usage

 

 

 

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.