vici: Match identity with wildcards against remote ID in redirect command
[strongswan.git] / src / swanctl / commands / stats.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
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 #include "command.h"
17
18 #include <collections/hashtable.h>
19
20 #include <errno.h>
21
22 CALLBACK(list, int,
23 hashtable_t *sa, vici_res_t *res, char *name, void *value, int len)
24 {
25 printf(" %.*s", len, value);
26 return 0;
27 }
28
29 static int stats(vici_conn_t *conn)
30 {
31 vici_req_t *req;
32 vici_res_t *res;
33 char *arg;
34 command_format_options_t format = COMMAND_FORMAT_NONE;
35 int ret;
36
37 while (TRUE)
38 {
39 switch (command_getopt(&arg))
40 {
41 case 'h':
42 return command_usage(NULL);
43 case 'P':
44 format |= COMMAND_FORMAT_PRETTY;
45 /* fall through to raw */
46 case 'r':
47 format |= COMMAND_FORMAT_RAW;
48 continue;
49 case EOF:
50 break;
51 default:
52 return command_usage("invalid --stats option");
53 }
54 break;
55 }
56
57 req = vici_begin("stats");
58 res = vici_submit(req, conn);
59 if (!res)
60 {
61 ret = errno;
62 fprintf(stderr, "stats request failed: %s\n", strerror(errno));
63 return ret;
64 }
65 if (format & COMMAND_FORMAT_RAW)
66 {
67 vici_dump(res, "stats reply", format & COMMAND_FORMAT_PRETTY, stdout);
68 }
69 else
70 {
71 printf("uptime: %s, since %s\n",
72 vici_find_str(res, "", "uptime.running"),
73 vici_find_str(res, "", "uptime.since"));
74
75 printf("worker threads: %s total, %s idle, working: %s/%s/%s/%s\n",
76 vici_find_str(res, "", "workers.total"),
77 vici_find_str(res, "", "workers.idle"),
78 vici_find_str(res, "", "workers.active.critical"),
79 vici_find_str(res, "", "workers.active.high"),
80 vici_find_str(res, "", "workers.active.medium"),
81 vici_find_str(res, "", "workers.active.low"));
82
83 printf("job queues: %s/%s/%s/%s\n",
84 vici_find_str(res, "", "queues.critical"),
85 vici_find_str(res, "", "queues.high"),
86 vici_find_str(res, "", "queues.medium"),
87 vici_find_str(res, "", "queues.low"));
88
89 printf("jobs scheduled: %s\n",
90 vici_find_str(res, "", "scheduled"));
91
92 printf("IKE_SAs: %s total, %s half-open\n",
93 vici_find_str(res, "", "ikesas.total"),
94 vici_find_str(res, "", "ikesas.half-open"));
95
96 if (vici_find_str(res, NULL, "mem.total"))
97 {
98 printf("memory usage: %s bytes, %s allocations\n",
99 vici_find_str(res, "", "mem.total"),
100 vici_find_str(res, "", "mem.allocs"));
101 }
102 if (vici_find_str(res, NULL, "mallinfo.sbrk"))
103 {
104 printf("mallinfo: sbrk %s, mmap %s, used %s, free %s\n",
105 vici_find_str(res, "", "mallinfo.sbrk"),
106 vici_find_str(res, "", "mallinfo.mmap"),
107 vici_find_str(res, "", "mallinfo.used"),
108 vici_find_str(res, "", "mallinfo.free"));
109 }
110 printf("loaded plugins:");
111 vici_parse_cb(res, NULL, NULL, list, NULL);
112 printf("\n");
113 }
114 vici_free_res(res);
115 return 0;
116 }
117
118 /**
119 * Register the command.
120 */
121 static void __attribute__ ((constructor))reg()
122 {
123 command_register((command_t) {
124 stats, 'S', "stats", "show daemon stats information",
125 {"[--raw|--pretty]"},
126 {
127 {"help", 'h', 0, "show usage information"},
128 {"raw", 'r', 0, "dump raw response message"},
129 {"pretty", 'P', 0, "dump raw response message in pretty print"},
130 }
131 });
132 }