Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A quick and dirty Groovy script to monitor Mongo connections for a short time
import javax.management.remote.*
import javax.management.*
import groovy.jmx.builder.*
// see https://mongodb.github.io/mongo-java-driver/3.0/driver/reference/management/monitoring/
attrs = ['CheckedOutCount','MinSize','MaxSize','WaitQueueSize','Size']
def print_mbeans(def mbeans) {
def webBean = mbeans.queryNames(new ObjectName('org.mongodb.driver:*'), null).find {
name->
def values = [ new Date() as String, name.getKeyProperty('clusterId')]
attrs.each { attr->
values<<mbeans.getAttribute(name,attr)
}
println values.join(",")
}
}
// Setup JMX connection.
def connection = new JmxBuilder().client(port: 8991, host: 'localhost')
connection.connect()
// Get the MBeanServer.
def mbeans = connection.MBeanServerConnection
print "Date,ClusterId,"
println attrs.join(",")
// change 10 to however many seconds you want this to run
(1..10).each {
print_mbeans(mbeans)
Thread.sleep(1000)
}
print "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.