Skip to content

Commit

Permalink
Merge pull request #112 from DataDog/cslee00-list-attachable-processes
Browse files Browse the repository at this point in the history
`list_jvms` command
  • Loading branch information
yannmh authored Oct 26, 2016
2 parents c574055 + a823eda commit e69217c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
15 changes: 14 additions & 1 deletion 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,16 @@ 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 +331,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 Expand Up @@ -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);
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
5 changes: 3 additions & 2 deletions src/main/java/org/datadog/jmxfetch/AttachApiConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
4 changes: 2 additions & 2 deletions 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);
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"));

Expand All @@ -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"));
Expand Down

0 comments on commit e69217c

Please sign in to comment.