2 * Copyright (c) 2012-2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <mon_internal.h>
42 #include <tr_inet_util.h>
45 /* command-line option setup */
46 static void print_version_info(void)
48 printf("Moonshot Trust Router Monitoring Client %s\n\n", PACKAGE_VERSION);
52 /* argp global parameters */
53 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
56 static const char doc[] =
57 PACKAGE_NAME " - Moonshot Trust Router Monitoring Client"
58 "\v" /* options list goes here */
59 "Supported monitoring commands:\n"
61 " show [<option> ...]\n"
63 " Show information about the Trust Router's current state.\n"
66 " version - current Trust Router software version\n"
67 " config_files - currently loaded configuration files\n"
68 " uptime - time, in seconds, since the Trust Router launched\n"
69 " tid_reqs_processed - number of TID requests completed successfully\n"
70 " tid_reqs_failed - number of TID requests completed with errors\n"
71 " tid_reqs_pending - number of TID requests currently being processed\n"
72 " tid_error_count - number of unprocessable TID connections\n"
73 " routes - current TID routing table\n"
74 " peers - dynamic Trust Router peer table\n"
75 " communities - community table\n"
76 " realms - known realm table\n"
77 " rp_clients - authorized TID RP clients\n"
79 " If no options are specified, data for all options will be retrieved.\n";
81 static const char arg_doc[]="<server> <port> <command> [<option> ...]"; /* string describing arguments, if any */
83 /* define the options here. Fields are:
84 * { long-name, short-name, variable name, options, help description } */
85 static const struct argp_option cmdline_options[] = {
86 { "version", 'v', NULL, 0, "Print version information and exit" },
90 #define MAX_OPTIONS 20
91 /* structure for communicating with option parser */
96 MON_OPT_TYPE options[MAX_OPTIONS];
97 unsigned int n_options;
100 /* parser for individual options - fills in a struct cmdline_args */
101 static error_t parse_option(int key, char *arg, struct argp_state *state)
105 /* get a shorthand to the command line argument structure, part of state */
106 struct cmdline_args *arguments=state->input;
110 print_version_info();
113 case ARGP_KEY_ARG: /* handle argument (not option) */
114 switch (state->arg_num) {
116 arguments->server = arg;
120 arguments->port=tr_parse_port(arg); /* optional */
121 if (arguments->port < 0) {
122 switch(-(arguments->port)) {
124 printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
128 printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
136 arguments->command=mon_cmd_from_string(arg);
137 if (arguments->command == MON_CMD_UNKNOWN) {
138 printf("\nUnknown command '%s'\n\n", arg);
144 if (arguments->n_options >= MAX_OPTIONS) {
145 printf("\nToo many command options given, limit is %d\n\n", MAX_OPTIONS);
150 arguments->options[arguments->n_options] = mon_opt_type_from_string(arg);
151 if (arguments->options[arguments->n_options] == OPT_TYPE_UNKNOWN) {
152 printf("\nUnknown command option '%s'\n\n", arg);
155 arguments->n_options++;
160 case ARGP_KEY_END: /* no more arguments */
161 if (state->arg_num < 3) {
162 /* not enough arguments encountered */
168 return ARGP_ERR_UNKNOWN;
173 return EINVAL; /* argp_usage() usually does not return, but just in case */
176 return 0; /* success */
179 /* assemble the argp parser */
180 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc, 0};
182 int main(int argc, char *argv[])
184 TALLOC_CTX *main_ctx=talloc_new(NULL);
185 MONC_INSTANCE *monc = NULL;
187 MON_RESP *resp = NULL;
190 struct cmdline_args opts;
191 int retval=1; /* exit with an error status unless this gets set to zero */
193 /* parse the command line*/
196 opts.port = TRP_PORT;
197 opts.command = MON_CMD_UNKNOWN;
200 argp_parse(&argp, argc, argv, 0, 0, &opts);
202 /* Use standalone logging */
205 /* set logging levels */
206 talloc_set_log_stderr();
207 tr_log_threshold(LOG_CRIT);
208 tr_console_threshold(LOG_WARNING);
210 /* Create a MON client instance */
211 monc = monc_new(main_ctx);
213 printf("Error allocating client instance.\n");
217 /* Set-up MON connection */
218 if (0 != monc_open_connection(monc, opts.server, opts.port)) {
220 printf("Error opening connection to %s:%d.\n", opts.server, opts.port);
224 req = mon_req_new(main_ctx, opts.command);
225 for (ii=0; ii < opts.n_options; ii++) {
226 if (MON_SUCCESS != mon_req_add_option(req, opts.options[ii])) {
227 printf("Error adding option '%s' to request. Request not sent.\n",
228 mon_opt_type_to_string(opts.options[ii]));
234 /* Send a MON request and get the response */
235 resp = monc_send_request(main_ctx, monc, req);
239 printf("Error executing monitoring request.\n");
243 /* Print the JSON to stdout */
244 json_dumpf(mon_resp_encode(resp), stdout, JSON_INDENT(4));
250 /* Clean-up the MON client instance, and exit */
252 talloc_free(main_ctx);