diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index fa27cc8a0..9f6651d91 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,16 @@ 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 +331,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(); @@ -356,7 +369,7 @@ public void init(boolean forceNewConnection) { } catch (IOException e) { instance.cleanUp(); brokenInstances.add(instance); - String warning = CANNOT_CONNECT_TO_INSTANCE + instance + " " + e.getMessage(); + String warning = CANNOT_CONNECT_TO_INSTANCE + instance + ". " + e.getMessage(); this.reportStatus(appConfig, reporter, instance, 0, warning, Status.STATUS_ERROR); this.sendServiceCheck(reporter, instance, warning, Status.STATUS_ERROR); LOGGER.error(warning); 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..7ad40d878 100644 --- a/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java +++ b/src/main/java/org/datadog/jmxfetch/AttachApiConnection.java @@ -29,7 +29,7 @@ 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 { @@ -49,7 +49,8 @@ private String getJMXUrlForProcessRegex(String processRegex) throws com.sun.tool return connectorAddress; } } - throw new IOException("Cannot find JVM matching regex: " + processRegex); + + throw new IOException("No match found. Available JVMs can be listed with the `list_jvms` command."); } private void loadJMXAgent(com.sun.tools.attach.VirtualMachine vm) throws IOException { diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 4df33ea83..0f84e32fe 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -136,7 +136,7 @@ public void init(boolean forceNewConnection) throws IOException, FailedLoginExce @Override public String toString() { if (this.yaml.get(PROCESS_NAME_REGEX) != null) { - return "process_regex: " + this.yaml.get(PROCESS_NAME_REGEX); + return "process_regex: `" + this.yaml.get(PROCESS_NAME_REGEX) + "`"; } else if (this.yaml.get("jmx_url") != null) { return (String) this.yaml.get("jmx_url"); } else { 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..fe296f268 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); + assertEquals("Cannot connect to instance process_regex: `.*non_running_process_test.*`. No match found. Available JVMs can be listed with the `list_jvms` command.", scMessage); assertEquals(scTags.length, 3); assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance")); @@ -137,7 +137,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.*. Is a JMX Server running at this address?", scMessage); + assertEquals("Cannot connect to instance process_regex: `.*non_running_process_test.*`. Is a JMX Server running at this address?", scMessage); assertEquals(scTags.length, 3); assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance")); assertTrue(Arrays.asList(scTags).contains("env:stage"));