diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index fa27cc8a0..d5dbabcb6 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -25,6 +25,7 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; + @SuppressWarnings("unchecked") public class App { private final static Logger LOGGER = Logger.getLogger(App.class.getName()); @@ -81,6 +82,15 @@ public static void main(String[] args) { System.exit(1); } + if( config.getAction().equals( AppConfig.ACTION_LIST_JVMS )) { + List descriptors = com.sun.tools.attach.VirtualMachine.list(); + System.out.println("List of JVMs for user " + System.getProperty("user.name") ); + for( com.sun.tools.attach.VirtualMachineDescriptor descriptor : descriptors ) { + System.out.println( "\tJVM id " + descriptor.id() + ": '" + descriptor.displayName() + "'" ); + } + System.exit(0); + } + // Set up the shutdown hook to properly close resources attachShutdownHook(); @@ -320,8 +330,10 @@ public void init(boolean forceNewConnection) { clearInstances(instances); clearInstances(brokenInstances); + Reporter reporter = appConfig.getReporter(); + Iterator> it = configs.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); diff --git a/src/main/java/org/datadog/jmxfetch/AppConfig.java b/src/main/java/org/datadog/jmxfetch/AppConfig.java index e7fb0bae1..03c66841c 100644 --- a/src/main/java/org/datadog/jmxfetch/AppConfig.java +++ b/src/main/java/org/datadog/jmxfetch/AppConfig.java @@ -18,6 +18,7 @@ @Parameters(separators = "=") class AppConfig { public static final String ACTION_COLLECT = "collect"; + public static final String ACTION_LIST_JVMS = "list_jvms"; public static final String ACTION_LIST_EVERYTHING = "list_everything"; public static final String ACTION_LIST_COLLECTED = "list_collected_attributes"; public static final String ACTION_LIST_MATCHING = "list_matching_attributes"; @@ -25,7 +26,7 @@ class AppConfig { public static final String ACTION_LIST_LIMITED = "list_limited_attributes"; public static final String ACTION_HELP = "help"; public static final HashSet ACTIONS = new HashSet(Arrays.asList(ACTION_COLLECT, ACTION_LIST_EVERYTHING, - ACTION_LIST_COLLECTED, ACTION_LIST_MATCHING, ACTION_LIST_NOT_MATCHING, ACTION_LIST_LIMITED, ACTION_HELP)); + ACTION_LIST_COLLECTED, ACTION_LIST_MATCHING, ACTION_LIST_NOT_MATCHING, ACTION_LIST_LIMITED, ACTION_HELP, ACTION_LIST_JVMS)); @Parameter(names = {"--help", "-h"}, description = "Display this help page", @@ -81,7 +82,7 @@ class AppConfig { @Parameter(description = "Action to take, should be in [help, collect, " + "list_everything, list_collected_attributes, list_matching_attributes, " + - "list_not_matching_attributes, list_limited_attributes]", + "list_not_matching_attributes, list_limited_attributes, list_jvms]", required = true) private List action = null; diff --git a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java index 1474c0767..d02c2de0f 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -2,8 +2,10 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.List; import javax.management.remote.JMXServiceURL; @@ -29,13 +31,15 @@ private JMXServiceURL getAddress(LinkedHashMap connectionParams) throw new IOException("Unnable to attach to process regex: "+ processRegex, e); } return address; - + } private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tools.attach.AttachNotSupportedException, IOException { + List jvms = new ArrayList(); for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) { if (vmd.displayName().matches(processRegex)) { com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(vmd); + LOGGER.info("Matched JVM '" + vmd.displayName() + "' against regex '" + processRegex + "'"); String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); //If jmx agent is not running in VM, load it and return the connector url if (connectorAddress == null) { @@ -48,8 +52,10 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool return connectorAddress; } + jvms.add( vmd.displayName() ); } - throw new IOException("Cannot find JVM matching regex: " + processRegex); + + throw new IOException("Cannot find JVM matching regex: '" + processRegex + "'; available JVMs (for this user account): " + jvms ); } private void loadJMXAgent(com.sun.tools.attach.VirtualMachine vm) throws IOException { diff --git a/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java b/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java index b1e5095a2..9760f1018 100644 --- a/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java +++ b/src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java @@ -312,7 +312,7 @@ public void testParsingAction() { } catch (ParameterException pe) { String expectedMessage = "Main parameters are required (\"Action to take, should be in [help, collect, " + "list_everything, list_collected_attributes, list_matching_attributes, " + - "list_not_matching_attributes, list_limited_attributes]\")"; + "list_not_matching_attributes, list_limited_attributes, list_jvms]\")"; assertEquals(expectedMessage, pe.getMessage()); } @@ -329,7 +329,7 @@ public void testParsingAction() { } catch (ParameterException pe) { String expectedMessage = "Main parameters are required (\"Action to take, should be in [help, collect, " + "list_everything, list_collected_attributes, list_matching_attributes, " + - "list_not_matching_attributes, list_limited_attributes]\")"; + "list_not_matching_attributes, list_limited_attributes, list_jvms]\")"; assertEquals(expectedMessage, pe.getMessage()); } } diff --git a/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java b/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java index ea229f193..db7d71db2 100644 --- a/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java +++ b/src/test/java/org/datadog/jmxfetch/TestServiceChecks.java @@ -113,7 +113,7 @@ public void testServiceCheckCRITICAL() throws Exception { assertEquals(Reporter.formatServiceCheckPrefix("non_running_process"), scName); assertEquals(Status.STATUS_ERROR, scStatus); - assertEquals("Cannot connect to instance process_regex: .*non_running_process_test.* Cannot find JVM matching regex: .*non_running_process_test.*", scMessage); + assertTrue( scMessage, scMessage.startsWith("Cannot connect to instance process_regex: .*non_running_process_test.* Cannot find JVM matching regex: '.*non_running_process_test.*'; available JVMs (for this user account): ")); assertEquals(scTags.length, 3); assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance"));