Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Last active April 27, 2021 00:27
Show Gist options
  • Save maurorappa/07ce39130cbb1249f8f3746393d66a02 to your computer and use it in GitHub Desktop.
Save maurorappa/07ce39130cbb1249f8f3746393d66a02 to your computer and use it in GitHub Desktop.
Monitoring via JMX for long periods
I had a problem which required a closed monitoring of AMQ resources.
All Enterprise Java program can be monitored by JMX and the standard interface to visualize instrumentation data is JConsole.
Jconsole is great for realtime monitoring, but our load tests were running for hours and we needed all raw data to be parsed offline, a Jconsole graph is an approximation for our purposes.
I wanted a Cli tool to save on file all raw numbers; first I needed a java utility to query java processes via JMX protocol.
I choose jmxterm ( http://wiki.cyclopsgroup.org/jmxterm/ ) which is simple and scriptable.
I also didn't want to spin a java process (java -vat jmxterm) every minute in a loop which is the easiest way but the most inefficient.
the right way is to start jmxclient, connect to the target, run periodically methods on the selected beans and save the data.
Jmxclient is an interactive tool so we couldn't really script is in a traditional way like bash,python or perl but I had to use expect.
This is my script which runs continuously and log the required stats with the timestamp:
#!/usr/bin/expect -f
set CHILD_PID [spawn java -jar jmxterm-1.0-alpha-4-uber.jar --url service:jmx:rmi:///jndi/rmi://localhost:11510/jmxrmi]
send "bean org.apache.activemq:BrokerName=bbc-activemq-idsrv-cwwtf,Type=Broker\r"
stty -echo
while { true } {
set now [clock seconds]
set date [clock format $now -format {%X}]
puts $date
send "get MemoryPercentUsage\r"
stty -echo
expect sleep 5
}
send "quit \n"
expect eof
The best guide for expect I found so far is this book:
https://books.google.co.uk/books?id=BJpkh3RdG_UC&pg=PA197&lpg=PA197&dq=expect+input+sleep+input&source=bl&ots=PpTqwXybUq&sig=Mk2w43QMap56xnz_ctsIr2GnqGU&hl=en&sa=X&ved
@sarnobat
Copy link

Thanks for the example and the book. I was hoping jmxterm would be easier than my script but it's not: https://github.com/sarnobat/jmx_programmatic/blob/main/src/main/java/PrintMBeans.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment