Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / tr / trmon_main.c
index 507bf34..fe5e660 100644 (file)
 #include <stdio.h>
 #include <talloc.h>
 #include <argp.h>
-#include <unistd.h>
 
 #include <mon_internal.h>
-#include <gsscon.h>
 #include <tr_debug.h>
-#include <trust_router/tr_dh.h>
+#include <tr_inet_util.h>
 
 
 /* command-line option setup */
+static void print_version_info(void)
+{
+  printf("Moonshot Trust Router Monitoring Client %s\n\n", PACKAGE_VERSION);
+}
+
 
 /* argp global parameters */
 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
 
 /* doc strings */
-static const char doc[]=PACKAGE_NAME " - TR Monitoring Client";
-static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
+static const char doc[] =
+    PACKAGE_NAME " - Moonshot Trust Router Monitoring Client"
+    "\v" /* options list goes here */
+    "Supported monitoring commands:\n"
+    "\n"
+    "  show [<option> ...]\n"
+    "\n"
+    "     Show information about the Trust Router's current state.\n"
+    "\n"
+    "     Options:\n"
+    "       version            - current Trust Router software version\n"
+    "       config_files       - currently loaded configuration files\n"
+    "       uptime             - time, in seconds, since the Trust Router launched\n"
+    "       tid_reqs_processed - number of TID requests completed successfully\n"
+    "       tid_reqs_failed    - number of TID requests completed with errors\n"
+    "       tid_reqs_pending   - number of TID requests currently being processed\n"
+    "       tid_error_count    - number of unprocessable TID connections\n"
+    "       routes             - current TID routing table\n"
+    "       peers              - dynamic Trust Router peer table\n"
+    "       communities        - community table\n"
+    "       realms             - known realm table\n"
+    "       rp_clients         - authorized TID RP clients\n"
+    "\n"
+    "    If no options are specified, data for all options will be retrieved.\n";
+
+static const char arg_doc[]="<server> <port> <command> [<option> ...]"; /* string describing arguments, if any */
 
 /* define the options here. Fields are:
  * { long-name, short-name, variable name, options, help description } */
 static const struct argp_option cmdline_options[] = {
-  { "repeat", 'r', "N", OPTION_ARG_OPTIONAL, "Repeat message until terminated, or N times." },
-  {NULL}
+    { "version", 'v', NULL, 0, "Print version information and exit" },
+    {NULL}
 };
 
+#define MAX_OPTIONS 20
 /* structure for communicating with option parser */
 struct cmdline_args {
-  char *msg;
   char *server;
-  int port; /* optional */
-  int repeat; /* how many times to repeat, or -1 for infinite */
+  int port;
+  MON_CMD command;
+  MON_OPT_TYPE options[MAX_OPTIONS];
+  unsigned int n_options;
 };
 
 /* parser for individual options - fills in a struct cmdline_args */
 static error_t parse_option(int key, char *arg, struct argp_state *state)
 {
+  int err = 0;
+
   /* get a shorthand to the command line argument structure, part of state */
   struct cmdline_args *arguments=state->input;
 
   switch (key) {
-  case 'r':
-    if (arg==NULL)
-      arguments->repeat=-1;
-    else
-      arguments->repeat=strtol(arg, NULL, 10);
-    break;
-
-  case ARGP_KEY_ARG: /* handle argument (not option) */
-    switch (state->arg_num) {
-    case 0:
-      arguments->msg=arg;
-      break;
-
-    case 1:
-      arguments->server=arg;
+    case 'v':
+      print_version_info();
+      exit(0);
+
+    case ARGP_KEY_ARG: /* handle argument (not option) */
+      switch (state->arg_num) {
+        case 0:
+          arguments->server = arg;
+          break;
+
+        case 1:
+          arguments->port=tr_parse_port(arg); /* optional */
+          if (arguments->port < 0) {
+            switch(-(arguments->port)) {
+              case ERANGE:
+                printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
+                break;
+
+              default:
+                printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
+                break;
+            }
+            argp_usage(state);
+          }
+          break;
+
+        case 2:
+          arguments->command=mon_cmd_from_string(arg);
+          if (arguments->command == MON_CMD_UNKNOWN) {
+            printf("\nUnknown command '%s'\n\n", arg);
+            err = 1;
+          }
+          break;
+
+        default:
+          if (arguments->n_options >= MAX_OPTIONS) {
+            printf("\nToo many command options given, limit is %d\n\n", MAX_OPTIONS);
+            err = 1;
+            break;
+          }
+
+          arguments->options[arguments->n_options] = mon_opt_type_from_string(arg);
+          if (arguments->options[arguments->n_options] == OPT_TYPE_UNKNOWN) {
+            printf("\nUnknown command option '%s'\n\n", arg);
+            err = 1;
+          }
+          arguments->n_options++;
+          break;
+      }
       break;
 
-    case 2:
-      arguments->port=strtol(arg, NULL, 10); /* optional */
+    case ARGP_KEY_END: /* no more arguments */
+      if (state->arg_num < 3) {
+        /* not enough arguments encountered */
+        err = 1;
+      }
       break;
 
     default:
-      /* too many arguments */
-      argp_usage(state);
-    }
-    break;
-
-  case ARGP_KEY_END: /* no more arguments */
-    if (state->arg_num < 2) {
-      /* not enough arguments encountered */
-      argp_usage(state);
-    }
-    break;
+      return ARGP_ERR_UNKNOWN;
+  }
 
-  default:
-    return ARGP_ERR_UNKNOWN;
+  if (err) {
+    argp_usage(state);
+    return EINVAL; /* argp_usage() usually does not return, but just in case */
   }
 
   return 0; /* success */
 }
 
