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

Four ways to create objects in JS

Table of contents 1. Create objects by literal va...

How to quickly query 10 million records in Mysql

Table of contents Normal paging query How to opti...

JavaScript uses setTimeout to achieve countdown effect

In order to enhance the ability to write JavaScri...

Practical way to build selenium grid distributed environment with docker

Recently, I needed to test the zoom video confere...

Detailed explanation of the JavaScript timer principle

Table of contents 1. setTimeout() timer 2. Stop t...

Analysis of the principle of centering elements with CSS

It is a very common requirement to set the horizo...

Solution to MySQL master-slave delay problem

Today we will look at why master-slave delay occu...

HTML CSS3 does not stretch the image display effect

1. Use the transform attribute to display the ima...

How does Vue track data changes?

Table of contents background example Misconceptio...

Implementation of Vue counter

Table of contents 1. Implementation of counter 2....

40 fonts recommended for famous website logos

Do you know what fonts are used in the logo desig...

SQL implements LeetCode (180. Continuous numbers)

[LeetCode] 180. Consecutive Numbers Write a SQL q...

JavaScript to implement a simple web calculator

background Since I was assigned to a new project ...

Vue3 manual encapsulation pop-up box component message method

This article shares the specific code of Vue3 man...