import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import java.util.Arrays; import java.util.Set; import java.util.HashSet; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; public class Jira { private static String host, port, pluginVersion, heartbeatRequired; public static void main(String[] args) { try { if(args.length == 4){ host = args[0]; port = args[1]; pluginVersion = args[2]; heartbeatRequired = args[3]; } MBeanServerConnection connection = getServerConnection(); ObjectName query = new ObjectName("com.atlassian.jira:*"); Set mbeans = connection.queryNames(query, null); printJmxAttributes(mbeans, connection); } catch(Exception e){ } } private static MBeanServerConnection getServerConnection() { MBeanServerConnection connection = null; try{ JMXServiceURL jmxurl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+host+":"+port+"/jmxrmi"); Map env = new HashMap(); if((System.getenv("RMI_UNAME") != null && System.getenv("RMI_PASSWORD") != null) && (System.getenv("RMI_UNAME").length() > 0 && System.getenv("RMI_PASSWORD").length() > 0)){ String[] credentials = {System.getenv("RMI_UNAME"), System.getenv("RMI_PASSWORD")}; env.put(JMXConnector.CREDENTIALS, credentials); } JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxurl, env); connection = jmxConnector.getMBeanServerConnection(); } catch(Exception e){ String errorMessage = "{" + String.format("\n\t\"plugin_version\" : \"%s\",", pluginVersion) + String.format("\n\t\"heartbeat_required\" : \"%s\"", heartbeatRequired) + "\n\t\"status\" : 0" + "\n\t\"msg\" : \"" + e.toString().trim().replace("\n", "") +"\"" + "\n}"; System.out.println(errorMessage); System.exit(0); } return connection; } private static void printJmxAttributes(Set mbeans, MBeanServerConnection connection){ System.out.print("{"); System.out.print(String.format("\n\t\"plugin_version\" : \"%s\",\n", pluginVersion)); System.out.print(String.format("\t\"heartbeat_required\" : \"%s\"", heartbeatRequired)); String value = ""; boolean isValue; List ignoreMetrics = new ArrayList(); ignoreMetrics.add("BasicDataSource"); ignoreMetrics.add("metrics"); String units = "\t\"units\": {" + "\n\t\t\"db.conns.invocation.count\" : \"ms\"," + "\n\t\t\"db.conns.total.elapsed.time\" : \"ms\"," + "\n\t\t\"db.reads.total.elapsed.time\" : \"ms\"," + "\n\t\t\"db.conns.time.to.borrow\" : \"ms\"," + "\n\t\t\"web.requests.total.elapsed.time\" : \"ms\"" + "\n\t}"; try { for (ObjectName mbean : mbeans) { isValue = false; if(!isStringInArrayList(ignoreMetrics, mbean.toString())){ ObjectName oname = new ObjectName(mbean.toString()); MBeanInfo info = connection.getMBeanInfo(oname); Set attributes = new HashSet(Arrays.asList(info.getAttributes())); for (MBeanAttributeInfo attribute : attributes) { Object obj; if(attribute.getName().equals("Value")){ isValue = true; obj = connection.getAttribute(oname,attribute.getName()); value = obj.toString(); continue; } System.out.print(","); if(isValue){ isValue = false; obj = connection.getAttribute(oname,attribute.getName()); System.out.print("\n\t\"" + obj + "\" : \"" + value + "\""); } else{ obj = connection.getAttribute(oname,attribute.getName()); System.out.print("\n\t\"" + mbean.toString().substring("com.atlassian.jira:type=".length()) + "." + attribute.getName() + "\" : \"" + obj + "\""); } } } } System.out.print(",\n"); System.out.print(units); System.out.print("\n}\n"); } catch(Exception e){} } private static boolean isStringInArrayList(List ignoreMetrics, String metricName){ for(String toIgnore : ignoreMetrics){ if(metricName.contains(toIgnore)){ return true; } } return false; } }