zabbix 监控tomcat ,开启JMX 修改catalina.sh文件,tomcat服务起不来
问题现象(配置Catalina.sh脚本,开启JMX不起效)
[root@localhost ~]# vi /usr/local/tomcat/apache-tomcat-9.0.56/bin/catalina.sh
#添加完
CATALINA_OPTS="-Docm.sun.management.jmxremote
-Docm.sun.management.jmxremote.authenticate=false
-Docm.sun.management.jmxremote.ssl=false
-Docm.sun.management.jmxremote.port=12345
-Djava.rmi.server.hostname=192.168.1.134"
问题一:配置完脚本,重启tomcat
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-9.0.56/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.56/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_311/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.56/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.56/bin/tomcat-juli.jar
Using CATALINA_OPTS: -Docm.sun.management.jmxremote
-Docm.sun.management.jmxremote.authenticate=false
-Docm.sun.management.jmxremote.ssl=false
-Docm.sun.management.jmxremote.port=12345
-Djava.rmi.server.hostname=192.168.1.134
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-9.0.56/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.56/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_311/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.56/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.56/bin/tomcat-juli.jar
Using CATALINA_OPTS: -Docm.sun.management.jmxremote
-Docm.sun.management.jmxremote.authenticate=false
-Docm.sun.management.jmxremote.ssl=false
-Docm.sun.management.jmxremote.port=12345
-Djava.rmi.server.hostname=192.168.1.134
Tomcat started.
[root@localhost ~]# lsof -i:8080
[root@localhost ~]#
[root@localhost ~]# 重启完发现根本就没有8080端口
[root@localhost ~]# netstat -antup |grep java
也是什么java程序都没有
tomcat服务启动失败
#而且去关闭tomcat服务,显示报错,其实就是tomcat刚刚根本就没有启动起来
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-9.0.56/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.56/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_311/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.56/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.56/bin/tomcat-juli.jar
Using CATALINA_OPTS: -Docm.sun.management.jmxremote
-Docm.sun.management.jmxremote.authenticate=false
-Docm.sun.management.jmxremote.ssl=false
-Docm.sun.management.jmxremote.port=12345
-Djava.rmi.server.hostname=192.168.1.134
十二月 22, 2021 4:24:15 下午 org.apache.catalina.startup.Catalina stopServer
严重: 无法联系[localhost:8005](基端口[8005]和偏移量[0])。Tomcat可能不在运行。
十二月 22, 2021 4:24:16 下午 org.apache.catalina.startup.Catalina stopServer
严重: 停止 Catalina 时出错
java.net.ConnectException: 拒绝连接 (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at java.net.Socket.connect(Socket.java:555)
at java.net.Socket.<init>(Socket.java:451)
at java.net.Socket.<init>(Socket.java:228)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:667)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:391)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:481)
查看tomcat的catalina.out日志
进入日志路径,根据自己所在tomcat路径
cd /usr/local/tomcat/apache-tomcat-9.0.56/logs
查看 catalina.out 日志
显示未找到命令,原因是上面的脚本格式并不是一行,无法正常识别
将脚本命令改为下面命令
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote=true"
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.authenticate=false"
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.port=12345"
CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=192.168.1.134"
再次重启tomcat
[root@localhost logs]# /usr/local/tomcat/apache-tomcat-9.0.56/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.56/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_311/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.56/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.56/bin/tomcat-juli.jar
Using CATALINA_OPTS: -Docm.sun.management.jmxremote=true -Docm.sun.management.jmxremote.authenticate=false -Docm.sun.management.jmxremote.ssl=false -Docm.sun.management.jmxremote.port=12345 -Djava.rmi.server.hostname=192.168.1.134
Tomcat started.
但是新的问题产生
问题二:tomcat可以正常启动了,但是JMX没起来
没有配置的12345 端口
[root@localhost logs]# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 3997 root 57u IPv6 57374 0t0 TCP *:webcache (LISTEN)
[root@localhost logs]# netstat -antup |grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3997/java
tcp6 0 0 :::8080 :::* LISTEN 3997/java
[root@localhost logs]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 802/rpcbind
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1497/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1106/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1108/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1239/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2716/sshd: root@pts
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3997/java
tcp6 0 0 :::111 :::* LISTEN 802/rpcbind
tcp6 0 0 :::8080 :::* LISTEN 3997/java
tcp6 0 0 :::22 :::* LISTEN 1106/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1108/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1239/master
tcp6 0 0 ::1:6010 :::* LISTEN 2716/sshd: root@pts
[root@localhost logs]# netstat -antup |grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 3997/java
tcp6 0 0 :::8080 :::* LISTEN 3997/java
[root@localhost logs]# netstat -antup |grep 12345
[root@localhost logs]# netstat -tnlp |grep 12345
原因:tomcat8.5以上版本,开启JMX需要多加一句话才能起效
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote=true"
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.authenticate=false"
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.port=12345"
CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=192.168.1.134"
#要加这句
CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote.rmi.port=12345"
按照网上所说,加了这句话,就可以,事实上我这边端口还是根本就没有,配置的语句看起来tomcat启动时,use了,但是根本不起作用
查看官方说明
官方tomcat9.0.56 JMX连接文档: Apache Tomcat 9 -Monitoring and Managing Tomcat
解决方法,不在catalian.sh下配置,按照官方新建脚本setenv.sh
#tomcat bin/路径下创建脚本
[root@localhost ~]# vi /usr/local/tomcat/apache-tomcat-9.0.56/bin/setenv.sh
#写入一下语句
CATALINA_OPTS="${CATALINA_OPTS} -Djava.rmi.server.hostname=192.168.1.134"
CATALINA_OPTS="${CATALINA_OPTS} -Djavax.management.builder.initial="
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote=true"
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.port=12345"
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.rmi.port=12345"
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="${CATALINA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"
参数 | 说明 |
---|---|
-Docm.sun.management.jmxremote | 启用JMX |
-Docm.sun.management.jmxremote.authenticate=false | 是否开启认证,看是否有需求(若开启,需要定义账户和密码) |
-Docm.sun.management.jmxremote.ssl=false | 远程ssl验证 |
-Docm.sun.management.jmxremote.port=12345 | 远程监听端口 |
-Djava.rmi.server.hostname=192.168.1.134 | Tomcat 所在服务器IP |
-Dcom.sun.management.jmxremote.rmi.port=12345" | tomcat 8.5以上要加 |
重启tomcat
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-9.0.56/bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.56/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_311/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.56/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.56/bin/tomcat-juli.jar
Using CATALINA_OPTS: -Djava.rmi.server.hostname=192.168.1.134 -Djavax.management.builder.initial= -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.rmi.port=12345 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
[root@localhost ~]# /usr/local/tomcat/apache-tomcat-9.0.56/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-9.0.56
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-9.0.56/temp
Using JRE_HOME: /usr/local/jdk/jdk1.8.0_311/jre
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-9.0.56/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-9.0.56/bin/tomcat-juli.jar
Using CATALINA_OPTS: -Djava.rmi.server.hostname=192.168.1.134 -Djavax.management.builder.initial= -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.rmi.port=12345 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
Tomcat started.