原博客地址: http://doc.okbase.net/zzone/archive/121301.html
Linux 查看端口命令
lsof -i:端口号
服务器是linux,我们远程在window系统上监控
使用JMX方式
[root@bspdev jdk1.7.0_09]# cd $JAVA_HOME/jre/lib/management
[root@bspdev management]# pwd
/usr/java/jdk1.7.0_09/jre/lib/management
[root@bspdev management]# ls -l
total 28
-rw-r--r-- 1 root root 3998 Sep 25 2012 jmxremote.access
-rw-r--r-- 1 root root 2856 Sep 25 2012 jmxremote.password.template
-rw-r--r-- 1 root root 14097 Sep 25 2012 management.properties
-rw-r--r-- 1 root root 3376 Sep 25 2012 snmp.acl.template
首先利用password.template文件创建jmxremote.password文件,并且设置相应的读写权限。
[root@bspdev management]# cp jmxremote.password.template jmxremote.password
[root@bspdev management]# ls -l
total 32
-rw-r--r-- 1 root root 3998 Sep 25 2012 jmxremote.access
-rw-r--r-- 1 root root 2856 Jul 5 06:09 jmxremote.password
-rw-r--r-- 1 root root 2856 Sep 25 2012 jmxremote.password.template
-rw-r--r-- 1 root root 14097 Sep 25 2012 management.properties
-rw-r--r-- 1 root root 3376 Sep 25 2012 snmp.acl.template
[root@bspdev management]#
修改jmxremote.password的文件权限,否则报错必须限制口令文件读取访问权限:jmxremote.password
Chmod –R 600 jmxremote.password
将jmxremote.password中的权限部分注释标记去除。
# password "QED". The "controlRole" role has password "R&D".
#
# monitorRole QED
# controlRole R&D
修改为:
monitorRole QED
controlRole R&D
"jmxremote.password" 64L, 2852C written
下一步修改Tomcat的启动语句,将bin/catalina.sh文件vi编辑:
将原
JAVA_OPTS="-Xmx1024M -Xms512M -Xss3M"
export JAVA_OPTS
调整为
CATALINA_OPTS="-Xmx1024M -Xms512M -Xss3M -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true"
export CATALINA_OPTS
注意这里不要使用JAVA_OPTS这个变量,否则会出现错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use
为什么会出现这个错误呢看官方的说法:
# JAVA_OPTS (Optional) Java runtime options used when any command
# is executed.
# Include here and not in CATALINA_OPTS all options, that
# should be used by Tomcat and also by the stop process,
# the version command etc.
# Most options should go into CATALINA_OPTS.
# CATALINA_OPTS (Optional) Java runtime options used when the "start",
# "run" or "debug" command is executed.
# Include here and not in JAVA_OPTS all options, that should
# only be used by Tomcat itself, not by the stop process,
# the version command etc.
# Examples are heap size, GC logging, JMX ports etc.
很明显 JAVA_OPTS变量会被任何命令执行时调用,而CATALINA_OPTS只是在启动、运行、调试的命令中才会被调用。
jmx配置参数
1、配置Tomcat catalina.sh
找到# OS specific support. $var _must_ be set to either true or false.添加如下变量:
JAVA_OPTS="-Dcom.sun.management.jmxremote.port=1090 --配置jmx远程监听端口1090,指定任意未被占用端口
-Dcom.sun.management.jmxremote.authenticate=true --启用用户认证
-Dcom.sun.management.jmxremote.ssl=false --禁用ssl
-Djava.rmi.server.hostname=10.111.43.164 --绑定远程主机IP
-Dcom.sun.management.jmxremote.acccess.file=/opt/apache-tomcat-7.0.2/jconsole/jmxremote.access --配置用户访问权限
-Dcom.sun.management.jmxremote.password.file=/opt/apache-tomcat-7.0.2/jconsole/jmxremote.password" --配置用户信息,包括用户名和密码
如果不需要配置访问用户名及密码,配置如下:
# OS specific support. $var _must_ be set to either true or false.
JAVA_OPTS="-Dcom.sun.management.jmxremote.port=1090
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.111.43.164"
选项配置文件说明:$JAVA_HOME/jre/lib/mamagement/jmxremote.properties
2、切换用户到root,修改/etc/hosts添加IP地址。添加如下信息:
10.111.43.164 fmcods.localdomain fmcods localhost.localdomain localhost
3、创建jmxremote.password、jmxremote.access文件
#mkdir -p $JAVA_HOME/jconsole
#cp $JAVA_HOME/jre/lib/management/jmxremote.password.template $JAVA_HOME/jconsole/jmxremote.password
#cp $JAVA_HOME/jre/lib/management/jmxremote.access $JAVA_HOME/jconsole/jmxremote.access
修改文件权限:
#chmod -R 775 jconsole
此时提示错误: 必须限制口令文件读取访问: $JAVA_HOME/jconsole/jmxremote.password。
解决办法:在$CATALINA_BASE目录下新建jconsole文件夹,访问文件权限必须是600
#mkdir -p $CATALINA_BASE/jconsole
复制jmxremote.password、jmxremote.access到$CATALINA_BASE/jconsole,修改文件权限
#chmod -R 600 jmxremote.password
#chmod -R 600 jmxremote.access
4、$CATALINA_BASE/bin/shutdown.sh.出现连接端口被占用提示。
5、ps -ef | grep 1090 --被占用端口
6、kill -9 pid
7、重启Tomcat.$CATALINA_BASE/startup.sh
8、jconsole 远程连接