CRaSH: An Extensible Command Line Shell For Monitoring A Running JVM
CRaSH (Common ReusAble SHell) is a utility which can provide a command line for interacting with a existing live Java process. It connects to a running JVM and can inspect its internals. It is not just a shell written in Java (like BeanShell). It essentially offers remote management and monitoring of a running JVM application in a similar way to Java Management Extensions (JMX).
JMX support must be explicitly developed in a Java application. A Java installation includes by default graphical tools such as JConsole and more recently VisualVM that can attach to a running Java application and retrieve several statistics such as memory usage, thread information, JVM properties etc.
JMX also allows developers or system administrators to change some settings of the application on the fly while the application is running. This functionality is only offered for features already exposed by the developers as MBean interfaces. Since Java 6 however, it is possible to use the Attach API programmatically in order to develop custom monitoring applications that attach directly on a running JVM. As an example, jtop is a command line application that monitors CPU thread utilization. This API is specific to the Hotspot VM implementation.
CRaSH takes this concept even further. It takes advantage of the Attach API and provides a command line shell (full with internal commands, history and autocompletion). The shell is attached to a running JVM and then can be used for loading and running Groovy scripts that are executed against this JVM. This essentially gives an interactive access to the internals of a running JVM allowing for more detailed monitoring.
CRaSH already has built-in support for:
- inspecting/changing VM properties, running the garbage collector
- SQL access to JDBC resources
- access via telnet/SSH (JMX uses Java RMI which can be problematic for firewalls)
- retrieving memory statistics
- managing of JVM threads
- loading external Groovy scripts
- accessing JCR repositories (e.g. Jackrabbit)
- logging commands
The core functionality of CRaSH is best shown in a video that demonstrates running Groovy scripts against a running JVM. A more realistic example is shown in another video where CRaSH is attached to an application server, and executes SQL commands on a datasource using the integrated JDBC commands. CRaSH is essentially just a thin layer between a running JVM and the custom Groovy scripts created by the developer in the form of monitoring "recipes".
Some possible use cases for CRaSH are:
- Monitoring a running JVM application (for system administrators, like JMX)
- Fine-tuning a running JVM application on the fly (for developers).
- Debugging/Logging a running JVM application (possibly after a specific event)
- Adding a command line interface to an existing JVM application (targeted to advanced users)
- Offering an easy way to load "plugins" on the fly for a running application
- Mocking some parts of a running JVM application or intercepting its I/O
- Orchestrating different resources/services from a central point
- Bridging Java and Groovy code together
CRaSH released version 1.0 earlier this year and has lately introduced an online demo showcasing its basic capabilities. For more information see the documentation, Javadoc and the Google user group.The source code is hosted on GitHub.
HouseMD - Invocation tracing tool