Saturday, January 08, 2011

CPU utilization on linux



To find out processes, which have exceeded CPU utilization threshold on the server.

In the example,

a) CPU utilization of java processes are checked. You may replace it for any other process.

b) Also, input the threshold value



This will list all the CPU processes which has exceeded threshold limit
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold= '$2 > threshold {print "pid " $1,"has crossed the threshold value of cpu " $2;}'

This will list all the java processes and their CPU utlixation
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold= '$2 > threshold {print $1,"has crossed the threshold value of cpu";} $2 <= threshold {print;}' > outputFile

This will put all alerts and command outout in same file
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold= '$2 > threshold {print $1,"has crossed the threshold value of cpu";} $2 <= threshold {print;}' > outputFile

if you need it in different files, then
All alerts in one file
 ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold= '$2 > threshold {print $1,"has crossed the threshold value of cpu";}' > alertFile


Rest in another file
 ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold= '$2 <= threshold {print;}' > outputFile

And content of alertFile can be sent as email using mailx command.
 /usr/sbin/mailx -s "Your Subject" ADDRESS_EMAIL < alertFile

# the end

Hope this helps. Rupam