Real Time CPU Utilization Monitoring and Store History Linux Script

Most of the times whenever server is hung or CPU Utilization went to peak even we can’t login to server via SSH or other channels. How your going to dig the issue.? of course you have an sar command but still it is not that easy/ straight forward to know detailed information. To make your work easy on Root Cause Analysis use this updated shell script to Monitor Real Time CPU Utilization Monitoring and Store history Linux Script.

Real Time CPU Utilization Monitoring And Store History Linux Script

In this shell script we are not going to install any extra packages because going to utilize existing command called top command

#!/bin/bash
##Author: Ankam Ravi Kumar
##WebSite: https://arkit.co.in - Arkit
##PurPose: Real time CPU Utilization Monitoring Shell Script
##Date: 13th Nov 2017

HOSTNAME=`hostname`
PATHS="/"
WARNING=90
CRIT=98
CAT=/bin/cat
MAILER=/bin/mail
CRITmailto="YOUREMAIL@DOMAIN.COM"
mailto="YOUREMAIL@DOMAIN.COM"
mkdir -p /var/log/cpuhistory
LOGFILE=/var/log/cpuhistory/hist-`date +%h%d%y`.log
touch $LOGFILE

for path in $PATHS
do
CPU_LOAD=`top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 | awk '{print $2}' |awk -F. '{print $1}'`
if [ -n "$WARNING" -a -n "$CRIT" ]; then
 if [ "$CPU_LOAD" -ge "$WARNING" -a "$CPU_LOAD" -lt "$CRIT" ]; then
 echo " `date "+%F %H:%M:%S"` WARNINGING - $CPU_LOAD on Host $HOSTNAME" >> $LOGFILE
echo "CPU Load is Warning $CPU_LOAD on $HOSTNAME" | $MAILER -s "CPU Load is Warning $CPU_LOAD on $HOSTNAME" $mailto
exit 1
elif [ "$CPU_LOAD" -ge "$CRIT" ]; then
 echo "`date "+%F %H:%M:%S"` CRITICAL - $CPU_LOAD on $HOSTNAME" >> $LOGFILE
echo "CPU Load is Critical $CPU_LOAD on $HOSTNAME" | $MAILER -s "CPU Load is Critical $CPU_LOAD on $HOSTNAME" $CRITmailto
exit 2
else
echo "`date "+%F %H:%M:%S"` OK - $CPU_LOAD on $HOSTNAME" >> $LOGFILE
exit 0
fi
fi
done

How To Use CPU utilization Monitoring Script

Create file as mentioned like below

# touch cpuutilarkit.sh

Copy above shell script and paste it in file

# vi cpuutilarkit.sh

### PASTE SCRIPT HERE ###

:wq

Now assign Execution permissions to the file using below command

# chmod u+x cpuutilarkit.sh

Run it

# sh -x cpuutilarkit.sh

If you want to schedule refer this crontab scheduling guide

That’s it.

Conclusion

Above script is tested and validated only on Red hat enterprise Linux 5.x/6.x and 7.x. If you want to use it in Ubuntu you can use it. Real time cpu utilization monitoring.

Related Articles

Linux Step by Step Guide

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

1 Response

  1. ravikumar says:

    i woulld ike to montor few things on different server – could you please help me in writing a bash script for the following scenario: Bash script for monitoring CPU RAM Disk linux server
    Need to get the CPU & RAM & Disk usage details along with the date for multiple servers using shell script and script should be executed from the one server.

Leave a Reply

Your email address will not be published. Required fields are marked *