blob: af69289f2293e3475919d900abe4df832ad270ab [file] [log] [blame]
Jacob Erlbeck45513e62015-10-19 15:14:13 +02001/* OpenBSC stats helper for the VTY */
2/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2009-2014 by Holger Hans Peter Freyther
4 * (C) 2015 by Sysmocom s.f.m.c. GmbH
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdlib.h>
24#include <string.h>
25
26#include "../../config.h"
27
28#include <osmocom/vty/command.h>
29#include <osmocom/vty/buffer.h>
30#include <osmocom/vty/vty.h>
31#include <osmocom/vty/telnet_interface.h>
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020032#include <osmocom/vty/telnet_interface.h>
Jacob Erlbeck45513e62015-10-19 15:14:13 +020033#include <osmocom/vty/misc.h>
34
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020035#include <osmocom/core/stats.h>
Alexander Couzensad580ba2016-05-16 16:01:45 +020036#include <osmocom/core/statistics.h>
37#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020038
Jacob Erlbeck45513e62015-10-19 15:14:13 +020039#define CFG_STATS_STR "Configure stats sub-system\n"
40#define CFG_REPORTER_STR "Configure a stats reporter\n"
41
42#define SHOW_STATS_STR "Show statistical values\n"
43
Harald Welte96e2a002017-06-12 21:44:18 +020044/*! \addtogroup stats
45 * @{
46 * \brief VTY interface for statsd / statistic items
47 */
48
Alexander Couzense052dc22016-10-04 18:04:37 +020049/* containing version info */
50extern struct host host;
51
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020052struct cmd_node cfg_stats_node = {
53 CFG_STATS_NODE,
54 "%s(config-stats)# ",
55 1
56};
57
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010058static const struct value_string stats_class_strs[] = {
59 { OSMO_STATS_CLASS_GLOBAL, "global" },
60 { OSMO_STATS_CLASS_PEER, "peer" },
61 { OSMO_STATS_CLASS_SUBSCRIBER, "subscriber" },
62 { 0, NULL }
63};
64
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010065static struct osmo_stats_reporter *osmo_stats_vty2srep(struct vty *vty)
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020066{
67 if (vty->node == CFG_STATS_NODE)
68 return vty->index;
69
70 return NULL;
71}
72
73static int set_srep_parameter_str(struct vty *vty,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010074 int (*fun)(struct osmo_stats_reporter *, const char *),
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020075 const char *val, const char *param_name)
76{
77 int rc;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010078 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020079 OSMO_ASSERT(srep);
80
81 rc = fun(srep, val);
82 if (rc < 0) {
83 vty_out(vty, "%% Unable to set %s: %s%s",
84 param_name, strerror(-rc), VTY_NEWLINE);
85 return CMD_WARNING;
86 }
87
88 return CMD_SUCCESS;
89}
90
91static int set_srep_parameter_int(struct vty *vty,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010092 int (*fun)(struct osmo_stats_reporter *, int),
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020093 const char *val, const char *param_name)
94{
95 int rc;
96 int int_val;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010097 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020098 OSMO_ASSERT(srep);
99
100 int_val = atoi(val);
101
102 rc = fun(srep, int_val);
103 if (rc < 0) {
104 vty_out(vty, "%% Unable to set %s: %s%s",
105 param_name, strerror(-rc), VTY_NEWLINE);
106 return CMD_WARNING;
107 }
108
109 return CMD_SUCCESS;
110}
111
112DEFUN(cfg_stats_reporter_local_ip, cfg_stats_reporter_local_ip_cmd,
113 "local-ip ADDR",
114 "Set the IP address to which we bind locally\n"
115 "IP Address\n")
116{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117 return set_srep_parameter_str(vty, osmo_stats_reporter_set_local_addr,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200118 argv[0], "local address");
119}
120
121DEFUN(cfg_no_stats_reporter_local_ip, cfg_no_stats_reporter_local_ip_cmd,
122 "no local-ip",
123 NO_STR
124 "Set the IP address to which we bind locally\n")
125{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100126 return set_srep_parameter_str(vty, osmo_stats_reporter_set_local_addr,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200127 NULL, "local address");
128}
129
130DEFUN(cfg_stats_reporter_remote_ip, cfg_stats_reporter_remote_ip_cmd,
131 "remote-ip ADDR",
132 "Set the remote IP address to which we connect\n"
133 "IP Address\n")
134{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100135 return set_srep_parameter_str(vty, osmo_stats_reporter_set_remote_addr,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200136 argv[0], "remote address");
137}
138
139DEFUN(cfg_stats_reporter_remote_port, cfg_stats_reporter_remote_port_cmd,
140 "remote-port <1-65535>",
141 "Set the remote port to which we connect\n"
142 "Remote port number\n")
143{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100144 return set_srep_parameter_int(vty, osmo_stats_reporter_set_remote_port,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200145 argv[0], "remote port");
146}
147
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100148DEFUN(cfg_stats_reporter_mtu, cfg_stats_reporter_mtu_cmd,
149 "mtu <100-65535>",
150 "Set the maximum packet size\n"
151 "Size in byte\n")
152{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100153 return set_srep_parameter_int(vty, osmo_stats_reporter_set_mtu,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100154 argv[0], "mtu");
155}
156
157DEFUN(cfg_no_stats_reporter_mtu, cfg_no_stats_reporter_mtu_cmd,
158 "no mtu",
159 NO_STR "Set the maximum packet size\n")
160{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100161 return set_srep_parameter_int(vty, osmo_stats_reporter_set_mtu,
Holger Hans Peter Freyther8f0374f2015-11-02 15:53:09 +0100162 "0", "mtu");
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100163}
164
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200165DEFUN(cfg_stats_reporter_prefix, cfg_stats_reporter_prefix_cmd,
166 "prefix PREFIX",
167 "Set the item name prefix\n"
168 "The prefix string\n")
169{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100170 return set_srep_parameter_str(vty, osmo_stats_reporter_set_name_prefix,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200171 argv[0], "prefix string");
172}
173
174DEFUN(cfg_no_stats_reporter_prefix, cfg_no_stats_reporter_prefix_cmd,
175 "no prefix",
176 NO_STR
177 "Set the item name prefix\n")
178{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100179 return set_srep_parameter_str(vty, osmo_stats_reporter_set_name_prefix,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200180 "", "prefix string");
181}
182
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100183DEFUN(cfg_stats_reporter_level, cfg_stats_reporter_level_cmd,
184 "level (global|peer|subscriber)",
185 "Set the maximum group level\n"
186 "Report global groups only\n"
187 "Report global and network peer related groups\n"
188 "Report global, peer, and subscriber groups\n")
189{
190 int level = get_string_value(stats_class_strs, argv[0]);
191 int rc;
192 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
193
194 OSMO_ASSERT(srep);
195 rc = osmo_stats_reporter_set_max_class(srep, level);
196 if (rc < 0) {
197 vty_out(vty, "%% Unable to set level: %s%s",
198 strerror(-rc), VTY_NEWLINE);
199 return CMD_WARNING;
200 }
201
202 return 0;
203}
204
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200205DEFUN(cfg_stats_reporter_enable, cfg_stats_reporter_enable_cmd,
206 "enable",
207 "Enable the reporter\n")
208{
209 int rc;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100210 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200211 OSMO_ASSERT(srep);
212
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100213 rc = osmo_stats_reporter_enable(srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200214 if (rc < 0) {
215 vty_out(vty, "%% Unable to enable the reporter: %s%s",
216 strerror(-rc), VTY_NEWLINE);
217 return CMD_WARNING;
218 }
219
220 return CMD_SUCCESS;
221}
222
223DEFUN(cfg_stats_reporter_disable, cfg_stats_reporter_disable_cmd,
224 "disable",
225 "Disable the reporter\n")
226{
227 int rc;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100228 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200229 OSMO_ASSERT(srep);
230
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100231 rc = osmo_stats_reporter_disable(srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200232 if (rc < 0) {
233 vty_out(vty, "%% Unable to disable the reporter: %s%s",
234 strerror(-rc), VTY_NEWLINE);
235 return CMD_WARNING;
236 }
237
238 return CMD_SUCCESS;
239}
240
241DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd,
242 "stats reporter statsd",
243 CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
244{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100245 struct osmo_stats_reporter *srep;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200246
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100247 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200248 if (!srep) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100249 srep = osmo_stats_reporter_create_statsd(NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200250 if (!srep) {
251 vty_out(vty, "%% Unable to create statsd reporter%s",
252 VTY_NEWLINE);
253 return CMD_WARNING;
254 }
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100255 srep->max_class = OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100256 /* TODO: if needed, add osmo_stats_add_reporter(srep); */
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200257 }
258
259 vty->index = srep;
260 vty->node = CFG_STATS_NODE;
261
262 return CMD_SUCCESS;
263}
264
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100265DEFUN(cfg_stats_interval, cfg_stats_interval_cmd,
266 "stats interval <1-65535>",
267 CFG_STATS_STR "Set the reporting interval\n"
268 "Interval in seconds\n")
269{
270 int rc;
271 int interval = atoi(argv[0]);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100272 rc = osmo_stats_set_interval(interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100273 if (rc < 0) {
274 vty_out(vty, "%% Unable to set interval: %s%s",
275 strerror(-rc), VTY_NEWLINE);
276 return CMD_WARNING;
277 }
278
279 return CMD_SUCCESS;
280}
281
282
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200283DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd,
284 "no stats reporter statsd",
285 NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
286{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100287 struct osmo_stats_reporter *srep;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200288
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100289 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200290 if (!srep) {
291 vty_out(vty, "%% No statsd logging active%s",
292 VTY_NEWLINE);
293 return CMD_WARNING;
294 }
295
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100296 osmo_stats_reporter_free(srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200297
298 return CMD_SUCCESS;
299}
300
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100301DEFUN(cfg_stats_reporter_log, cfg_stats_reporter_log_cmd,
302 "stats reporter log",
303 CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n")
304{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100305 struct osmo_stats_reporter *srep;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100306
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100307 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100308 if (!srep) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100309 srep = osmo_stats_reporter_create_log(NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100310 if (!srep) {
311 vty_out(vty, "%% Unable to create log reporter%s",
312 VTY_NEWLINE);
313 return CMD_WARNING;
314 }
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100315 srep->max_class = OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100316 /* TODO: if needed, add osmo_stats_add_reporter(srep); */
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100317 }
318
319 vty->index = srep;
320 vty->node = CFG_STATS_NODE;
321
322 return CMD_SUCCESS;
323}
324
325DEFUN(cfg_no_stats_reporter_log, cfg_no_stats_reporter_log_cmd,
326 "no stats reporter log",
327 NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n")
328{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100329 struct osmo_stats_reporter *srep;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100330
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100331 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100332 if (!srep) {
333 vty_out(vty, "%% No log reporting active%s",
334 VTY_NEWLINE);
335 return CMD_WARNING;
336 }
337
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100338 osmo_stats_reporter_free(srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100339
340 return CMD_SUCCESS;
341}
342
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200343DEFUN(show_stats,
344 show_stats_cmd,
345 "show stats",
346 SHOW_STR SHOW_STATS_STR)
347{
348 vty_out_statistics_full(vty, "");
349
350 return CMD_SUCCESS;
351}
352
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100353DEFUN(show_stats_level,
354 show_stats_level_cmd,
355 "show stats level (global|peer|subscriber)",
356 SHOW_STR SHOW_STATS_STR
Holger Hans Peter Freyther83481942015-11-07 21:10:01 +0100357 "Set the maximum group level\n"
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100358 "Show global groups only\n"
359 "Show global and network peer related groups\n"
360 "Show global, peer, and subscriber groups\n")
361{
362 int level = get_string_value(stats_class_strs, argv[0]);
363 vty_out_statistics_partial(vty, "", level);
364
365 return CMD_SUCCESS;
366}
367
Alexander Couzensad580ba2016-05-16 16:01:45 +0200368static int asciidoc_handle_counter(struct osmo_counter *counter, void *sctx_)
369{
370 struct vty *vty = sctx_;
371 char *name = osmo_asciidoc_escape(counter->name);
372 char *description = osmo_asciidoc_escape(counter->description);
373
374 /* | name | This document & | description | */
375 vty_out(vty, "| %s | <<ungroup_counter_%s>> | %s%s",
376 name,
377 name,
378 description ? description : "",
379 VTY_NEWLINE);
380
381 talloc_free(name);
382 talloc_free(description);
383
384 return 0;
385}
386
387static void asciidoc_counter_generate(struct vty *vty)
388{
389 vty_out(vty, "// ungrouped osmo_counters%s", VTY_NEWLINE);
390 vty_out(vty, ".ungrouped osmo counters%s", VTY_NEWLINE);
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200391 vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200392 vty_out(vty, "|===%s", VTY_NEWLINE);
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200393 vty_out(vty, "| Name | Reference | Description%s", VTY_NEWLINE);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200394 osmo_counters_for_each(asciidoc_handle_counter, vty);
395 vty_out(vty, "|===%s", VTY_NEWLINE);
396}
397
398static int asciidoc_rate_ctr_handler(
399 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
400 const struct rate_ctr_desc *desc, void *sctx_)
401{
402 struct vty *vty = sctx_;
403 char *name = osmo_asciidoc_escape(desc->name);
404 char *description = osmo_asciidoc_escape(desc->description);
405 char *group_name_prefix = osmo_asciidoc_escape(ctrg->desc->group_name_prefix);
406
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200407 /* | Name | This document & | Description | */
Alexander Couzensad580ba2016-05-16 16:01:45 +0200408 vty_out(vty, "| %s | <<%s_%s>> | %s%s",
409 name,
410 group_name_prefix,
411 name,
412 description ? description : NULL,
413 VTY_NEWLINE);
414
415 /* description seems to be optional */
416 talloc_free(name);
417 talloc_free(group_name_prefix);
418 talloc_free(description);
419
420 return 0;
421}
422
423static int asciidoc_rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_)
424{
425 struct vty *vty = sctx_;
426
427 char *group_description = osmo_asciidoc_escape(ctrg->desc->group_description);
428 char *group_name_prefix = osmo_asciidoc_escape(ctrg->desc->group_name_prefix);
429
430 vty_out(vty, "// rate_ctr_group table %s%s", group_description, VTY_NEWLINE);
431 vty_out(vty, ".%s - %s %s", group_name_prefix, group_description, VTY_NEWLINE);
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200432 vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200433 vty_out(vty, "|===%s", VTY_NEWLINE);
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200434 vty_out(vty, "| Name | Reference | Description%s", VTY_NEWLINE);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200435 rate_ctr_for_each_counter(ctrg, asciidoc_rate_ctr_handler, sctx_);
436 vty_out(vty, "|===%s", VTY_NEWLINE);
437
438 talloc_free(group_name_prefix);
439 talloc_free(group_description);
440
441 return 0;
442}
443
444static int asciidoc_osmo_stat_item_handler(
445 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
446{
447 struct vty *vty = sctx_;
448
449 char *name = osmo_asciidoc_escape(item->desc->name);
450 char *description = osmo_asciidoc_escape(item->desc->description);
451 char *group_name_prefix = osmo_asciidoc_escape(statg->desc->group_name_prefix);
452 char *unit = osmo_asciidoc_escape(item->desc->unit);
453
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200454 /* | Name | Reference | Description | Unit | */
Alexander Couzensad580ba2016-05-16 16:01:45 +0200455 vty_out(vty, "| %s | <<%s_%s>> | %s | %s%s",
456 name,
457 group_name_prefix,
458 name,
459 description ? description : "",
460 unit ? unit : "",
461 VTY_NEWLINE);
462
463 talloc_free(name);
464 talloc_free(group_name_prefix);
465 talloc_free(description);
466 talloc_free(unit);
467
468 return 0;
469}
470
471static int asciidoc_osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
472{
473 char *group_name_prefix = osmo_asciidoc_escape(statg->desc->group_name_prefix);
474 char *group_description = osmo_asciidoc_escape(statg->desc->group_description);
475
476 struct vty *vty = sctx_;
477 vty_out(vty, "%s%s", group_description ? group_description : "" , VTY_NEWLINE);
478
479 vty_out(vty, "// osmo_stat_item_group table %s%s", group_description ? group_description : "", VTY_NEWLINE);
480 vty_out(vty, ".%s - %s %s", group_name_prefix, group_description ? group_description : "", VTY_NEWLINE);
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200481 vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200482 vty_out(vty, "|===%s", VTY_NEWLINE);
Alexander Couzenseb604cf2016-10-14 13:54:47 +0200483 vty_out(vty, "| Name | Reference | Description | Unit%s", VTY_NEWLINE);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200484 osmo_stat_item_for_each_item(statg, asciidoc_osmo_stat_item_handler, sctx_);
485 vty_out(vty, "|===%s", VTY_NEWLINE);
486
487 talloc_free(group_name_prefix);
488 talloc_free(group_description);
489
490 return 0;
491}
492
493DEFUN(show_stats_asciidoc_table,
494 show_stats_asciidoc_table_cmd,
495 "show asciidoc counters",
Harald Weltef624c332016-06-15 11:35:16 +0200496 SHOW_STR "Asciidoc generation\n" "Generate table of all registered counters\n")
Alexander Couzensad580ba2016-05-16 16:01:45 +0200497{
Alexander Couzense052dc22016-10-04 18:04:37 +0200498 vty_out(vty, "// autogenerated by show asciidoc counters%s", VTY_NEWLINE);
499 vty_out(vty, "These counters and their description based on %s %s (%s).%s%s",
500 host.app_info->name,
501 host.app_info->version,
502 host.app_info->name ? host.app_info->name : "", VTY_NEWLINE, VTY_NEWLINE);
503 /* 2x VTY_NEWLINE are intentional otherwise it would interpret the first table header
504 * as usual text*/
Alexander Couzensad580ba2016-05-16 16:01:45 +0200505 vty_out(vty, "// generating tables for rate_ctr_group%s", VTY_NEWLINE);
506 rate_ctr_for_each_group(asciidoc_rate_ctr_group_handler, vty);
507
508 vty_out(vty, "// generating tables for osmo_stat_items%s", VTY_NEWLINE);
509 osmo_stat_item_for_each_group(asciidoc_osmo_stat_item_group_handler, vty);
510
511 vty_out(vty, "// generating tables for osmo_counters%s", VTY_NEWLINE);
512 asciidoc_counter_generate(vty);
513 return CMD_SUCCESS;
514}
515
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100516static int config_write_stats_reporter(struct vty *vty, struct osmo_stats_reporter *srep)
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200517{
518 if (srep == NULL)
519 return 0;
520
521 switch (srep->type) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100522 case OSMO_STATS_REPORTER_STATSD:
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200523 vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE);
524 break;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100525 case OSMO_STATS_REPORTER_LOG:
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100526 vty_out(vty, "stats reporter log%s", VTY_NEWLINE);
527 break;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200528 }
529
530 vty_out(vty, " disable%s", VTY_NEWLINE);
531
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100532 if (srep->have_net_config) {
533 if (srep->dest_addr_str)
534 vty_out(vty, " remote-ip %s%s",
535 srep->dest_addr_str, VTY_NEWLINE);
536 if (srep->dest_port)
537 vty_out(vty, " remote-port %d%s",
538 srep->dest_port, VTY_NEWLINE);
539 if (srep->bind_addr_str)
540 vty_out(vty, " local-ip %s%s",
541 srep->bind_addr_str, VTY_NEWLINE);
542 if (srep->mtu)
543 vty_out(vty, " mtu %d%s",
544 srep->mtu, VTY_NEWLINE);
545 }
546
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100547 if (srep->max_class)
548 vty_out(vty, " level %s%s",
549 get_value_string(stats_class_strs, srep->max_class),
550 VTY_NEWLINE);
551
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200552 if (srep->name_prefix && *srep->name_prefix)
553 vty_out(vty, " prefix %s%s",
554 srep->name_prefix, VTY_NEWLINE);
555 else
556 vty_out(vty, " no prefix%s", VTY_NEWLINE);
557
558 if (srep->enabled)
559 vty_out(vty, " enable%s", VTY_NEWLINE);
560
561 return 1;
562}
563
564static int config_write_stats(struct vty *vty)
565{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100566 struct osmo_stats_reporter *srep;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200567
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100568 /* TODO: loop through all reporters */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100569 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200570 config_write_stats_reporter(vty, srep);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100571 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100572 config_write_stats_reporter(vty, srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200573
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100574 vty_out(vty, "stats interval %d%s", osmo_stats_config->interval, VTY_NEWLINE);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100575
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200576 return 1;
577}
578
Harald Welte96e2a002017-06-12 21:44:18 +0200579/*! \brief Add stats related commands to the VTY
580 * Call this once during your application initialization if you would
581 * like to have stats VTY commands enabled.
582 */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100583void osmo_stats_vty_add_cmds()
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200584{
585 install_element_ve(&show_stats_cmd);
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100586 install_element_ve(&show_stats_level_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200587
588 install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd);
589 install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100590 install_element(CONFIG_NODE, &cfg_stats_reporter_log_cmd);
591 install_element(CONFIG_NODE, &cfg_no_stats_reporter_log_cmd);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100592 install_element(CONFIG_NODE, &cfg_stats_interval_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200593
594 install_node(&cfg_stats_node, config_write_stats);
595 vty_install_default(CFG_STATS_NODE);
596
597 install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd);
598 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd);
599 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd);
600 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd);
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100601 install_element(CFG_STATS_NODE, &cfg_stats_reporter_mtu_cmd);
602 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_mtu_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200603 install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd);
604 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd);
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100605 install_element(CFG_STATS_NODE, &cfg_stats_reporter_level_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200606 install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd);
607 install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd);
Alexander Couzensad580ba2016-05-16 16:01:45 +0200608
609 install_element_ve(&show_stats_asciidoc_table_cmd);
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200610}
Harald Welte96e2a002017-06-12 21:44:18 +0200611
612/*! @} */