From 4ddf9faef01bd37f79d1635861c4d0669d21e6aa Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Sat, 14 May 2016 16:51:40 -0700 Subject: [PATCH 1/6] Added list_jvms option to list available JVMs (that this process can see) --- src/main/java/org/datadog/jmxfetch/App.java | 12 ++++++++++++ src/main/java/org/datadog/jmxfetch/AppConfig.java | 5 +++-- .../org/datadog/jmxfetch/AttachApiConnection.java | 10 +++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index fa27cc8a0..0ee6b72af 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -24,6 +24,7 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; +import com.sun.tools.attach.VirtualMachineDescriptor; @SuppressWarnings("unchecked") public class App { @@ -320,8 +321,19 @@ public void init(boolean forceNewConnection) { clearInstances(instances); clearInstances(brokenInstances); + Reporter reporter = appConfig.getReporter(); + String action = appConfig.getAction(); + + if( action.equals( AppConfig.ACTION_LIST_JVMS )) { + List descriptors = com.sun.tools.attach.VirtualMachine.list(); + for( VirtualMachineDescriptor descriptor : descriptors ) { + System.out.println( "\tJVM id " + descriptor.id() + ": '" + descriptor.displayName() + "'" ); + } + return; + } + 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..d3bb3d3e2 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; @@ -35,6 +37,7 @@ private JMXServiceURL getAddress(LinkedHashMap connectionParams) private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tools.attach.AttachNotSupportedException, IOException { for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) { if (vmd.displayName().matches(processRegex)) { + LOGGER.info("Matched JVM '" + vmd.displayName() + "' against regex '" + processRegex + "'"); com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(vmd); String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); //If jmx agent is not running in VM, load it and return the connector url @@ -49,7 +52,12 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool return connectorAddress; } } - throw new IOException("Cannot find JVM matching regex: " + processRegex); + + List jvms = new ArrayList(); + for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) { + jvms.add( vmd.displayName() ); + } + 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 { From 4a831683a2a0676f0a566e860612460bae537e87 Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Fri, 3 Jun 2016 21:17:18 -0700 Subject: [PATCH 2/6] Adjustements based on pull request feedback --- src/main/java/org/datadog/jmxfetch/App.java | 19 ++++++++++--------- .../datadog/jmxfetch/AttachApiConnection.java | 8 +++----- .../jmxfetch/TestParsingJCommander.java | 4 ++-- .../datadog/jmxfetch/TestServiceChecks.java | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 0ee6b72af..cb3d5df59 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -24,6 +24,7 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; +import com.sun.tools.attach.VirtualMachine; import com.sun.tools.attach.VirtualMachineDescriptor; @SuppressWarnings("unchecked") @@ -82,6 +83,15 @@ public static void main(String[] args) { System.exit(1); } + if( config.getAction().equals( AppConfig.ACTION_LIST_JVMS )) { + List descriptors = VirtualMachine.list(); + System.out.println("List of JVMs for user " + System.getProperty("user.name") ); + for( 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(); @@ -324,15 +334,6 @@ public void init(boolean forceNewConnection) { Reporter reporter = appConfig.getReporter(); - String action = appConfig.getAction(); - - if( action.equals( AppConfig.ACTION_LIST_JVMS )) { - List descriptors = com.sun.tools.attach.VirtualMachine.list(); - for( VirtualMachineDescriptor descriptor : descriptors ) { - System.out.println( "\tJVM id " + descriptor.id() + ": '" + descriptor.displayName() + "'" ); - } - return; - } Iterator> it = configs.entrySet().iterator(); while (it.hasNext()) { diff --git a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java index d3bb3d3e2..0cf761682 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -31,10 +31,11 @@ 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)) { LOGGER.info("Matched JVM '" + vmd.displayName() + "' against regex '" + processRegex + "'"); @@ -51,12 +52,9 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool return connectorAddress; } + jvms.add( vmd.displayName() ); } - List jvms = new ArrayList(); - for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) { - jvms.add( vmd.displayName() ); - } throw new IOException("Cannot find JVM matching regex: '" + processRegex + "'; available JVMs (for this user account): " + jvms ); } 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")); From 100c26f00772e5c57c6d3777292ac748a3aeb680 Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Fri, 3 Jun 2016 21:24:11 -0700 Subject: [PATCH 3/6] Fix crash on OpenJDK --- src/main/java/org/datadog/jmxfetch/AttachApiConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java index 0cf761682..d02c2de0f 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -38,8 +38,8 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool List jvms = new ArrayList(); for (com.sun.tools.attach.VirtualMachineDescriptor vmd : com.sun.tools.attach.VirtualMachine.list()) { if (vmd.displayName().matches(processRegex)) { - LOGGER.info("Matched JVM '" + vmd.displayName() + "' against regex '" + 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) { From 6715343debb234a790514f63228a82538543521e Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Fri, 3 Jun 2016 21:30:53 -0700 Subject: [PATCH 4/6] Fix crash on OpenJDK --- src/main/java/org/datadog/jmxfetch/AttachApiConnection.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java index d02c2de0f..620150f93 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -39,7 +39,7 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool 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 + "'"); +// 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) { @@ -52,7 +52,7 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool return connectorAddress; } - jvms.add( vmd.displayName() ); +// jvms.add( vmd.displayName() ); } throw new IOException("Cannot find JVM matching regex: '" + processRegex + "'; available JVMs (for this user account): " + jvms ); From 0b44718e4e555ac161277116fd462e891fe85666 Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Fri, 3 Jun 2016 21:33:40 -0700 Subject: [PATCH 5/6] Fix crash on OpenJDK --- src/main/java/org/datadog/jmxfetch/AttachApiConnection.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java index 620150f93..d02c2de0f 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -39,7 +39,7 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool 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 + "'"); + 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) { @@ -52,7 +52,7 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool return connectorAddress; } -// jvms.add( vmd.displayName() ); + jvms.add( vmd.displayName() ); } throw new IOException("Cannot find JVM matching regex: '" + processRegex + "'; available JVMs (for this user account): " + jvms ); From 60f37443c91608201e24743c5a01bf1033a7bc80 Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Fri, 3 Jun 2016 21:44:50 -0700 Subject: [PATCH 6/6] Fix crash on OpenJDK --- src/main/java/org/datadog/jmxfetch/App.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index cb3d5df59..d5dbabcb6 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -24,8 +24,7 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; -import com.sun.tools.attach.VirtualMachine; -import com.sun.tools.attach.VirtualMachineDescriptor; + @SuppressWarnings("unchecked") public class App { @@ -84,9 +83,9 @@ public static void main(String[] args) { } if( config.getAction().equals( AppConfig.ACTION_LIST_JVMS )) { - List descriptors = VirtualMachine.list(); + List descriptors = com.sun.tools.attach.VirtualMachine.list(); System.out.println("List of JVMs for user " + System.getProperty("user.name") ); - for( VirtualMachineDescriptor descriptor : descriptors ) { + for( com.sun.tools.attach.VirtualMachineDescriptor descriptor : descriptors ) { System.out.println( "\tJVM id " + descriptor.id() + ": '" + descriptor.displayName() + "'" ); } System.exit(0);