LINUX配置JMX监控tomcat7

jopen 9年前

1) 正常安装JDK和Tomcat 配置环境变量(此步略过)

    2) 配置JMX访问文件
    # cd $CATALINA_HOME/conf/
    # cp $JAVA_HOME/jre/lib/management/jmxremote.password.template jmxremote.password
    # chmod 700 jmxremote.password    (一定注意这个文件默认是不可写的)
    # vi $CATALINA_HOME/conf/jmxremote.password

    在最下面添加一个用户(第一个是用户名 第二个是密码)
    fox        123456

    # chmod 400 jmxremote.password    (要求该文件是任何用户均不可写的)
    # cp $JAVA_HOME/jre/lib/management/jmxremote.access jmxremote.access
    # vi jmxremote.access    (该文件是对jmx用户的授权)

    到最后一行 将
    monitorRole   readonly
    controlRole   readwrite
    注释 更改为
    # monitorRole   readonly
    # controlRole   readwrite
    然后在最后添加一行
    fox    readwrite

    # chmod 400 jmxremote.access

    3) 配置Tomcat启动参数
    # cd $CATALINA_HOME/bin
    # vi catalina.sh

    找到以下一行
    # ----- Execute The Requested Command -----------------------------------------
    在其上方添加一段脚本 加在最后的目的是要沿用之前的JAVA_OPTS和CATALINA_HOME变量设置(特别是堆内存参数设置) 网上文章说法有直接设置JAVA_OPTS的 不过这样设置在stop的时候会有异常抛出 虽然不影响使用
    (参数意义依次是:
        -Dcom.sun.management.jmxremote 启用JMX远程监控
        -Dcom.sun.management.jmxremote.port=9004 使用端口9004(可随意指定只要不冲突)
        -Dcom.sun.management.jmxremote.authenticate=true 远程连接需要密码认证
        -Dcom.sun.management.jmxremote.ssl=false 不使用SSL(SSL我试了多次都没有配置成功:P 暂时不开启)
        -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access 使用指定的JMX帐号授权文件
        -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password 使用指定的JMX帐号文件)


    # ----- JMX Config Start -----
    if [ "$1" = "run" ]; then
      JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password"
    elif [ "$1" = "start" ]; then
      JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password"
    fi

   
    # ----- JMX Config End -----

   也就是说需要在tomcat的bin下需要在catalina.sh下加入如下代码即可。

    # ----- JMX Config Start -----
    if [ "$1" = "run" ]; then
      JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.password"
    elif [ "$1" = "start" ]; then
      JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.password"
    fi
    # ----- JMX Config End -----

export JAVA_OPTS  (重要)

   然后我们要做的就是进入的/usr/java/jdk1.6.0_32/jre/lib/management下

   

  有个模板,拷贝一份作为jmxremote.password,切记,这个文件权限比较特殊,我用的时候用的400

   记得jmsremote.password最后

        monitorRole  QED
        controlRole   R&D

   前的注释去掉(系统自带的账号和密码,建议自己新增一个,新增的时候记得去jmxremote.access中付给账号读写权限) 

        然后就可以了。

       

       再就是注意一下hostname那部分,应该就直接可以在客户端输入jconsole然后输入IP地址,端口,账号,密码,你就可以监控tomcat了

        再次感谢@逝水fox大神

       vi /etc/hosts 修改主机名为外网IP地址,这个很重要,要不然连接不上...

WIndows访问Linux服务器Tomcat:使用JDK自带jvisualvm监控tomcat