Solved

Shell script to check for a process CPU util & then thread dumps it if it exceeds 30%

Posted on 2011-03-15
5
755 Views
Last Modified: 2012-08-13

Need to run a script every 15 mins in crontab :
0,15,30,45 * * * *  the_script

I think my HP-UX version doesn't support the followg:
ps -eo pid,comm,%cpu


So need a script to grab the %CPU column (think it's 9th or
10th column in top in HP-UX) & if the application concerned
consumes more than 30%, it will thread dump that process :

top –n 99 -f /tmp/topcpu.txt
cpuval=`grep  "myapp" /tmp/topcpu.txt |grep java | grep -v grep| awk or sed
         for the CPU column`

if cpuval > 30  then
   {
     get the pid of myapp from 'ps -ef | grep myapp | grep java | grep -v grep'
     kill -3 $pid
     $DATE > /tmp/log1.txt
     sleep 30  # sleep 30 secs
     kill -3 $pid
     sleep 30
     kill -3 $pid
     echo $cpuval >> /tmp/log1.txt
     mailx -s "myapp cpu utilizn exceed 30%"  myemail@xx.com < /tmp/log1.txt
     mailx ... the threadump file ... to myemail
  }
0
Comment
Question by:sunhux
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:sentner
sentner earned 180 total points
Comment Utility
You should be able to use the standard ps output, but will need to specify to use the standard berkely syntax.


export UNIX95=yes
cpuval=`ps -eo ps -eo pid,comm,pcpu | grep "[m]yapp" | grep java | awk '{print $3}'`


0
 
LVL 3

Accepted Solution

by:
Alberto Cortes earned 320 total points
Comment Utility
Solution:

To create the file topcpu.txt use next command:
UNIX95= ps -e -o "pcpu pid args" | sort -rn | head -99 > /tmp/topcpu.txt

For cpuval use next command:
cpuval=$(cat /tmp/topcpu.txt | grep myapp | awk '{print $1}')

0
 
LVL 14

Expert Comment

by:sentner
Comment Utility
Rico, your solution adds nothing that mine didn't already do, and in fact it adds extra commands that aren't needed.

There's no need to push this first to a file, nor to sort it or to limit it to 99 lines in the ps output (in fact that may cause you to miss what you're looking for).  

The key is the UNIX95 setting.  That can either be set by adding "UNIX95= " (the space after the '=' is important) or by explicitly setting it using "export UNIX95=yes".  I chose the latter, as that's more readable, and this is going into a script.

0
 

Author Comment

by:sunhux
Comment Utility

RicoCebiche solution is fine.



> cpuval=`ps -eo ps -eo pid,comm,pcpu | grep "[m]yapp" | grep java | awk '{print $3}'`
Sentner's solution has slight issue :
"ps -eo" occurred twice & by doing "ps -ef pid,comm,pcpu" too early
the grep "myapp" & grep "java" would not be able to find the search string
as the process/application name columns were truncated too early
0
 

Author Closing Comment

by:sunhux
Comment Utility
Excellent
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now