SHELL SCRIPT TO MONITOR HIGHCPU IN LINUX

SHELL SCRIPT TO MONITOR HIGHCPU IN LINUX

This shell script is going to help us in identifying high consistent CPU usage due to concurrent peak process on linux

You can modify the samples and time interval accordingly according to your system. Based on your projects criticality of business, you can determine the number of booms and threshold for CPU to be printed to logfile when idle% < 5. You can keep idle%=0 and decrease number of booms to 10 depending on your situation…

Note: If your system is already CPU bound, then it is not recommended to run this script because of multiple loops in the script

#!/bin/bash -x
#Create log directory
idir=/home/oracle/idle
#Loop the CPU utilization using vmstat statistics for every 5 seconds with 30 samples and fetch idle CPU < 5%
while true
  do
   #remove the old logfiles
   rm -rf idle highcpu
    vmstat 5 30|awk '{print $15}' >> $idir
   #delete empty spaces and column name 'id' from output file
   sed -i '/^$/d' $idir
   sed -i '/id/d' $idir
  cat $idir|grep -v 'id'|grep -v '^$' > idlelast
   #if idle CPU is less than 5%,then print boom to output file
cat idlelast|while read li;do if [ $li -lt 5 ];then echo "boom" >> highcpu;fi
   #count the number of boom from logfile
   report=`grep 'boom' highcpu|wc -l`
   #if there are more than 20 booms from output file, then CPU usage is high
  if [ $report -gt 20 ]
   then
  echo 'ALERT:HIGHCPU'|mail -s 'HIGH CPU on `hostname`' xxx.gmail.com
  fi
   #clean up lines from log
   #sed -i '1,11d' idlelast
     cleanup=`cat idlelast |wc -l`
    lastclean=$((cleanup))
    l="1,$cleanup"
   if [ $cleanup -gt 10 ];then sed -i "$l"'d' idlelast;fi
   sleep 30
   #print top CPU users
   echo 'TOP 5 CPU USERS' && ps -eo pcpu,user|sort -nr|head -5
  done
done

SAMPLE OUTPUT:

Temporary output file from the script

[oracle@exdbadm01 ~]$ cat idlelast
81
91
90
94
95
91
92
95
88
92

Top CPU users

TOP 5 CPU USERS
 0.6 root
 0.3 oracle
 0.2 oracle
 0.2 oracle
 0.1 root

Execution of script:

+ idir=/home/oracle/idle
+ true
+ rm -rf idle highcpu
+ vmstat 5 30
+ awk '{print $15}'
+ sed -i '/^$/d' /home/oracle/idle
+ sed -i /id/d /home/oracle/idle
+ cat /home/oracle/idle
+ grep -v id
+ grep -v '^$'
+ cat idlelast
+ read li
+ '[' 89 -lt 5 ']'
++ grep boom highcpu
++ wc -l
+ report=0
+ '[' 0 -gt 20 ']'
++ cat idlelast
++ wc -l
+ cleanup=30
+ lastclean=30
+ l=1,30
+ '[' 30 -gt 10 ']'
+ sed -i 1,30d idlelast
+ sleep 30
+ echo 'TOP 5 CPU USERS'
TOP 5 CPU USERS
+ ps -eo pcpu,user
+ sort -nr
+ head -5
 0.5 root
 0.1 root
 0.1 root
 0.1 root
 0.1 oracle

Leave a Reply

%d bloggers like this: