Tomcat笔记之监控

实现监控方法
1.利用tomcat自带的默认查看状态信息页面
在conf目录下的tomcat-user.xml中添加登陆用户名和密码
 <role rolename="manager-gui"/>
     <role rolename="admin-gui"/>
     <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>
 	
 	加入以上代码
 	
 	http://10.0.0.81:8080/manager
 	输入账号密码登陆
 	都为tomcat

2.在tomcat服务器上部署相应监控页面代码

 <%
 Runtime rtm = Runtime.getRuntime();
 long mm = rtm.maxMemory()/1024/1024;
 long tm = rtm.totalMemory()/1024/1024;
 long fm = rtm.freeMemory()/1024/1024;
 
 out.println("JVM memory detail info :<br>");
 out.println("Max memory:"+mm+"MB"+"<br>");
 out.println("Total memory:"+tm+"MB"+"<br>");
 out.println("Free memory:"+fm+"MB"+"<br>");
 out.println("Available memory can be used is :"+(mm+fm-tm)+"MB"+"<br>");
 %>

3.第三方监控软件(jdk)

tomcat服务端配置
 #在tomcat配置文件中开启监控功能
 vim /application/tomcat-01/bin/catalina.sh +97
 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=10.0.0.81"
修改完重启并查看相应端口
 [root@tomcat conf]# shutdown.sh
 Using CATALINA_BASE:   /application/tomcat
 Using CATALINA_HOME:   /application/tomcat
 Using CATALINA_TMPDIR: /application/tomcat/temp
 Using JRE_HOME:        /application/jdk
 Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
 [root@tomcat conf]# startup.sh 
 Using CATALINA_BASE:   /application/tomcat
 Using CATALINA_HOME:   /application/tomcat
 Using CATALINA_TMPDIR: /application/tomcat/temp
 Using JRE_HOME:        /application/jdk
 Using CLASSPATH:       /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
 Tomcat started.
 [root@tomcat conf]# ss -lntup|grep 12345
 tcp    LISTEN     0      50       :::12345                :::*                   users:(("java",pid=4841,fd=20))
windows安装第三方监控软件jdk
 一直下一步就好
在地址栏输入:C:\Program Files\Java\jdk1.8.0_31\bin\jconsole.exe

在弹出页面点击远程进程
输入相应ip+端口即可

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在地址栏输入:C:\Program Files\Java\jdk1.8.0_31\bin\jvisualvm.exe

在这里插入图片描述

在弹出页面:远程->右键->添加远程主机->输入tomcat服务器ip->选择主机右键->添加JMX连接->输入相应端口->再开启即可
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.利用命令进行查看操作或者利用监控脚本

命令:jps -lvm
脚本代码
 #!/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

5.利用zabbix监控软件(重点)

安装

yum install -y zabbix-java-gateway-4.0.0-2.el7.x86_64
或者
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-java-gateway-4.0.0-2.el7.x86_64.rpm

PS:这里有一个坑
 若是直接使用rpm安装会报错,切记!
启动
 [root@zabbix_ser ~]# systemctl start zabbix-java-gateway.service 
 [root@zabbix_ser ~]# ss -lntup|grep java
 tcp    LISTEN     0      50       :::10052                :::*                   users:   (("java",pid=126186,fd=13))
修改zabbix配置文件中与Java有关的
 [root@zabbix_ser ~]# grep java -i /etc/zabbix/zabbix_server.conf 
 #	Number of pre-forked instances of pollers for unreachable hosts (including IPMI and Java).
 #	At least one poller for unreachable hosts must be running if regular, IPMI or Java pollers
 ### Option: JavaGateway
 #	IP address (or hostname) of Zabbix Java gateway.
 #	Only required if Java pollers are started.
 # JavaGateway=127.0.0.1
 ### Option: JavaGatewayPort
 #	Port that Zabbix Java gateway listens on.
 # JavaGatewayPort=10052
 ### Option: StartJavaPollers
 #	Number of pre-forked instances of Java pollers.
 # StartJavaPollers=5
修改的地方:
  JavaGateway=127.0.0.1
  StartJavaPollers=5
重启
在tomcat端安装zabbix—agent

[root@tomcat conf]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm

修改配置文件
 不多说=-=

 在http://10.0.0.77/zabbix  web界面添加一台主机,勾选JMX输入tomcat的地址即可

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值