监控 Java 应用信息

信息输出

主要输出CPU、内存、线程、链接等使用情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#[yl@yl-test ~]$ curl http://localhost:8080/moniter-java.sh -s | sh
[yl@yl-test ~]$ sh moniter-java.sh

==================== 2020-06-01 10:57:09 ====================
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
USER STIME | %CPU %MEM thrd | conn dbLn | lsnPorts:conn:wait | PID | path
root Apr30 | 0.0 1.5 21 | | | 1394 |
imhtp May28 | 0.0 5.1 38 | | | 14510 | /opt/timer/data-job.jar
imhtp May08 | 0.0 5.5 36 | 10 3 | 20880:5:0 | 1484 | /opt/ds-service/project/ds-service.jar

imhtp May18 | 0.0 11.4 37 | 1 0 | 8021 | 1171 | /opt/xmt/tomcat/

imhtp Apr30 | 0.0 5.4 17 | 19 0 | 2080:19:0 40540 | 2613 | /opt/zookeeper/zookeeper-3.4.10.jar
==================== 2020-06-01 10:57:09 ====================

查询脚本

moniter-java.sh

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
#!/bin/bash
# to moniter the java applications

echo
echo "==================== $(date "+%F %T") ===================="

#declare -A aPid aUser aCpu aMem aStime aThrd aDbln aConn aPort aJar

markTHR(){
if [ -n "$2" ] && [ "${2%.*}" -ge "$1" ]; then
echo -n "\033[1;31m%${3}s\033[0m"
else
echo -n "%${3}s"
fi
}

markConn(){
awk '{
len=(20>length($0)) ? 20-length($0) : 0;
if(!length($0))
$1="";
for(i=1;i<=NF;i++){
if($i~/^u/)
printf "\033[1;32m%s\033[0m",$i;
else if($i~/:/)
printf "\033[1;34m%s\033[0m",$i;
else
printf "%s",$i;
printf "%s"," ";
}
printf "%"len"s","";
}' <<< "$*"
}

myPrtf(){
printf "%9s " "$1" # process username
printf "%5s |" "$2" # process start time
printf "$(markTHR 10 "$3" 5) " "$3" # process cpu usage
printf "$(markTHR 10 "$4" 5) " "$4" # process memory usage
printf "$(markTHR 100 "$5" 4) | " "$5" # process threads count
printf "$(markTHR 100 "$6" 4) " "$6" # network connections count
printf "$(markTHR 30 "$7" 4) | " "$7" # database connections count
printf "$(markConn $8)| " # listen ports
printf "%5s | " "${9}" # process id
printf "%s\n" "${10}" # jar file/path
}

getBasicCmd=$(/bin/ps -eo pid,user,pcpu,pmem,stime,nlwp,args 2>/dev/null | grep [j]ava | awk '{
print "aPid["NR"]="$1";";
print "aUser["$1"]="$2";";
print "aCpu["$1"]="$3";";
print "aMem["$1"]="$4";";
print "aStime["$1"]="$5";";
print "aThrd["$1"]="$6";";
}' 2>/dev/null)
eval $getBasicCmd

getPortCmd=$(/bin/netstat -natup | grep -E 'java|-' | awk '
$1~/^tcp/{
gsub(/[^:]*:/,"",$4);
gsub(/[^:]*:/,"",$5);
gsub(/\/.*$/,"",$7);
}
$1~/^udp/{
gsub(/[^:]*:/,"",$4);
$4=($1~/^udp/)?"u"$4:$4;
gsub(/[^:]*:/,"",$5);
$7=($5=="*")?$6:$7;
gsub(/\/.*$/,"",$7);
}
$5=="*" && $7~/[0-9]+/{
lsnArr[$7,$4]++;
conn[$7]=0;
dblink[$7]=0;
}
$5!="*" && $7~/[0-9]+/{
conn[$7]++;
if(!dblink[$7])
dblink[$7]=0;
if($5==1521)
dblink[$7]++;
if(lsnArr[$7,$4])
workArr[$7,$4]++;
}
$7=="-"{
tw[$4]++;
}
END{
for(i in lsnArr){
split(i,lsn,SUBSEP);
if(lsnArr[i]){
workCount = (workArr[i]) ? ":"workArr[i] : ":"0;
waitCount = (tw[lsn[2]]) ? ":"tw[lsn[2]] : ":"0;
if(workCount==":0" && waitCount==":0"){
workCount="";
waitCount="";
}
workStr[lsn[1]]=workStr[lsn[1]]" "lsn[2] workCount waitCount;
twCount[lsn[1]] += tw[lsn[2]];
}
}
for(i in conn){
print "aConn["i"]="conn[i]";";
print "aWait["i"]="twCount[i]";";
print "aDbln["i"]="dblink[i]";";
print "aPort["i"]=\""workStr[i]"\";";
}
}' 2>/dev/null)
eval $getPortCmd

getJarCmd=$(/usr/sbin/lsof -nc java | grep '.jar$' | grep -v '/jre/lib/' | awk '
/bootstrap.jar/{
gsub(/bin\/bootstrap.jar/,"",$9);
jar[$2]=$9;
}
/jetty/ && /start/{
jar[$2]=$9;
}
/knity/{
jar[$2]=$9;
}
!/bootstrap.jar/ && !jar[$2]{
jar[$2]=$9;
}
END{
for(i in jar){
print "aJar["i"]="jar[i]";";
}
}' 2>/dev/null)
eval $getJarCmd

printf "\033[1m%9s %5s |%5s %5s %4s | %4s %4s | %-20s | %5s | %s\033[0m\n" \
"USER" "STIME" "%CPU" "%MEM" "thrd" "conn" "dbLn" "lsnPorts:conn:wait" "PID" "path"
unset PID
declare -i PID
for PID in ${aPid[*]}
do
myPrtf "${aUser[$PID]}" "${aStime[$PID]}" "${aCpu[$PID]}" "${aMem[$PID]}" "${aThrd[$PID]}" "${aConn[$PID]}" "${aDbln[$PID]}" "${aPort[$PID]}" "$PID" "${aJar[$PID]}"
done | sort -t'|' -k6 | sed 'n;n;n;n;G'
echo "==================== $(date "+%F %T") ===================="
echo
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/8d68f4e0.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!