[Top 3 Monitoring Bash Scripts] CPU, Disk Space and Service Monitoring Bash Scripts for Linux

Overview:

Today, In this Blog we will cover in detail the Top 3 most widely used Shell Scripts in IT Services for Linux OS. Below are the mentioned 3 Bash Scripts that we will understand in detail:

(1) Real Time CPU Monitoring Bash Script

(2) Disk Space Utilization Bash Script

(3) Service Monitoring Bash Script

CPU Monitoring Bash Script

Real Time CPU Monitoring of Running Instances is very vital in IT Infrastructure services especially when you have some critical applications loaded on those Instances. Below is an example of CPU Monitoring Usage, we will now understand line by line the core logic so that on a similar fashion you can also develop the same sort of script:

#!/bin/bash
#Purpose: Real Time CPU Utilization Monitoring
#Version: 1.0
#Auth: Mohit Chaudhary
#Date
# START #

HOSTNAME=$(hostname)
CRITICAL=98
WARNING=90
CRITICALMAIL="YOUREMAIL@DOMAIN.COM"
WARNMAIL="YOURMAIL@DOMAIN.COM"
PATHS="/"

mkdir -p /Users/mohitchaudhary/Downloads/cpu_util
LOGFILE=/Users/mohitchaudhary/Downloads/cpu_util/cpu_usage-`date +%h%d%y.log`
touch $LOGFILE

for path in $PATHS
do
        CPULOAD=`top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 | awk '{print $2}'| awk -F. '{print $1}'`

        if [ -n $WARNING -a -n $CRITICAL ]; then
                if [ "$CPULOAD" -ge "$WARNING" -a "$CPULOAD" -lt "$CRITICAL" ]; then
                        echo "`date "+F %H:%M:%S"` WARNING - $CPULOAD on Host $HOSTNAME " >> $LOGFILE
                        echo "Warning CPULOAD $CPULOAD on Host is $hostname" | mailx -s "CPULOAD is in WARNING state" $WARNMAIL
                        exit 1
                elif [ $CPULOAD -ge $CRITICAL ]; then
                        echo "`date "+F %H:%M:%S"` CRITICAL - $CPULOAD on Host $HOSTNAME " >> $LOGFILE
                        echo "Critical CPULOAD $CPULOAD on Host is $hostname" | mailx -s "CPULOAD is in CRITICAL state" $CRITICAL
                        exit 2
                else
                        echo "`date "+F %H:%M:%S"` OK - $CPULOAD on Host $HOSTNAME " >> $LOGFILE
                        exit 0
                fi
        fi
done

# END #

From Line No. 8 to 13 – we have declared the variables which we have used further in our scripts.

From Line No. 15 to 17 – we have created the log file which we will be used to store the required CPU monitoring logs.

From Line No. 19 to 37 – we have store the CPU Load in the “CPULOAD” variable and in further steps we are just comparing the value with CRITICAL and WARNING values and If it gets matched then we are taking certain actions as shown in the scripts as well as we are sending the emails to Admins.

Disk Space utilization Bash Script

Disk Space Utilization/Monitoring again an important aspect when you have critical sub-sytems/applications in your environment. Below is an example of Disk Space Bash Script, feel free to use it and edit it according to your needs. Let’s understand the core vitals of below script:

#!/bin/bash
#Purpose: Disk Space Utilization Monitoring
#Version: 1.0
#Auth: Mohit Chaudhary
#Date
# START #

THRESHOLD=40
MAILTO="YOURMAIL@DOMAIN.COM"
HOSTNAME=$(hostname)

for fsvalue in `/bin/df -h | grep -vE 'Filesystem|tmpfs' | awk '{print $5}' | sed 's/%//g'`
do
       if [ "$fsvalue" -ge "$THRESHOLD" ]; then
               df -h | grep $fsvalue >> /tmp/fsutil.log
       fi
done

COUNT=`cat /tmp/fsutil.log | wc -l`

if [ $COUNT -ge 1 ]; then
        echo "Mail Dropped"
        mailx -s "$HOSTNAME Disk Space is CRITICAL" $MAILTO < /tmp/fsutil.log
fi

# END #

From Line No. 8 to 10 – we have declared the variables which we have used further in our scripts.

From Line No. 10 to 17 – “fsvalue” variable will hold the Disk Space Utilization value and this value will compared with the “THRESHOLD” variable that we have declared above and the output will be appended to the /tmp/fsutil.log file.

Service Monitoring Bash Script

Service Monitoring scripts will help you to check & monitor specific services which are either Running or NOT Running in your Linux system. Below is the sample script which takes the “service-name” as input as 1st Positional Argument, lets dig deep Line by Line to understand the logic:

#!/bin/bash
#Purpose: Monitoring Services Running on the System
#Version: 1.0
#Auth: Mohit Chaudhary
#Date

if [ $# -eq 0 ]; then
	echo "Usage: $(basename $0) [service-name] "
	exit
fi

service=$(which $1 | cut -d/ -f4)

if [ $? -ne 0 ] || [ -z "$service" ]; then
	echo "service not found...exiting!"
	exit
fi

echo "checking for $service service..."
#checking service
while [ 1 ]
do
	#echo "checking service..."
	ps -ef | grep $service &> /dev/null
	if [ $? -eq 0 ]; then
		echo "$service Service is Running Fine!"
	fi
	# if service stop, attempt to restart 3 times
	if [ $? -ne 0 ]; then
		i=0
		echo "$service stopped.. restarting"
		while [ $i -lt 3 ]
		do
			systemctl restart $service &> /dev/null
			[ $? -eq 0 ] && break
			i=$((i + 1))			
			sleep 1
		done
		#checking if service could not be restarted
		if [ $i == 3 ]; then
			echo "$service could not restart exiting..."
			break
		fi		
	fi
	sleep 3
done

echo "Service $service could not be started. Needs attn" | mail -s "$service stopped" root 

#end 

Line No. 7 to 10 – Checking for “Service-Name” arguments

Line No. 12 – We are checking for installed service in local system

Line No. 14 to 17 – Checking whether service exists on the system or NOT.

Line No. 48 – Will take care of Email to Admin if service failed to restart.

Sample Outputs:

  • Here, we are checking for “syslogd” service and we have pass the same as an argument, below is the ouput:
bash-3.2$ ./service_monitor.sh syslogd
checking for syslogd service...
syslogd Service is Running Fine!
  • Below are examples when we passed the service-name which is NOT present in our system and other when we have NOT passed any arguments:
bash-3.2$ ./service_monitor.sh apache
service not found...exiting!

bash-3.2$ ./service_monitor.sh
Usage: service_monitor.sh [service-name] 
bash-3.2$

Hope above Monitoring Bash scripts help you to automate your day to-day tasks. Cheers!!!

About the author

Mohit Chaudhary

Hey there, I am IT enthusiast who is passionate about Middleware, DevOps, Cloud and much more.

View all posts

Leave a Reply

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