Introduction

Sometimes it can be useful to monitor performance of Java Virtual Machine (VM) on remote host. To do so, a very nice tool – VisualVM – can be used. It can be run on local host and get information from jstatd running on a remote host. In addition, VisualVM comes with a number of useful plugins. This blog describes how to run VisualVM with VisualGC, which is Visual Garbage Collection Monitoring Tool to monitor Tomcat on remote machine. However, the solution can be also applied to other applications running on JavaVM.

Remote machine

Run Tomcat

Add the following options to CATALINA_OPTS variable to enable JMX support in Apache Tomcat.

CATALINA_OPTS="
    -Dcom.sun.management.jmxremote=true
    -Dcom.sun.management.jmxremote.port=<portNumber>
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false
    -Djava.rmi.server.hostname=<hostIP>"

in my case:

CATALINA_OPTS="
    -Dcom.sun.management.jmxremote=true
    -Dcom.sun.management.jmxremote.port=8084
    -Dcom.sun.management.jmxremote.ssl=false
    -Dcom.sun.management.jmxremote.authenticate=false
    -Djava.rmi.server.hostname=192.168.1.20"

You can find more information about Monitoring and Managing Tomcat here: Monitoring and Managing Tomcat

Run Tomcat:

startup.sh

We want to monitor Tomcat instance running on remote machine. To check whether it is running use:

ps aux | grep tomcat

The above command run on my remote machine returns the following:

tomcat6  28743  181  3.4 5136644 210932 ?      Sl   13:15   0:20 /opt/java-1.6.0_22/bin/java -Djava.util.logging.config.file=/var/lib/tomcat6/conf/logging.properties -Djava.awt.headless=true -Xms512m -Xmx3048m -XX:+UseConcMarkSweepGC -XX:MaxPermSize=512m -XX:-DisableExplicitGC -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8084 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=192.168.1.20 -Djava.endorsed.dirs=/usr/share/tomcat6/endorsed -classpath /usr/share/tomcat6/bin/bootstrap.jar -Dcatalina.base=/var/lib/tomcat6 -Dcatalina.home=/usr/share/tomcat6 -Djava.io.tmpdir=/tmp/tomcat6-tomcat6-tmp org.apache.catalina.startup.Bootstrap start

Notice that pid is ’28743′.

To get JavaVM process status you can run jps command, which is Java Virtual Machine Process Status Tool. jps is located is your Java JDK HOME/bin directory. Description of jps command can be found here. Note that jps returns only Java processes run by the user, who runs jps. To get list of all Java processes run sudo jps. See examples below.

Outcome of jps run on my remote machine:

28144 Jps

Outcome of sudo jps run on my remote machine:

28743 Bootstrap
1673 CPService
28159 Jps
28897 Jstatd

Notice that lvmid for Tomcat (in this case – Bootstrap) is ’28743′ which is the same as pid.

Hostname

Run

hostname

to check the host name, e.g., agile003.

Make sure that in /etc/hosts file this hostname has IP by which it is visible to the machine that will be running VisualVM (local machine), e.g., 192.168.1.20 agile003.

Run jstat Deamon

jstatd, which is jstat Daemon can be found in Java JDK HOME/bin. As described in documentation to jstatd, which can be found here: , create a file called jstatd.policy in any directory you choose, e.g., /home/joanna. The file should contain the following text:

grant codebase "file:${java.home}/../lib/tools.jar" {
    permission java.security.AllPermission;
};

Run jstatd using the following command. Make sure you run it with root permissions.

sudo jstatd -J-Djava.security.policy=/home/joanna/jstatd.policy

You can add the following flag to log the calls and see what is going on:

-J-Djava.rmi.server.logCalls=true

Local machine

Check access to jstatd

Run:

jps <hostname>

in my case

jps agile003

You should see the same output as for sudo jps running on remote machine.

1673 CPService
28743 Bootstrap
28897 Jstatd

Run VisualVM

Run:

jvisualvm

or

visualvm

jvisualvm can be located in your Java JDK HOME/bin directory also it can be downloaded from here: JVM download

Go to

Tools -> Plugins

and install the plugins you require including VisualGC plugin. My selection is presented in the screen shot below.

VisualVM plugins

Restart VisualVM.

Add remote host – one that jstatd is running on.

File -> AddRemoteHost...

give IP of the host, e.g., 192.158.1.20. The same IP should be set for the host name in /etc/hosts on remote server.

Now you should be able to access the Java processes running on remote host including Tomcat as presented below.

Java Processes on Remote Host

All tabs including VisualGC on right hand site should now show appropriate graphs. See sample screen shot below:

Visual GC

Enjoy!