-
 /* assemble the argp parser */
-static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
+static struct argp argp = {cmdline_options, parse_option, arg_doc, doc, 0};
 
-int main (int argc, 
-          char *argv[]) 
+int main(int argc, char *argv[])
 {
   TALLOC_CTX *main_ctx=talloc_new(NULL);
-  MONC_INSTANCE *monc=NULL;
+  MONC_INSTANCE *monc = NULL;
+  MON_REQ *req = NULL;
+  MON_RESP *resp = NULL;
+  unsigned int ii;
+
   struct cmdline_args opts;
-  int rc;
-  int conn;
-  gss_ctx_id_t gssctx;
+  int retval=1; /* exit with an error status unless this gets set to zero */
 
   /* parse the command line*/
   /* set defaults */
-  opts.msg=NULL;
-  opts.server=NULL;
-  opts.port=TRP_PORT;
-  opts.repeat=1;
+  opts.server = NULL;
+  opts.port = TRP_PORT;
+  opts.command = MON_CMD_UNKNOWN;
+  opts.n_options = 0;
 
   argp_parse(&argp, argc, argv, 0, 0, &opts);
 
@@ -145,34 +205,51 @@ int main (int argc,
   /* set logging levels */
   talloc_set_log_stderr();
   tr_log_threshold(LOG_CRIT);
-  tr_console_threshold(LOG_DEBUG);
-
-  printf("TR Monitor:\nServer = %s, port = %i\n", opts.server, opts.port);
+  tr_console_threshold(LOG_WARNING);
 
-  /* Create a MON client instance & the client DH */
-  monc = monc_create();
-  if (NULL == (monc->client_dh = tr_create_dh_params(main_ctx, 0))) {
-    printf("Error creating client DH params.\n");
-    return 1;
+  /* Create a MON client instance */
+  monc = monc_new(main_ctx);
+  if (monc == NULL) {
+    printf("Error allocating client instance.\n");
+    goto cleanup;
   }
 
   /* Set-up MON connection */
-  if (-1 == (conn = monc_open_connection(monc, opts.server, opts.port, &gssctx))) {
+  if (0 != monc_open_connection(monc, opts.server, opts.port)) {
     /* Handle error */
-    printf("Error in monc_open_connection.\n");
-    return 1;
+    printf("Error opening connection to %s:%d.\n", opts.server, opts.port);
+    goto cleanup;
   };
 
-  /* Send a MON request */
-  if (0 > (rc = monc_send_request(monc, conn, gssctx, NULL, NULL))) {
+  req = mon_req_new(main_ctx, opts.command);
+  for (ii=0; ii < opts.n_options; ii++) {
+    if (MON_SUCCESS != mon_req_add_option(req, opts.options[ii])) {
+      printf("Error adding option '%s' to request. Request not sent.\n",
+             mon_opt_type_to_string(opts.options[ii]));
+      goto cleanup;
+    }
+
+  }
+
+  /* Send a MON request and get the response */
+  resp = monc_send_request(main_ctx, monc, req);
+
+  if (resp == NULL) {
     /* Handle error */
-    printf("Error in monc_send_request, rc = %d.\n", rc);
-    return 1;
+    printf("Error executing monitoring request.\n");
+    goto cleanup;
   }
 
+  /* Print the JSON to stdout */
+  json_dumpf(mon_resp_encode(resp), stdout, JSON_INDENT(4));
+  printf("\n");
+
+  /* success */
+  retval = 0;
+
   /* Clean-up the MON client instance, and exit */
-  monc_destroy(monc);
+cleanup:
   talloc_free(main_ctx);
-  return 0;
+  return retval;
 }