首页 > 其他分享 >09.tomcat监控

09.tomcat监控

时间:2024-09-17 10:22:35浏览次数:10  
标签:java tomcat MB Djava 09 application 监控 sun

  • 简单命令
  • 现成脚本
  • 通过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

相关文章

  • 当年青少年学习编程的重要性 政策原文 20240917_090943
    新一代人工智能发展规划20240917_082658_鲸鱼编程pyhui的技术博客_51CTO博客https://blog.51cto.com/u_13137233/12036071国务院关于印发全民科学素质行动规划纲要(2021—2035年)的通知20240917_083539_鲸鱼编程pyhui的技术博客_51CTO博客https://blog.51cto.com/u_13137233/12......
  • Docker 进阶篇-CIG 重量级监控系统
    上一篇讲的是轻量级的监控工具,本文就来讲重量级的:CAdvisor+InfluxDB+Granfana,简称CIG。​‍‍dockerstats原生的Docker命令中,stats可以查看每个容器占用的CPU,内存,网络流量等情况:CONTAINERIDNAMECPU%MEMUSAGE/LIMITMEM%NETI/O......
  • error: rpmdb, failed: BDB1507 Thread died in Berkeley DB library,error(-30973) fro
    rpm数据库错误,一般原因:yum更新等rpm软件安装进程被异常终止[root@49bdfccd7f61~]#yuminstall-yxxxerror:rpmdb:BDB0113Thread/process22858/140222685267712failed:BDB1507ThreaddiedinBerkeleyDBlibraryerror:db5error(-30973)fromdbenv->failchk:BDB0......
  • 0916
    高数第二型曲线积分计算类对称(看微元方向)一投二代三计算格林公式曲线封闭无奇点曲线封闭有奇点非封闭曲线,二维旋度为0,换路径非封闭曲线,旋度不为0,添线使之封闭(加线减线)积分与路径无关的六个等价命题两类曲线积分关系(二型与一型)Pdx+Qdy+Rdz=(P,Q,R)*(cosα,cosβ,cosγ)d......
  • 20240916总结
    不积跬步,无以千里。这两天主要是复习了图的连通性相关的题+听了youwike哥哥讲课。先是复习了缩点,割点,割边,点双,边双,2-SAT,感觉比较需要注意的是割点的那个第一个节点的判断,写题的时候总是容易忘。然后又写了几道练习题。缩点#include<iostream>#include<cstring>usingna......
  • 聪明办法学Python丨202409TASK1学习笔记
        踏入Python编程的世界之初,我便深刻地体会到了这门语言的独特魅力。Python凭借其简洁明了的语法与强大的功能性,迅速吸引了我的注意。相较于C语言等编译型语言,Python的语法更加接近自然语言,这使得即使是初次接触编程的人也能快速上手。Python的设计理念强调代码的可......
  • 列表与克隆体专题 scratch 20240916_182231
    体验克隆体变量scratch20240916_153936_鲸鱼编程pyhui的技术博客_51CTO博客https://blog.51cto.com/u_13137233/12031738数据的容器列表scratch20240916_155811_鲸鱼编程pyhui的技术博客_51CTO博客https://blog.51cto.com/u_13137233/12031757多组列表共同表达同一数据sc......
  • 多组列表共同表达同一数据 scratch 20240916_170510
    需求如果点击空格就会产生一个克隆体克隆体会随机位置克隆体它会有自己的id同时克隆体会有自己的座标要求我们使用三个列表分别记录他们的id,x,y坐标同时如果点击了某一个克隆体那么就从列表中把它相对应的一组数据删除功能克隆体的id三个列表一个列表存id一个列表......
  • 数据的容器 列表 scratch 20240916_155811
    什么是列表列表是数据的容器创建列表列表添加内容清空内容查找数据根据位置查找数据修改数据删除数据根据下标删除数据遍历所有数据让主角依次把所有的数据都说一遍......
  • 体验克隆体变量 scratch 20240916_153936
    需求本体产生三个克隆体每个克隆体都会说出自己的血量如果鼠标点击这个克隆体角色克隆体的血量就减少同时他说出来的数据也就会变小制作克隆体变量克隆体变量一定要是私有的当本体被克隆时这个私有的变量也会被克隆不过克隆后就各是各的数据了最终代码......