JMX - Java Management Extension

JMX is a tool to provide access to MBeans. Most application server console tools will provide access via JMX. The JBoss distribution includes twiddle.sh, a command-line tool that provides JMX access.

The Sun website includes the list of options and how to configure JMX Remote on Java 5:

http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html.

JMX uses a 3-tier architecture: (snagged from wikipedia...)

    • The Instrumentation level. Also referred to as the Probe level, it contains the probes (called MBeans) implementing the resource.

    • The Agent level, or MBeanServer, is the core of JMX. It is an intermediary between the MBean and the applications.

    • The Remote Management level enables remote applications to access the MBeanServer through Connectors and Adaptors. A connector provides full remote access to the MBeanServer API using various communication frameworks (RMI, IIOP, JMS, WS-* …), while an adaptor adapts the API to another protocol (SNMP, …) or to Web-based GUI (HTML/HTTP, WML/HTTP, …).

Further details are here: http://en.wikipedia.org/wiki/JMX

The package that provided JMX jar files for my Debian distribution is: libmx4j-java

A GUI based tool that utilizes JMX for accessing MBeans is JConsole. JConsole is bundled with the standard Java distribution from Sun/Oracle.

A command-line tool that utilizes JMX for accessing MBeans is jmxterm, and is available here: http://wiki.cyclopsgroup.org/jmxterm

NOTE: See attachments below.

To configure Tomcat 5/6 for use with JMX:

    • Add the following options to your Tomcat startup script:

CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote

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

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=false"

You'll need to update your $TOMCAT_HOME/bin/catalina.sh script to include these options.

Restart Tomcat once you've made these changes.

    • Ensure you pick a port number that does not conflict.

    • You'll need to install the jmxterm jar file into some directory. I put mine in /usr/global/packages.

    • You may test your JMX install with the following:

n1:/usr/global/packages# java -jar jmxterm-1.0-alpha-4-uber.jar -h

usage: jmxterm [-e] [-h] [-i <val>] [-l <val>] [-n] [-o <val>] [-p <val>] [-u

<val>] [-v <val>]

Main executable of JMX terminal CLI tool

-e,--exitonfailure With this flag, terminal exits for any Exception

-h,--help Show usage of this command line

-i,--input <val> Input script file. There can only be one input file.

"stdin" is the default value which means console input

-l,--url <val> Location of MBean service. It can be <host>:<port> or

full service URL.

-n,--noninteract Non interactive mode. Use this mode if input doesn't come

from human or jmxterm is embedded

-o,--output <val> Output file, stdout or stderr. Default value is stdout

-p,--password <val> Password for user/password authentication

-u,--user <val> User name for user/password authentication

-v,--verbose <val> Verbose level, could be silent|brief|verbose. Default

value is brief

Without any option, this command opens an interactive command line based

console. With a given input file, commands in file will be executed and process

ends after file is processed

NOTE: In Debian 5.0, the tomcat 5 package installs gij (Java), which did NOT work.

I needed to install sun-java-5. Don't forget to uninstall gij.

NOTE: Authentication had problems with Debian package for tomcat 5. I installed

the tomcat 6 distribution (zip-file) directly from the apache-tomcat website.

    • In the above example, authentication is disabled. We need to have authentication enabled, otherwise anyone would be able to wreck havoc on our java applications. JMX not only allows you to query MBeans, but to also change them. Change your Tomcat startup options to:

-Dcom.sun.management.jmxremote.authenticate=true \

-Dcom.sun.management.jmxremote.password.file=/opt/tomcat/conf/jmxremote.password \

-Dcom.sun.management.jmxremote.access.file=/opt/tomcat/conf/jmxremote.access \

NOTE: I installed Tomcat 6 into directory /opt/tomcat.

    • Ensure that jmxremote.password & jmxremote.access have permissions of 0600.

    • Contents of jmxremote.password:

monitor tomcat

control hard-to-guess-password

Where the first column is the account name, the second column is the cleartext password, (thus permissions of 0600).

If you fail to set permissions correctly, Tomcat will refuse to start.

    • Contents of jmxremote.access:

monitor readonly

control readwrite

Where the first column is the account name, and the second column is the type of access granted to the account.

    • With authentication enabled, I can connect via:

n1:/usr/global/packages# java -jar jmxterm-1.0-alpha-4-uber.jar -l localhost:9004 -u monitor -p tomcat

Welcome to JMX terminal. Type "help" for available commands.

$>help

#following commands are available to use:

about - Display about page

bean - Display or set current selected MBean.

beans - List available beans under a domain or all domains

bye - Terminate console and exit

close - Close current JMX connection

domain - Display or set current selected domain.

domains - List all available domain names

exit - Terminate console and exit

get - Get value of MBean attribute(s)

help - Display available commands or usage of a command

info - Display detail information about an MBean

jvms - List all running local JVM processes

open - Open JMX session or display current connection

option - Set options for command session

quit - Terminate console and exit

run - Invoke an MBean operation

set - Set value of an MBean attribute

$>

Where -u is the username to connect as, -p is the password, -l is the connection URL. (See Usage above)

    • To get a list of the JVM's running:

$>jvms

7826 ( ) - org.apache.catalina.startup.Bootstrap start

8732 ( ) - jmxterm-1.0-alpha-4-uber.jar -l localhost:9004 -u monitor -p tomcat

$>

  • To show Heap Memory Usage (hint: use the "beans" command to get a list):

$>get -b java.lang:type=Memory HeapMemoryUsage

#mbean = java.lang:type=Memory:

HeapMemoryUsage = {

committed = 5361664;

init = 0;

max = 66650112;

used = 3006120;

};

$>