Monitoring the JVM with SNMP + MRTG |
Some time ago I blogged about monitoring the JVM with SNMP and OpenNMS. Here is an alternative method using the more lightweight MRTG which makes graphs such as the one below.
First you need to enable SNMP in your jvm by adding -Dcom.sun.management.config.file=snmp.properties to your java command line. You can do this for tomcat by adding it to CATALINA_OPTS in your startup script. Next, you need to create that properties file properly. The following example configures the daemon to listen on all interfaces on port 1161. The ACL is disabled so you keep your security configuration in your firewall instead.
com.sun.management.snmp.interface=0.0.0.0
com.sun.management.snmp.port=1161
com.sun.management.snmp.acl=false
Now restart your JVM and test the SNMP daemon using snmpwalk:
$ snmpwalk -c public -v2c host:1161 .1.3.6.1.4.1.42.2.145.3.163.1.1.4.1 SNMPv2-SMI::enterprises.42.2.145.3.163.1.1.4.1.0 = STRING: "pid@host"
Install MRTG with apt-get or whatever package manager your OS uses. Configuration isn't difficult, but there are a few caveats. First MRTG targets must explicity specify SNMP version 2 or you will get the following error:
SNMP Error: Received SNMP response with error code error status: noSuchName index 3 (OID: 1.3.6.1.2.1.1.3.0) SNMPv1_Session (remote host: "xxx" [111.222.333.444].1161)
As explained in the MRTG Reference, the SNMP version to use is specified as a part of the target. Basically you're going to have something like ::::2 on the end of your targets.
The other one that got me is that the OIDs specified in the Java MIB need an extra .0 appended. You can download Java MIB if you want to use OID aliases. Just remember to add the .0 to the alias. To add the MIB to your MRTG configuration, add the path to the LoadMIBs option in the global section. Some useful OIDs to track are:
Variable | OID |
jvmMemoryHeapUsed | 1.3.6.1.4.1.42.2.145.3.163.1.1.2.11.0 |
jvmMemoryHeapCommitted | 1.3.6.1.4.1.42.2.145.3.163.1.1.2.12.0 |
jvmMemoryHeapMaxSize | 1.3.6.1.4.1.42.2.145.3.163.1.1.2.13.0 |
jvmMemoryNonHeapUsed | 1.3.6.1.4.1.42.2.145.3.163.1.1.2.21.0 |
jvmMemoryNonHeapCommited | 1.3.6.1.4.1.42.2.145.3.163.1.1.2.22.0 |
jvmMemoryNonHeapMaxSize | 1.3.6.1.4.1.42.2.145.3.163.1.1.2.23.0 |
jvmThreadCount | 1.3.6.1.4.1.42.2.145.3.163.1.1.3.1.0 |
jvmThreadPeakCount | 1.3.6.1.4.1.42.2.145.3.163.1.1.3.3.0 |
I chose to monitor the heap and threads using the following config:
# JVM Heap Usage PageTop[heap]: <H1>JVM Heap Usage</H1> Title[heap]: JVM Heap Usage Target[heap]: 1.3.6.1.4.1.42.2.145.3.163.1.1.2.11.0&1.3.6.1.4.1.42.2.145.3.163.1.1.2.12.0:public@localhost:1161::::2 / 1000000 MaxBytes[heap]: 1000 YLegend[heap]: Heap Used ShortLegend[heap]: MB LegendI[heap]: Used: LegendO[heap]: Committed: Options[heap]: nopercent,growright,gauge,noinfo # JVM Threads PageTop[threads]: <H1>JVM Threads</H1> Title[threads]: JVM Threads Target[threads]: 1.3.6.1.4.1.42.2.145.3.163.1.1.3.1.0&1.3.6.1.4.1.42.2.145.3.163.1.1.3.3.0:public@localhost:1161::::2 MaxBytes[threads]: 200 YLegend[threads]: JVM Threads LegendI[threads]: Active: LegendO[threads]: Max: ShortLegend[threads]: threads Options[threads]: nopercent,growright,gauge,noinfo
That's it, you can experiment further with the MRTG configuration as necessary. Have fun and try not to check your pretty new graphs too often. It's strangely addictive and can become pointless pretty quickly.
Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food. |