script to check high cpu utilization for java process

Hello Team,

I need help in preparing script to check for high cpu utilisation for java process.

I have many java process on my system which consumes high cpu so i have to monitor

it using script.

---------- Post updated 12-10-10 at 02:21 AM ---------- Previous update was 12-09-10 at 09:08 AM ----------

Hello Team,

I got the command to check high cpu usage by java process.The command and it's output is as shown below.

bash-3.00$ ps -eo pid,pcpu,args | sort +1n | grep -i java
3385 0.0 grep -i java
8804 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -XX:+UnlockDiag
9241 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -XX:+UnlockDiag
18558 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/java -Xms128m -Xmx512m -DPOL
26719 0.0 /usr/jdk1.6.0_18/bin/java -Djava.compiler=NONE -Djava.security.auth.login.confi
64 6.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -server -Dwas.s
bash-3.00$

I have requirement to develope script in such a way that it should check output of above command for high cpu and should send an alert saying java process with this pid is consuming high cpu resources after comparing it with thershhodl value.

You are on the right track. Once you have a list, you save it for the next pass, say a minute later, and then if they change too much between passes, you could alert. For instance, at one minute intervals, 50% of one CPU would be 30 seconds. You might want to have an alerts sent file, or empty flag file in a flag dir, to reduce alerts every minute from the same problem proc to once per clock hour or so. You might want to send one updated list of all problem procs, not an alert per. Maybe make the list of all every minute (without time stamps and headers), and if it is the either different (comm -3 file1 file2 or just cmp) from a minute ago or both the first minute of the hour and not empty, then email it out (with time stamp and header, host name first in subject so testing and prod are not confused).