Script to capture top 10 performance usage

#################################################################################################
# Program: performance_mon.sh #
# Directory: /usr/local/bin #
# Purpose: (1) To Monitor the CPU, MEM & Swap percentage #
# (2) To Capture the Top 20 CPU and Memory process if threshold are met #
# Called By: Cron #
# Calls: N/A #
# Requirement: This script is developed for RHEL 7.1 and above , lower versions requires #
# modification on calculation part #
#################################################################################################
# Defining the Variables required for the script
dt=`date +%Y-%m-%d`
host_name=`hostname`
S_uptime=`uptime | awk -F [," "] '{print $4 " " $5 "," $7}'`
DIR=/var/log/performance_monitoring
FILE=$DIR/$host_name-PerfMon-$dt
P_FILE=$DIR/$host_name-PerfMon-Process-$dt
b_old=$(tput bold)
n_ormal=$(tput sgr0)
HOUR_MIN_SEC=`/usr/bin/date +%T`
# To validate & create Log Directory
if [ ! -d "$DIR" ];
then
mkdir -p $DIR
fi
# To validate & create Log File
if [ ! -e "$FILE" ];
then
header="%-10s %10s %15s %15s %15s %15s %15s %15s %15s \n"
printf "$header" "TIME" "Run_Queue" "Block_Queue" " CPU_USER_PCT" "CPU_SYS_PCT" "CPU_IDLE_PCT" "CPU_WAIT_PCT" "MEM_PCT" "SWAP_PCT" > $FILE
fi
#Calculating the CPU , MEM and Swap stats
Net_MEM=`free -m | awk 'NR==2{printf "Net Memory Usage: %s/%sMB %.3f%%\n", $3,$2,$3*100/$2 }'`
Net_MEM_Pct=`echo $Net_MEM | awk -F '[." "]' '{print $5}'`
Swap_mem=`free -m | awk 'NR==3{printf "Swap Usage: %s/%sMB %.3f%%\n", $3,$2,$3*100/$2 }'`
Swap_mem_Pct=`echo $Swap_mem | awk -F '[." "]' '{print $4}'`
vmstat 2 2 |awk 'NR==4{print $1, $2, $13, $14, $15, $16}' > /tmp/vmstat_temp.txt
read a1 a2 a3 a4 a5 a6 < /tmp/vmstat_temp.txt
# Formatting the O/P to Log file
echo -e "$HOUR_MIN_SEC \t$a1 \t\t$a2 \t\t$a3 \t\t$a4 \t\t$a5 \t\t$a6 \t\t$Net_MEM_Pct \t\t$Swap_mem_Pct" >> $FILE
touch $P_FILE
chmod 660 $P_FILE
chmod 660 $FILE
# Top 20 CPU and Memory calculation
if [ $Net_MEM_Pct -ge 60 ] || [ $a5 -le 20 ]; then
echo -e " ${b_old}Server_Name:- ${n_ormal} $host_name" >> $P_FILE
echo -e " ${b_old}Date:- ${n_ormal} $dt $HOUR_MIN_SEC" >> $P_FILE
echo -e " ${b_old}CPU Percentage:- ${n_ormal} $a3" >> $P_FILE
echo -e " ${b_old}Memory Used Percentage:- ${n_ormal} $Net_MEM_Pct" >> $P_FILE
echo -e " ${b_old}Swap Used Percentage:- ${n_ormal} $Swap_mem_Pct " >> $P_FILE
echo -e " =======================================================================================================================================================" >> $P_FILE
echo -e " \t \t \t \t \t \t \e[92m ${b_old}TOP 20 Memory Consuming Process\e[39m ${n_ormal} " >> $P_FILE
echo -e " =======================================================================================================================================================" >> $P_FILE
ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head -20 >> $P_FILE
echo -e " =======================================================================================================================================================" >> $P_FILE
echo -e " \t \t \t \t \t \t \e[92m ${b_old}TOP 20 CPU Consuming Process\e[39m ${n_ormal} " >> $P_FILE
echo -e " =======================================================================================================================================================" >> $P_FILE
ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head -20 >> $P_FILE
echo -e " \e[96m ${b_old}======================================================================================================================================================= \e[39m ${n_ormal} " >> $P_FILE
fi
#Log rotation for every 90 days
find $DIR -type f -mtime +90 -print -exec rm -f {} \;