Monitor Artifactory outbound connectionpools via JMX

tags: artifactory jmx groovy

Written on January 6, 2018

If you like to find out how many connections your Artifactory is using for dealing with its configured remote repositories, there is no obvious way to do that. Instead there’s a simple way to monitor the leased, pending, available and max connections via Artifactory’s built-in JMX-beans.

Software and versions:

name version download
Artifactory OSS 5.8.1 https://jfrog.com/artifactory/
Groovy 2.4.12 http://groovy-lang.org/download.html

Given an Artifactory instance with following configured remote repositories:

All you have to do is to configure the JAVA_OPTIONS for Artifactory ($ARTIFACTORY_HOME/bin/artifactory.default) with jmxremote options, e.g.:

export JAVA_OPTIONS="$JAVA_OPTIONS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"

In production I would recommend to use ssl with authentication ;-)

Then you can connect with your favourite JMX-Client:

This is nice, if you want to manually check this current situation. But if you need to continuously monitor the connections, you need a script:

import javax.management.remote.JMXConnector
import javax.management.remote.JMXConnectorFactory
import javax.management.remote.JMXServiceURL
import javax.management.MBeanServerConnection
import javax.management.InstanceNotFoundException
import java.io.IOException

def repoKeys = [
    'repo1',
    'jcenter'
]

def connectionPoolDetailsPerRepo = [:]


String jmxServerUrl = 'service:jmx:rmi:///jndi/rmi://localhost:22222/jmxrmi'
JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl)
MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection()

repoKeys.each { repo ->
    String beanName = "org.jfrog.artifactory:instance=Artifactory, type=HTTPConnectionPool,prop=${repo}"
    try {
        def mbean = new GroovyMBean(server, beanName)
        def connectionPoolDetails = [:]
        connectionPoolDetails['Pending'] = mbean.getProperty("Pending")
        connectionPoolDetails['Leased'] = mbean.getProperty("Leased")
        connectionPoolDetails['Max'] = mbean.getProperty("Max")
        connectionPoolDetails['Available'] = mbean.getProperty("Available")
        connectionPoolDetailsPerRepo[repo] = connectionPoolDetails
    }
    catch (IOException e) {
        println "Unable to connect to JVM"
        System.exit(0)
    }
    catch (InstanceNotFoundException e) {
        println "WARN: ${repo}: InstanceNotFoundException: ${e.message}"
    }
}

jmxConnector.close()

println 'connection pool details per repo:'
connectionPoolDetailsPerRepo.each { repoKey, connectionDetails ->
    println "* ${repoKey}: ${connectionDetails}"
}

By executing the above script, the JMX beans for the given list of remote repositories are requested:

$ groovy artifactory-jmx.groovy
connection pool details per repo:
* repo1: [Pending:0, Leased:0, Max:50, Available:0]
* jcenter: [Pending:0, Leased:0, Max:50, Available:0]