Annotation of embedaddon/strongswan/src/swanctl/commands/counters.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2017 Tobias Brunner
3: * HSR Hochschule fuer Technik Rapperswil
4: *
5: * This program is free software; you can redistribute it and/or modify it
6: * under the terms of the GNU General Public License as published by the
7: * Free Software Foundation; either version 2 of the License, or (at your
8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9: *
10: * This program is distributed in the hope that it will be useful, but
11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13: * for more details.
14: */
15:
16: #define _GNU_SOURCE
17: #include <stdio.h>
18: #include <errno.h>
19:
20: #include "command.h"
21:
22: CALLBACK(counters_kv, int,
23: void *null, vici_res_t *res, char *name, void *value, int len)
24: {
25: if (chunk_printable(chunk_create(value, len), NULL, ' '))
26: {
27: printf(" %-22s: %.*s\n", name, len, value);
28: }
29: return 0;
30: }
31:
32: CALLBACK(conns_sn, int,
33: void *null, vici_res_t *res, char *name)
34: {
35: printf("%s:\n", strlen(name) ? name : "global");
36: return vici_parse_cb(res, NULL, counters_kv, NULL, NULL);
37: }
38:
39: CALLBACK(counters_sn, int,
40: void *null, vici_res_t *res, char *name)
41: {
42: return vici_parse_cb(res, conns_sn, NULL, NULL, NULL);
43: }
44:
45: static int counters(vici_conn_t *conn)
46: {
47: vici_req_t *req;
48: vici_res_t *res;
49: command_format_options_t format = COMMAND_FORMAT_NONE;
50: char *arg, *name = NULL;
51: int ret = 0;
52: bool all = FALSE, reset = FALSE;
53:
54: while (TRUE)
55: {
56: switch (command_getopt(&arg))
57: {
58: case 'h':
59: return command_usage(NULL);
60: case 'P':
61: format |= COMMAND_FORMAT_PRETTY;
62: /* fall through to raw */
63: case 'r':
64: format |= COMMAND_FORMAT_RAW;
65: continue;
66: case 'n':
67: name = arg;
68: continue;
69: case 'a':
70: all = TRUE;
71: continue;
72: case 'R':
73: reset = TRUE;
74: continue;
75: case EOF:
76: break;
77: default:
78: return command_usage("invalid --counters option");
79: }
80: break;
81: }
82: if (reset)
83: {
84: req = vici_begin("reset-counters");
85: }
86: else
87: {
88: req = vici_begin("get-counters");
89: }
90: if (all)
91: {
92: vici_add_key_valuef(req, "all", "yes");
93: }
94: else if (name)
95: {
96: vici_add_key_valuef(req, "name", "%s", name);
97: }
98:
99: res = vici_submit(req, conn);
100: if (!res)
101: {
102: ret = errno;
103: fprintf(stderr, "%s-counters request failed: %s\n",
104: reset ? "reset" : "get", strerror(errno));
105: return ret;
106: }
107: if (format & COMMAND_FORMAT_RAW)
108: {
109: vici_dump(res, "counters reply", format & COMMAND_FORMAT_PRETTY,
110: stdout);
111: }
112: else
113: {
114: if (streq(vici_find_str(res, "no", "success"), "yes"))
115: {
116: if (reset)
117: {
118: printf("reset-counters completed successfully\n");
119: }
120: else if (vici_parse_cb(res, counters_sn, NULL, NULL, NULL) != 0)
121: {
122: fprintf(stderr, "parsing get-counters reply failed: %s\n",
123: strerror(errno));
124: }
125: }
126: else
127: {
128: fprintf(stderr, "%s-counters failed: %s\n", reset ? "reset" : "get",
129: vici_find_str(res, "", "errmsg"));
130: ret = 1;
131: }
132: }
133: vici_free_res(res);
134: return ret;
135: }
136:
137: /**
138: * Register the command.
139: */
140: static void __attribute__ ((constructor))reg()
141: {
142: command_register((command_t) {
143: counters, 'C', "counters", "list or reset IKE event counters",
144: {"[--name <name>|--all] [--reset] [--raw|--pretty]"},
145: {
146: {"help", 'h', 0, "show usage information"},
147: {"name", 'n', 1, "connection name, omit for global counters"},
148: {"all", 'a', 0, "get/reset counters for all tracked connections"},
149: {"reset", 'R', 0, "reset the counters"},
150: {"raw", 'r', 0, "dump raw response message"},
151: {"pretty", 'P', 0, "dump raw response message in pretty print"},
152: }
153: });
154: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>