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 scriptdt=`date +%Y-%m-%d`host_name=`hostname`S_uptime=`uptime | awk -F [," "] '{print $4 " " $5 "," $7}'`DIR=/var/log/performance_monitoringFILE=$DIR/$host_name-PerfMon-$dtP_FILE=$DIR/$host_name-PerfMon-Process-$dtb_old=$(tput bold)n_ormal=$(tput sgr0)HOUR_MIN_SEC=`/usr/bin/date +%T`# To validate & create Log Directoryif [ ! -d "$DIR" ];thenmkdir -p $DIRfi# To validate & create Log Fileif [ ! -e "$FILE" ];thenheader="%-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" > $FILEfi#Calculating the CPU , MEM and Swap statsNet_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.txtread a1 a2 a3 a4 a5 a6 < /tmp/vmstat_temp.txt# Formatting the O/P to Log fileecho -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" >> $FILEtouch $P_FILEchmod 660 $P_FILEchmod 660 $FILE# Top 20 CPU and Memory calculationif [ $Net_MEM_Pct -ge 60 ] || [ $a5 -le 20 ]; thenecho -e " ${b_old}Server_Name:- ${n_ormal} $host_name" >> $P_FILEecho -e " ${b_old}Date:- ${n_ormal} $dt $HOUR_MIN_SEC" >> $P_FILEecho -e " ${b_old}CPU Percentage:- ${n_ormal} $a3" >> $P_FILEecho -e " ${b_old}Memory Used Percentage:- ${n_ormal} $Net_MEM_Pct" >> $P_FILEecho -e " ${b_old}Swap Used Percentage:- ${n_ormal} $Swap_mem_Pct " >> $P_FILEecho -e " =======================================================================================================================================================" >> $P_FILEecho -e " \t \t \t \t \t \t \e[92m ${b_old}TOP 20 Memory Consuming Process\e[39m ${n_ormal} " >> $P_FILEecho -e " =======================================================================================================================================================" >> $P_FILEps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem | head -20 >> $P_FILEecho -e " =======================================================================================================================================================" >> $P_FILEecho -e " \t \t \t \t \t \t \e[92m ${b_old}TOP 20 CPU Consuming Process\e[39m ${n_ormal} " >> $P_FILEecho -e " =======================================================================================================================================================" >> $P_FILEps -eo pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head -20 >> $P_FILEecho -e " \e[96m ${b_old}======================================================================================================================================================= \e[39m ${n_ormal} " >> $P_FILEfi#Log rotation for every 90 daysfind $DIR -type f -mtime +90 -print -exec rm -f {} \;