-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjmap.javaProject.sh
More file actions
executable file
·164 lines (150 loc) · 4.34 KB
/
jmap.javaProject.sh
File metadata and controls
executable file
·164 lines (150 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env bash
which jmap >/dev/null && : || exit 0
Count=0 ; Break=1 ; waitTime=10 ; CpuUseAlarm=10
maxSleepCount=1000 ; minFreeMem=365
projectName='java' ; Site="${projectName}A"
# projectName=("etermconfig" | "tomcat" | "server-rpc-project.jar" \
# | "..." | "All JAVA projects")
if [ $# -eq 1 ];then
projectName="$1" ; Site="${projectName}A"
else
projectName="${projectName}"
fi
Help () {
echo "$0 [tomcat|server-rpc-project.jar|...|All_Java_Projects]"
}
Dump () {
whoAmi=`whoami`
userPid=(`ps -ef |grep java|grep "$projectName" |grep -Ev \
"(grep|$0)" |awk '{print $1,$2}'`)
#runPid=${userPid##*\ } ; runUser=${userPid%%\ *}
if [ ${#userPid[@]} -ge 2 ]; then
runUser="${userPid[0]}"
runPid=${userPid[1]}
if [ $runPid -le 1 ] || [ -z "$runUser" ]; then
echo "runPid:<=1,runUser:null"
exit 0
fi
else
echo "userPid:null"
Help
exit 0
fi
if [ "$whoAmi" = "$runUser" ] && [ -n $runPid ] ; then
Time=$(date +%s) ; Dir=$0 ; Dir=`dirname $Dir`
jmap -dump:format=b,file=$Dir/${Site}.pid.${runPid}.dump $runPid ; ret=$?
jmap -histo:live $runPid >> $Dir/${Site}.pid.${runPid}.log
if [ $ret -ne 0 ];then
break
fi
#echo "jmap $runPid >> $Dir/${Site}.pid.${runPid}.log"
#echo ">>>$Time" >> $Dir/${Site}.pid.${runPid}.log
else
echo "当前用户${whoAmi},并非运行该java项目${projectName}的用户."
exit 0
fi
}
Check () {
unset RunSleepCount sleepCount freeMem
RunSleepCount=(`top -b -n1 |head -10|grep -E '(sleeping|running)'\
| awk '{print $4,$6}'`)
if [ ${#RunSleepCount[@]} -eq 2 ]; then
running=${RunSleepCount[0]}
sleeping=${RunSleepCount[1]}
sleepCount=$(( running + sleeping ))
else
sleepCount=0
fi
freeMem=$(free -m|grep Mem|awk '{print $4}')
}
CheckSysVer() {
unset ret RELEASE OS_VER
RELEASE=$(uname -a|awk '{print $3}') >/dev/null
echo $RELEASE |grep -E '(el6|el7)' >/dev/null && OS_TYPE='CentOS'
ret=$?
#OS_TYPE=test
if [ "$OS_TYPE" == 'CentOS' ];then
echo $RELEASE |grep 'el6' >/dev/null && OS_VER=6
echo $RELEASE |grep 'el7' >/dev/null && OS_VER=7
return $OS_VER
else
OS_VER=Unkonwn
return $ret
fi
}
CheckCpu() {
uptime1m=$(printf '%.f' `uptime | awk '{print $10}'|tr -d ','`)
cpuCoreNum=$(( $(cat /proc/cpuinfo |grep processor|wc -l) * 2 ))
cpuCore100=$(( `cat /proc/cpuinfo |grep processor|wc -l` * 100 ))
CheckSysVer
case ${OS_VER} in
6)
cpuUsSy=(`printf '%.f %.f' $(top -b -n1 |head -10|sed -n '3p' |\
awk '{print $2,$3}'|tr -d '%us,' |tr -d '%sy,' )`)
;;
7)
#uptime1m=$(printf '%.f' `uptime | awk '{print $11}'|tr -d ','`)
cpuUsSy=(`printf '%.f %.f' $(top -b -n1 |head -10|sed -n '3p' | \
awk -F ',' '{print $1,$2}'|grep -o ':.*[0-9].[0-9].*[0-9].[0-9]'| \
tr -d ':' |tr -d 'us' |tr -s ' ' ' ')`)
;;
* )
echo "This OS is not supported..."
exit 1
;;
esac
if [ $uptime1m -gt $cpuCoreNum ];then
cpuUseCount=$(( cpuUseCount += 1 ))
elif [ ${cpuUsSy[0]} -gt 90 ] || [ ${cpuUsSy[1]} -gt 90 ] ;then
cpuUseCount=$(( cpuUseCount += 1 ))
fi
echo "uptime1m:$uptime1m, cpuCoreNum:$cpuCoreNum, cpuCore100:$cpuCore100, \
cpuUsSy:${cpuUsSy[@]}, cpuUseCount:$cpuUseCount"
return $cpuUseCount
}
outputDict () {
echo -e "{"
echo -e " \"Count\":$Count,"
echo -e " \"Break\":$Break,"
echo -e " \"sleepCount\":$sleepCount,"
echo -e " \"maxSleepCount\":$maxSleepCount,"
echo -e " \"freeMem\":$freeMem,"
echo -e " \"minFreeMem\":$minFreeMem"
echo -e "}"
}
Main () {
while [ $Count -lt $Break ]
do
Check
if [ -n "$sleepCount" -a $sleepCount -ge $maxSleepCount ]\
|| [ -n "$freeMem" -a $freeMem -le $minFreeMem ]
then
Dump
Count=$(( Count += 1 ))
fi
outputDict
sleep $waitTime
done
}
MainCpu () {
cpuUseCount=0
while :
#while [ $Count -le $Break ]
do
CheckCpu
#echo "<>$cpuUseCount<>"
if [ $cpuUseCount -ge $CpuUseAlarm ]; then
Dump
#cpuUseCount=0
echo "cpuUseCount:$cpuUseCount"
fi
if [ $cpuUseCount -ge $(( CpuUseAlarm * 2 )) ];then
exit 1
fi
sleep $waitTime
done
}
#MainCpu&
Main&
# time echo "scale=5000; 4*a(1)" | bc -l -q
#