Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[HHQ-3924] Add agent ping CLI command.

  • Loading branch information...
commit 76779754ddcb8c0115797b65b895701712f4ebef 1 parent fb1c864
Ryan Morgan authored
Showing with 85 additions and 2 deletions.
  1. +2 −0  ChangeLog
  2. +83 −2 src/org/hyperic/hq/hqapi1/tools/AgentCommand.java
View
2  ChangeLog
@@ -1,6 +1,8 @@
Changes in HQApi 2.4
+ *) [HHQ-3924] Add agent ping CLI command.
+
*) [HHQ-3786] Allow for listing of alerts from the CLI based on resource
description.
View
85 src/org/hyperic/hq/hqapi1/tools/AgentCommand.java
@@ -31,16 +31,29 @@
import joptsimple.OptionSet;
import org.hyperic.hq.hqapi1.AgentApi;
import org.hyperic.hq.hqapi1.HQApi;
+import org.hyperic.hq.hqapi1.ResourceApi;
import org.hyperic.hq.hqapi1.XmlUtil;
+import org.hyperic.hq.hqapi1.types.Agent;
+import org.hyperic.hq.hqapi1.types.AgentResponse;
import org.hyperic.hq.hqapi1.types.AgentsResponse;
+import org.hyperic.hq.hqapi1.types.PingAgentResponse;
+import org.hyperic.hq.hqapi1.types.ResourceResponse;
+import java.io.InputStream;
import java.util.Arrays;
+import java.util.List;
public class AgentCommand extends Command {
- private static String CMD_LIST = "list";
+ private static final String CMD_LIST = "list";
+ private static final String CMD_PING = "ping";
- private static String[] COMMANDS = { CMD_LIST };
+ private static final String OPT_ID = "id";
+ private static final String OPT_ADDRESS = "agentAddress";
+ private static final String OPT_PORT = "agentPort";
+ private static final String OPT_FQDN = "fqdn";
+
+ private static String[] COMMANDS = { CMD_LIST, CMD_PING };
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
@@ -55,6 +68,8 @@ protected void handleCommand(String[] args) throws Exception {
if (args[0].equals(CMD_LIST)) {
list(trim(args));
+ } else if (args[0].equals(CMD_PING)) {
+ ping(trim(args));
} else {
printUsage();
System.exit(-1);
@@ -72,7 +87,73 @@ private void list(String[] args) throws Exception {
AgentApi agentApi = api.getAgentApi();
AgentsResponse agents = agentApi.getAgents();
+ checkSuccess(agents);
XmlUtil.serialize(agents, System.out, Boolean.TRUE);
}
+
+ private void printPingResponse(Agent a, PingAgentResponse response) {
+ if (response.isUp()) {
+ System.out.println("Successfully pinged agent " + a.getAddress() + ":" + a.getPort());
+ } else {
+ System.out.println("Failure pinging agent at " + a.getAddress() + ":" + a.getPort());
+ }
+ }
+
+ private void ping(String[] args) throws Exception {
+
+ OptionParser p = getOptionParser();
+
+ p.accepts(OPT_ID, "The id of the agent to ping").
+ withRequiredArg().ofType(Integer.class);
+ p.accepts(OPT_ADDRESS, "The address of the agent to ping. Must be used with --" + OPT_PORT).
+ withRequiredArg().ofType(String.class);
+ p.accepts(OPT_PORT, "The port of the agent to ping. Must be used with --" + OPT_ADDRESS).
+ withRequiredArg().ofType(Integer.class);
+ p.accepts(OPT_FQDN, "The platform FQDN of the agent to ping").
+ withRequiredArg().ofType(String.class);
+
+ OptionSet options = getOptions(p, args);
+
+ HQApi api = getApi(options);
+
+ AgentApi agentApi = api.getAgentApi();
+
+ if (options.has(OPT_ID)) {
+ Integer id = (Integer)options.valueOf(OPT_ID);
+ AgentResponse response = agentApi.getAgent(id);
+ checkSuccess(response);
+ PingAgentResponse pingResponse = agentApi.pingAgent(response.getAgent());
+ checkSuccess(pingResponse);
+ printPingResponse(response.getAgent(), pingResponse);
+ } else if (options.has(OPT_ADDRESS) && options.has(OPT_PORT)) {
+ String address = (String)options.valueOf(OPT_ADDRESS);
+ Integer port = (Integer)options.valueOf(OPT_PORT);
+ AgentResponse response = agentApi.getAgent(address, port);
+ checkSuccess(response);
+ PingAgentResponse pingResponse = agentApi.pingAgent(response.getAgent());
+ checkSuccess(pingResponse);
+ printPingResponse(response.getAgent(), pingResponse);
+ } else if (options.has(OPT_FQDN)) {
+ ResourceApi rApi = api.getResourceApi();
+ String fqdn = (String)options.valueOf(OPT_FQDN);
+ ResourceResponse resourceResponse = rApi.getPlatformResource(fqdn, false, false);
+ checkSuccess(resourceResponse);
+ Agent a = resourceResponse.getResource().getAgent();
+ PingAgentResponse pingResponse = agentApi.pingAgent(a);
+ checkSuccess(pingResponse);
+ printPingResponse(a, pingResponse);
+ } else {
+ // Ping via XML
+ InputStream is = getInputStream(options);
+
+ AgentsResponse resp = XmlUtil.deserialize(AgentsResponse.class, is);
+ List<Agent> agents = resp.getAgent();
+ for (Agent a : agents) {
+ PingAgentResponse pingResponse = agentApi.pingAgent(a);
+ checkSuccess(pingResponse);
+ printPingResponse(a, pingResponse);
+ }
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.