Skip to content

Commit

Permalink
Added list_jvms option to list available JVMs (that this process can …
Browse files Browse the repository at this point in the history
…see)
  • Loading branch information
cslee00 authored and yannmh committed Oct 26, 2016
1 parent c574055 commit 8542d40
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -81,6 +82,15 @@ public static void main(String[] args) {
System.exit(1);
}

if( config.getAction().equals( AppConfig.ACTION_LIST_JVMS )) {
List<com.sun.tools.attach.VirtualMachineDescriptor> 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();

Expand Down Expand Up @@ -320,8 +330,10 @@ public void init(boolean forceNewConnection) {
clearInstances(instances);
clearInstances(brokenInstances);


Reporter reporter = appConfig.getReporter();


Iterator<Entry<String, YamlParser>> it = configs.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, YamlParser> entry = it.next();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/datadog/jmxfetch/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
@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";
public static final String ACTION_LIST_NOT_MATCHING = "list_not_matching_attributes";
public static final String ACTION_LIST_LIMITED = "list_limited_attributes";
public static final String ACTION_HELP = "help";
public static final HashSet<String> ACTIONS = new HashSet<String>(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",
Expand Down Expand Up @@ -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<String> action = null;

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/datadog/jmxfetch/AttachApiConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,13 +31,15 @@ private JMXServiceURL getAddress(LinkedHashMap<String, Object> 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<String> jvms = new ArrayList<String>();
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) {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/datadog/jmxfetch/TestServiceChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down

0 comments on commit 8542d40

Please sign in to comment.