7ec6a4f83ba28df06a75f23448ff22efcbf562c4
[trust_router.git] / tr / trmon_main.c
1 /*
2  * Copyright (c) 2012-2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <talloc.h>
38 #include <argp.h>
39
40 #include <mon_internal.h>
41 #include <tr_debug.h>
42
43
44 /* command-line option setup */
45 static void print_version_info(void)
46 {
47   printf("Moonshot Trust Router Monitoring Client %s\n\n", PACKAGE_VERSION);
48 }
49
50
51 /* argp global parameters */
52 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
53
54 /* doc strings */
55 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router Monitoring Client";
56 static const char arg_doc[]="<server> <port> <command> [<option> ...]"; /* string describing arguments, if any */
57
58 /* define the options here. Fields are:
59  * { long-name, short-name, variable name, options, help description } */
60 static const struct argp_option cmdline_options[] = {
61     { "version", 'v', NULL, 0, "Print version information and exit" },
62     {NULL}
63 };
64
65 #define MAX_OPTIONS 20
66 /* structure for communicating with option parser */
67 struct cmdline_args {
68   char *server;
69   int port;
70   MON_CMD command;
71   MON_OPT_TYPE options[MAX_OPTIONS];
72   unsigned int n_options;
73 };
74
75 /* parser for individual options - fills in a struct cmdline_args */
76 static error_t parse_option(int key, char *arg, struct argp_state *state)
77 {
78   long tmp_l = 0;
79
80   /* get a shorthand to the command line argument structure, part of state */
81   struct cmdline_args *arguments=state->input;
82
83   switch (key) {
84     case 'v':
85       print_version_info();
86       exit(0);
87       break;
88
89     case ARGP_KEY_ARG: /* handle argument (not option) */
90       switch (state->arg_num) {
91         case 0:
92           arguments->server = arg;
93           break;
94
95         case 1:
96           tmp_l = strtol(arg, NULL, 10);
97           if (errno || (tmp_l < 0) || (tmp_l > 65535)) /* max valid port */
98             argp_usage(state);
99
100           arguments->port = (int) tmp_l; /* we already checked the range */
101           break;
102
103         case 2:
104           arguments->command=mon_cmd_from_string(arg);
105           if (arguments->command == MON_CMD_UNKNOWN) {
106             printf("\nUnknown command '%s'\n", arg);
107             argp_usage(state);
108           }
109           break;
110
111         default:
112           if (arguments->n_options >= MAX_OPTIONS) {
113             printf("\nToo many command options given, limit is %d\n", MAX_OPTIONS);
114             argp_usage(state);
115           }
116
117           arguments->options[arguments->n_options] = mon_opt_type_from_string(arg);
118           if (arguments->options[arguments->n_options] == OPT_TYPE_UNKNOWN) {
119             printf("\nUnknown command option '%s'\n", arg);
120             argp_usage(state);
121           }
122           arguments->n_options++;
123           break;
124       }
125       break;
126
127     case ARGP_KEY_END: /* no more arguments */
128       if (state->arg_num < 3) {
129         /* not enough arguments encountered */
130         argp_usage(state);
131       }
132       break;
133
134     default:
135       return ARGP_ERR_UNKNOWN;
136   }
137
138   return 0; /* success */
139 }
140
141
142 /* assemble the argp parser */
143 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
144
145 int main(int argc, char *argv[])
146 {
147   TALLOC_CTX *main_ctx=talloc_new(NULL);
148   MONC_INSTANCE *monc = NULL;
149   MON_REQ *req = NULL;
150   MON_RESP *resp = NULL;
151   unsigned int ii;
152
153   struct cmdline_args opts;
154   int retval=1; /* exit with an error status unless this gets set to zero */
155
156   /* parse the command line*/
157   /* set defaults */
158   opts.server = NULL;
159   opts.port = TRP_PORT;
160   opts.command = MON_CMD_UNKNOWN;
161   opts.n_options = 0;
162
163   argp_parse(&argp, argc, argv, 0, 0, &opts);
164
165   /* Use standalone logging */
166   tr_log_open();
167
168   /* set logging levels */
169   talloc_set_log_stderr();
170   tr_log_threshold(LOG_CRIT);
171   tr_console_threshold(LOG_WARNING);
172
173   /* Create a MON client instance */
174   monc = monc_new(main_ctx);
175   if (monc == NULL) {
176     printf("Error allocating client instance.\n");
177     goto cleanup;
178   }
179
180   /* Set-up MON connection */
181   if (0 != monc_open_connection(monc, opts.server, opts.port)) {
182     /* Handle error */
183     printf("Error opening connection to %s:%d.\n", opts.server, opts.port);
184     goto cleanup;
185   };
186
187   req = mon_req_new(main_ctx, opts.command);
188   for (ii=0; ii < opts.n_options; ii++) {
189     if (MON_SUCCESS != mon_req_add_option(req, opts.options[ii])) {
190       printf("Error adding option '%s' to request. Request not sent.\n",
191              mon_opt_type_to_string(opts.options[ii]));
192       goto cleanup;
193     }
194
195   }
196
197   /* Send a MON request and get the response */
198   resp = monc_send_request(main_ctx, monc, req);
199
200   if (resp == NULL) {
201     /* Handle error */
202     printf("Error executing monitoring request.\n");
203     goto cleanup;
204   }
205
206   /* Print the JSON to stdout */
207   json_dumpf(mon_resp_encode(resp), stdout, JSON_INDENT(4));
208   printf("\n");
209
210   /* success */
211   retval = 0;
212
213   /* Clean-up the MON client instance, and exit */
214 cleanup:
215   talloc_free(main_ctx);
216   return retval;
217 }
218