-->
🏠 🔍
SHAREOLITE

Beginners Linux shell script to check system average CPU , Swap usage

Below mentioned is a ready to use Linux script to get the average CPU usage for the day . The script uses sar command to capture the details which require sysstat rpm. Copy the code below highlighted in blue , save it in a file and execute for output. 

There is a threshold variable provided which you can customize as per requirement. If the average crosses the threshold , output displays red else in green.


#!/bin/sh
threshold=50
cpuusage=`sar -u |grep Average |awk '{print $3}' | awk -F '.' '{print $1}'`
swapusage=`sar -r |grep Average |awk '{print $9}'| awk -F '.' '{print $1}'`
if [ $cpuusage -gt $threshold ] || [ $swapusage -gt $threshold ]
then
echo -e "\033[1;31mAverage CPU usage - $cpuusage% : Average Swap usage - $swapusage%\033[0m"
else
echo -e "\033[1;32mAverage CPU usage - $cpuusage% : Average Swap usage - $swapusage%\033[0m"
fi

Hope it helps to some Linux beginners.
Comments

–>