sCfaQK.jpg
“`bash
#!/bin/bash

DATE=$(date +%y%m%d%H%M%S)
stackfile=~/stackfile”$DATE”.txt #jstack file locate.
threadfile=~/threadfile”$DATE”.txt #thread file locate.

SysTouchPeriod=5 #period of loop.(seconds)
CpuTouchTimePeriod=0.5 #inner para,to define two cpu time slice.(seconds)
LimitMemUsedRate=50 #max allowed memory used,included but without %.
LimitCpuUsedRate=80 #max allowed cpu used,included but without %.
CurentMemUsedRate=”” #global init para.
CurentCpuUsedRate=”” #global init para.

#to exec jstack single Bootstrap
function jstackCtpid(){
ctpidname=$(jps -l|grep Bootstrap) #key pid name
ctpid=${ctpidname% *}
jstack $ctpid >>$stackfile
#top -n1bHc -p $ctpid>>$threadfile
ps p $ctpid -L -o pcpu,pid,tid,time,tname,stat,psr | sort -n -k1 -r >>$threadfile
}

function getMemUsedRate(){
LoadMemory=$(cat /proc/meminfo | awk ‘{print $2}’)
Total=$(echo $LoadMemory | awk ‘{print $1}’)
Free1=$(echo $LoadMemory | awk ‘{print $2}’)
Free2=$(echo $LoadMemory | awk ‘{print $3}’)
Free3=$(echo $LoadMemory | awk ‘{print $4}’)

Used=`expr $Total - $Free1 - $Free2 - $Free3`
Used_Rate=`expr  $Used/$Total*100 | bc -l`
CurentMemUsedRate=`expr "scale=3; $Used_Rate/1.0" |bc`  

}

function getCpuUsedRate(){
CPULOG_1=$(cat /proc/stat | grep ‘cpu ‘ | awk ‘{print $2″ “$3” “$4” “$5” “$6” “$7” “$8}’)
SYS_IDLE_1=$(echo $CPULOG_1 | awk ‘{print $4}’)
Total_1=$(echo $CPULOG_1 | awk ‘{print $1+$2+$3+$4+$5+$6+$7}’)

sleep $CpuTouchTimePeriod

CPULOG_2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_2=$(echo $CPULOG_2 | awk '{print $4}')
Total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1`
Total=`expr $Total_2 - $Total_1`
CurentCpuUsedRate=`expr "scale=3; 100-$SYS_IDLE/$Total*100" |bc -l`

}

while :
do
ctpidname=$(jps -l|grep Bootstrap) #key pid name
if [ “$ctpidname” = “” ]; then
echo no process found!
sleep $SysTouchPeriod
continue
fi
getMemUsedRate;
getCpuUsedRate;
if [ echo "$CurentMemUsedRate > $LimitMemUsedRate" | bc -eq 1 ]||[ echo "$CurentCpuUsedRate > $LimitCpuUsedRate" | bc -eq 1 ]; then
jstackCtpid;
fi
sleep $SysTouchPeriod
done

“`

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code