使用本地JConsole监控远程JVM

问题背景
Tomcat经常崩溃crash,想看看JVM内存使用情况,就想到了用Jconsole监控,以前只是监控本地JVM,这次要监控远程的,遇到了不少问题。

配置jmx信息

在server.xml中增加

  
  

linux:

JAVA_OPTS="$JAVA_OPTS -Xms256m -Xmx1024m -XX:PermSize=128M -XX:MaxPermSize=256m"
if [ "$1" = "start" ];then
# Remote Debug for JM
JAVA_OPTS="$JAVA_OPTS -Xms256m -Xmx1024m -XX:PermSize=128M -XX:MaxPermSize=256m";
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=172.16.1.240";
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=true";
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false";
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.password.file=$CATALINA_BASE/conf/jmxremote.password";
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.access.file=$CATALINA_BASE/conf/jmxremote.access";
;
fi;

windows:

if ""%1"" == ""start"" (
rem Remote Debug for JMX
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote=true
set JAVA_OPTS=%JAVA_OPTS% -Djava.rmi.server.hostname=172.16.1.240
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.authenticate=true
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.ssl=false
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.password.file=%CATALINA_BASE%/conf/jmxremote.password
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.access.file=%CATALINA_BASE%/conf/jmxremote.access
)

配置在catalish.sh/catalish.bat中。

systemctl:

Environment=CATALINA_OPTS="-Dcom.sun.management.jmxremote=true -Djava.rmi.server.hostname=172.16.1.240 -Dcom.sun.management.jmxremote.port=11001 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.file=${CATALINA_HOME}/conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=${CATALINA_HOME}/conf/jmxremote.access

启用密码
1.-Dcom.sun.management.jmxremote.authenticate=true
2.-Dcom.sun.management.jmxremote.password.file 指定正确的密码文件
3.用户名及密码(参考jmxremote.password文件) monitorRole只能读,controlRole能读写
配置中的安全原因出错,由于密码是以明文的方式保存在:jmxremote.password中,所以对此文件只能有所有者都读取,其他人都不能读取。
权限需要注意,600,所有者有rw权限。
4.用户和权限其实是存在jmxremote.access中。

zeno   readonly
admin   readwrite \
              create javax.management.monitor.*,javax.management.timer.* \
              unregister

jmxremote.password内容

zeno  123456
admin  123456

执行命令,修改文件属性:
chown -R tomcat8.tomcat8 /usr/share/tomcat8/conf/jmxremote.password
chown -R tomcat8.tomcat8 /usr/share/tomcat8/conf/jmxremote.access
chmod 400 jmxremote.access
chmod 400 jmxremote.password

至此,不使用密码和使用用户名及密码,“admin”-“123456”成功登录127.0.0.1上的Tomcat使用的JVM。

关于Zeno Chen

本人涉及的领域较多,杂而不精 程序设计语言: Perl, Java, PHP, Python; 数据库系统: MySQL,Oracle; 偶尔做做电路板的开发,主攻STM32单片机
此条目发表在Java分类目录。将固定链接加入收藏夹。