How to enable JMX monitoring through Tomcat

How to enable JMX monitoring through Tomcat

Build a simulation environment:
Operating system: centos7
Memory: 1G
jdk:1.8.0_131
tomcat:8.0.48

We will not demonstrate the environment preparation here, but directly configure the jmx of tomcat

1. Enter the bin directory of tomcat

# cd /opt/tomcat/apache-tomcat-8.0.48/bin/

2. Edit the configuration file

# vim catalina.sh

3. Add parameters above the picture below

What it looks like after adding parameters

3.1. This configuration does not require an account or password to connect to jmx:

CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.100.117
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"

Screenshot after configuration:

Parameter Description:

Hostname: is the IP address of the server where Tomcat is monitored

jmxremote.port: port number, which is the monitoring port number to be opened

jmxremote.ssl: whether to enable SSL connection

authenticate: false means monitoring does not require a user and password

3.2. User name and password need to be configured:

CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.100.117
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password
-Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access"

Screenshot after configuration:

Parameter Description:

authenticate: true to enable account authentication

access.file: permission file path

password.file: password file path

3.3. If password verification is not configured, this step is not required. This step is required when user and password verification is enabled.

3.3.1. Find the JDK installation directory

If you don't know where the directory is installed, you can find it by command:

# java -verbose

3.3.2. Switch to the jdk directory

Enter # cd jre/lib/management/

There will be jmxremote.access jmxremote.password.template files in the file directory, copy them to the conf directory of tomcat

# cp jmxremote.* /opt/tomcat/apache-tomcat-8.0.48/conf/

3.3.3. Modify the jmxremote.access file

3.3.4. Rename the password file:

# mv jmxremote.password.template jmxremote.password

3.3.5 Edit password file

# vim jmxremote.password

3.3.6. After editing the file, you need to modify the permissions of the access file and password file, otherwise Tomcat will not start

# chmod 600 jmxremote.*

4. Start tomcat

[root@ha bin]# ./startup.sh 
Using CATALINA_BASE: /opt/tomcat/apache-tomcat-8.0.48
Using CATALINA_HOME: /opt/tomcat/apache-tomcat-8.0.48
Using CATALINA_TMPDIR: /opt/tomcat/apache-tomcat-8.0.48/temp
Using JRE_HOME: /usr
Using CLASSPATH: /opt/tomcat/apache-tomcat-8.0.48/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.48/bin/tomcat-juli.jar
Tomcat started. 

4.1. After completing the above operations, use the jvisualvm.exe provided by JDK to connect and test it under Windows.

Double-click to open the jmx connection

4.2. Restart tomcat and test whether jmx can still connect

# ./shutdown.sh

4.2.1. Connect to jmx again to see if it can be connected

The data monitoring chart cannot be viewed and the connection cannot be made

Why does this problem occur?

The conclusion given online is:

Tomcat's jmx remote health requires two ports, one is the registration port and the other is the service port. The previously specified port 9999 is the registration port. Tomcat will randomly specify a service port when it starts. Since we only have one registration port, after restarting the service, the registration port has not changed, but the service port has changed. Connecting through the registration port again can no longer connect to the previous service port. There is no way to specify the server connection, and the service port is random.

One method is to set jmx fixed registration port and service port, through the jar package to achieve

5. Log in to the official website of Tomcat (note that my Tomcat version is 1.8.0 and only 1.8.5 can be used on the official website)

Website: https://tomcat.apache.org/

Find the JMX Remote jar according to your tomcat version

6. Upload the downloaded files to the lib directory of tomcta

You can also directly wget the link to the lib directory to get the jar package

# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/extras/catalina-jmx-remote.jar

7. Edit the server.xml file in the conf directory of tomcat

# vim server.xml

Add the following content:

 <Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener"
 rmiRegistryPortPlatform="10001" rmiServerPortPlatform="10002" /> 

8. Edit catalina.sh in the bin directory of tomcat

# vim catalina.sh

To remove a parameter:

-Dcom.sun.management.jmxremote.port=9999

9. Restart tomcat test, jmx registration port is 10001, service port is 10002

10. Restart tomcat and connect again for testing

When we double-click the jmx remote connection created before, we will find that we cannot connect. Why is this? Here we should pay attention to the pid number in the above connection. After querying, we found that this pid number is the pid number of tomcat in linux. When we restart tomcat, a new pid number will be generated, so if we want to connect, we must create a new connection

11. When we create a new jmx connection again, we will get a new pid number

12. Here we have tested successfully, so when we use zabbix to monitor tomcat using jmx, we don't have to worry about tomcat restarting and the jmx port not being able to connect

Summarize

The above is the graphic method of enabling JMX monitoring through Tomcat introduced by the editor. I hope it will be helpful to everyone!

You may also be interested in:
  • Introduction to Tomcat's JMX service method
  • Tomcat configuration JMX remote connection

<<:  Detailed explanation of the concepts, principles and common usage of MySQL stored procedures

>>:  Vue implements interface sliding effect

Recommend

Add and delete table information using javascript

Getting Started with JavaScript JavaScript is a l...

Common browser compatibility issues (summary)

Browser compatibility is nothing more than style ...

IDEA uses the Docker plug-in (novice tutorial)

Table of contents illustrate 1. Enable Docker rem...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

W3C Tutorial (11): W3C DOM Activities

The Document Object Model (DOM) is a platform, a ...

Solution to VMware virtual machine no network

Table of contents 1. Problem Description 2. Probl...

About React Native unable to link to the simulator

React Native can develop iOS and Android native a...

Universal solution for MySQL failure to start under Windows system

MySQL startup error Before installing MySQL on Wi...

Detailed explanation of the use of Linux seq command

01. Command Overview The seq command is used to g...

Detailed explanation of JDBC database link and related method encapsulation

Detailed explanation of JDBC database link and re...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

How to get the maximum or minimum value of a row in sql

Original data and target data Implement SQL state...

Detailed explanation of setting Context Path in Web application

URL: http://hostname.com/contextPath/servletPath/...