- 简单命令
- 现成脚本
- 通过zabbix进行监控
- 自定义监控(jmap-heap pid 自定义监控)
- 通过jmx
- jps java ps
- -lvm
[root@web01 ~]# jps -lvm
11808 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat_8081/conf/logging.properties -Djava.util.logging.manager=org.apache.j
uli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat_8081/endorsed -Dcatalina.base=/application/tomcat_8081 -Dcatalina.home=/application/tomcat_8081 -Djava.io.tmpdir=/application/tomcat_8081/temp11748 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.C
lassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat/endorsed -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp11845 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/application/tomcat_8082/conf/logging.properties -Djava.util.logging.manager=org.apache.j
uli.ClassLoaderLogManager -Djava.endorsed.dirs=/application/tomcat_8082/endorsed -Dcatalina.base=/application/tomcat_8082 -Dcatalina.home=/application/tomcat_8082 -Djava.io.tmpdir=/application/tomcat_8082/temp12109 sun.tools.jps.Jps -lvm -Denv.class.path=.:/application/jdk/lib:/application/jdk/jre/lib:/application/jdk/lib/tools.jar -Dapplication.home=/application/jdk1.8.0_60 -X
ms8m
- jmap导出jvm信息
[root@web01 ~]# jmap -heap 11845
Attaching to process ID 11845, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.60-b23
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 520093696 (496.0MB)
NewSize = 11141120 (10.625MB)
MaxNewSize = 173342720 (165.3125MB)
OldSize = 22413312 (21.375MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 16646144 (15.875MB)
used = 10298808 (9.821708679199219MB)
free = 6347336 (6.053291320800781MB)
61.86903105007382% used
Eden Space:
capacity = 14811136 (14.125MB)
used = 8463800 (8.071708679199219MB)
free = 6347336 (6.053291320800781MB)
57.14484020672013% used
From Space:
capacity = 1835008 (1.75MB)
used = 1835008 (1.75MB)
free = 0 (0.0MB)
100.0% used
To Space:
capacity = 1835008 (1.75MB)
used = 0 (0.0MB)
free = 1835008 (1.75MB)
0.0% used
tenured generation:
capacity = 36851712 (35.14453125MB)
used = 23062736 (21.994338989257812MB)
free = 13788976 (13.150192260742188MB)
62.58253619261976% used
14246 interned Strings occupying 1890552 bytes.
- **jstack 导出java进程信息
- 故障案例: 系统负载高,发现tomcat占用cpu较高
- 1.jps /top/htop 精确定位哪个java进程导致
- 2.jstack 导出java详细
- 3.catalina.out日志
- 4.jmap 导出jvm信息 通过mat进行分析
- 现成脚本:新建脚本show-busy-java-threads.sh内容如下
#!/bin/bash
# @Function
# Find out the highest cpu consumed threads of java, and print the stack of these threads.
#
# @Usage
# $ ./show-busy-java-threads.sh
#
# @author Jerry Lee
readonly PROG=`basename $0`
readonly -a COMMAND_LINE=("$0" "$@")
usage() {
cat <<EOF
Usage: ${PROG} [OPTION]...
Find out the highest cpu consumed threads of java, and print the stack of these threads.
Example: ${PROG} -c 10
Options:
-p, --pid find out the highest cpu consumed threads from the specifed java process,
default from all java process.
-c, --count set the thread count to show, default is 5
-h, --help display this help and exit
EOF
exit $1
}
readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
[ $? -ne 0 ] && usage 1
eval set -- "${ARGS}"
while true; do
case "$1" in
-c|--count)
count="$2"
shift 2
;;
-p|--pid)
pid="$2"
shift 2
;;
-h|--help)
usage
;;
--)
shift
break
;;
esac
done
count=${count:-5}
redEcho() {
[ -c /dev/stdout ] && {
# if stdout is console, turn on color output.
echo -ne "\033[1;31m"
echo -n "$@"
echo -e "\033[0m"
} || echo "$@"
}
yellowEcho() {
[ -c /dev/stdout ] && {
# if stdout is console, turn on color output.
echo -ne "\033[1;33m"
echo -n "$@"
echo -e "\033[0m"
} || echo "$@"
}
blueEcho() {
[ -c /dev/stdout ] && {
# if stdout is console, turn on color output.
echo -ne "\033[1;36m"
echo -n "$@"
echo -e "\033[0m"
} || echo "$@"
}
# Check the existence of jstack command!
if ! which jstack &> /dev/null; then
[ -z "$JAVA_HOME" ] && {
redEcho "Error: jstack not found on PATH!"
exit 1
}
! [ -f "$JAVA_HOME/bin/jstack" ] && {
redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack file does NOT exists!"
exit 1
}
! [ -x "$JAVA_HOME/bin/jstack" ] && {
redEcho "Error: jstack not found on PATH and $JAVA_HOME/bin/jstack is NOT executalbe!"
exit 1
}
export PATH="$JAVA_HOME/bin:$PATH"
fi
readonly uuid=`date +%s`_${RANDOM}_$$
cleanupWhenExit() {
rm /tmp/${uuid}_* &> /dev/null
}
trap "cleanupWhenExit" EXIT
printStackOfThread() {
local line
local count=1
while IFS=" " read -a line ; do
local pid=${line[0]}
local threadId=${line[1]}
local threadId0x=`printf %x ${threadId}`
local user=${line[2]}
local pcpu=${line[4]}
local jstackFile=/tmp/${uuid}_${pid}
[ ! -f "${jstackFile}" ] && {
{
if [ "${user}" == "${USER}" ]; then
jstack ${pid} > ${jstackFile}
else
if [ $UID == 0 ]; then
sudo -u ${user} jstack ${pid} > ${jstackFile}
else
redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user})."
redEcho "User of java process($user) is not current user($USER), need sudo to run again:"
yellowEcho " sudo ${COMMAND_LINE[@]}"
echo
continue
fi
fi
} || {
redEcho "[$((count++))] Fail to jstack Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user})."
echo
rm ${jstackFile}
continue
}
}
blueEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
done
}
ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
[ -z "${pid}" ] &&
awk '$4=="java"{print $0}' ||
awk -v "pid=${pid}" '$1==pid,$4=="java"{print $0}'
} | sort -k5 -r -n | head --lines "${count}" | printStackOfThread
- zabbix通过jmx监控 tomcat (tomcat需要开启监控功能)
#修改
#catalina.sh
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=192.168.111.12"
#说明:
CATALINA_OPTS="$CATALINA_OPTS
#修改tomcat启动参数
-Dcom.sun.management.jmxremote #开启tomcat远程管理功能
-Dcom.sun.management.jmxremote.port=12345 #远程管理功能 除了12345端口 还会生成2个随机端口
开放所有端口(单独设置 安全组规则)
-Dcom.sun.management.jmxremote.authenticate=false #是否支持认证,例如用户登录
-Dcom.sun.management.jmxremote.ssl=false #是否https协议
-Djava.rmi.server.hostname=192.168.111.12 #指定本地的ip地址
"
#配置完参数后重启启动tomcat查看进程信息
/application/jdk/bin/java
-Djava.util.logging.config.file=/application/tomcat/conf/logging.properties
-Djava.util.logging
.manager=org.apache.juli.ClassLoaderLogManager
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=192.168.111.12
-Djava.endorsed.dirs=/application/tomcat/endorsed
-classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
-Dcatalina.base=/application/tomcat
-Dcatalina.home=/application/tomcat
-Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
除了定义好的端口,额外生成两个随机端口
本地电脑验证监控tomcat测试 本地电脑需要安装jdk,如果有则忽略 执行jconsole命令
远程进程连接输入地址和端口连接即可
连接成功,监控界面
标签:java,tomcat,MB,Djava,09,application,监控,sun From: https://blog.51cto.com/quyunlong/12036142