swanctl: Add a --stats command to print daemon infos and statistics
[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 <errno.h>
19
20 static int stats(vici_conn_t *conn)
21 {
22 vici_req_t *req;
23 vici_res_t *res;
24 char *arg;
25 command_format_options_t format = COMMAND_FORMAT_NONE;
26
27 while (TRUE)
28 {
29 switch (command_getopt(&arg))
30 {
31 case 'h':
32 return command_usage(NULL);
33 case 'P':
34 format |= COMMAND_FORMAT_PRETTY;
35 /* fall through to raw */
36 case 'r':
37 format |= COMMAND_FORMAT_RAW;
38 continue;
39 case EOF:
40 break;
41 default:
42 return command_usage("invalid --stats option");
43 }
44 break;
45 }
46
47 req = vici_begin("stats");
48 res = vici_submit(req, conn);
49 if (!res)
50 {
51 fprintf(stderr, "stats request failed: %s\n", strerror(errno));
52 return errno;
53 }
54 if (format & COMMAND_FORMAT_RAW)
55 {
56 vici_dump(res, "stats reply", format & COMMAND_FORMAT_PRETTY, stdout);
57 }
58 else
59 {
60 printf("uptime: %s, since %s\n",
61 vici_find_str(res, "", "uptime.running"),
62 vici_find_str(res, "", "uptime.since"));
63
64 printf("worker threads: %s total, %s idle, working: %s/%s/%s/%s\n",
65 vici_find_str(res, "", "workers.total"),
66 vici_find_str(res, "", "workers.idle"),
67 vici_find_str(res, "", "workers.active.critical"),
68 vici_find_str(res, "", "workers.active.high"),
69 vici_find_str(res, "", "workers.active.medium"),
70 vici_find_str(res, "", "workers.active.low"));
71
72 printf("job queues: %s/%s/%s/%s\n",
73 vici_find_str(res, "", "queues.critical"),
74 vici_find_str(res, "", "queues.high"),
75 vici_find_str(res, "", "queues.medium"),
76 vici_find_str(res, "", "queues.low"));
77
78 printf("jobs scheduled: %s\n",
79 vici_find_str(res, "", "scheduled"));
80
81 printf("IKE_SAs: %s total, %s half-open\n",
82 vici_find_str(res, "", "ikesas.total"),
83 vici_find_str(res, "", "ikesas.half-open"));
84
85 if (vici_find_str(res, NULL, "mem.total"))
86 {
87 printf("memory usage: %s bytes, %s allocations\n",
88 vici_find_str(res, "", "mem.total"),
89 vici_find_str(res, "", "mem.allocs"));
90 }
91 if (vici_find_str(res, NULL, "mallinfo.sbrk"))
92 {
93 printf("mallinfo: sbrk %s, mmap %s, used %s, free %s\n",
94 vici_find_str(res, "", "mallinfo.sbrk"),
95 vici_find_str(res, "", "mallinfo.mmap"),
96 vici_find_str(res, "", "mallinfo.used"),
97 vici_find_str(res, "", "mallinfo.free"));
98 }
99 }
100 vici_free_res(res);
101 return 0;
102 }
103
104 /**
105 * Register the command.
106 */
107 static void __attribute__ ((constructor))reg()
108 {
109 command_register((command_t) {
110 stats, 'S', "stats", "show daemon stats information",
111 {"[--raw|--pretty]"},
112 {
113 {"help", 'h', 0, "show usage information"},
114 {"raw", 'r', 0, "dump raw response message"},
115 {"pretty", 'P', 0, "dump raw response message in pretty print"},
116 }
117 });
118 }