blob: a65151577c79007f64a466fb3f307b749a4b7920 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file utils.c
2 * Utility routines for printing common objects in the Osmocom world. */
3/*
4 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Weltee08da972017-11-13 01:00:26 +09005 * (C) 2013,2015 by sysmocom - s.f.m.c. GmbH
Harald Welte3fb0b6f2010-05-19 19:02:52 +02006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Harald Welte3fb0b6f2010-05-19 19:02:52 +020011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
Harald Welte3fb0b6f2010-05-19 19:02:52 +020021 */
22
23#include <stdint.h>
Harald Welte3b007f82022-04-18 11:39:08 +020024#include <stdbool.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020025#include <inttypes.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020026#include <string.h>
Holger Hans Peter Freytherb321b932012-09-11 10:39:29 +020027#include <ctype.h>
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +010028#include <limits.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020029
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/linuxlist.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/timer.h>
33#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020034#include <osmocom/core/stat_item.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020035#include <osmocom/core/utils.h>
Harald Welte216338c2017-10-15 19:46:19 +020036#include <osmocom/core/counter.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020037
38#include <osmocom/vty/vty.h>
39
Harald Welte7acb30c2011-08-17 17:13:48 +020040/*! \addtogroup rate_ctr
41 * @{
42 */
43
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020044struct vty_out_context {
45 struct vty *vty;
46 const char *prefix;
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +010047 int max_level;
Harald Welte3b007f82022-04-18 11:39:08 +020048 bool skip_zero;
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020049};
50
51static int rate_ctr_handler(
52 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
53 const struct rate_ctr_desc *desc, void *vctx_)
54{
55 struct vty_out_context *vctx = vctx_;
56 struct vty *vty = vctx->vty;
57
Harald Welte3b007f82022-04-18 11:39:08 +020058 if (vctx->skip_zero && ctr->current == 0)
59 return 0;
60
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020061 vty_out(vty, " %s%s: %8" PRIu64 " "
62 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
63 vctx->prefix, desc->description, ctr->current,
64 ctr->intv[RATE_CTR_INTV_SEC].rate,
65 ctr->intv[RATE_CTR_INTV_MIN].rate,
66 ctr->intv[RATE_CTR_INTV_HOUR].rate,
67 ctr->intv[RATE_CTR_INTV_DAY].rate,
68 VTY_NEWLINE);
69
70 return 0;
71}
72
Neels Hofmeyr87e45502017-06-20 00:17:59 +020073/*! print a rate counter group to given VTY
Harald Welte7acb30c2011-08-17 17:13:48 +020074 * \param[in] vty The VTY to which it should be printed
75 * \param[in] prefix Any additional log prefix ahead of each line
76 * \param[in] ctrg Rate counter group to be printed
Harald Welte3b007f82022-04-18 11:39:08 +020077 * \param[in] skip_zero Skip all zero-valued counters
Harald Welte7acb30c2011-08-17 17:13:48 +020078 */
Harald Welte3b007f82022-04-18 11:39:08 +020079void vty_out_rate_ctr_group2(struct vty *vty, const char *prefix,
80 struct rate_ctr_group *ctrg, bool skip_zero)
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081{
Harald Welte3b007f82022-04-18 11:39:08 +020082 struct vty_out_context vctx = {vty, prefix, 0, skip_zero};
Harald Welte3fb0b6f2010-05-19 19:02:52 +020083
84 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020085
86 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx);
87}
88
Harald Welte3b007f82022-04-18 11:39:08 +020089void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
90 struct rate_ctr_group *ctrg)
91{
92 vty_out_rate_ctr_group2(vty, prefix, ctrg, false);
93}
94
Stefan Sperling97d3da22018-05-24 16:42:43 +020095static char *
96pad_append_str(char *s, const char *a, int minwidth)
97{
98 s = talloc_asprintf_append(s, "%*s", minwidth, a);
99 OSMO_ASSERT(s);
100 return s;
101}
102
103static char *
Stefan Sperling4df22512018-05-25 13:05:58 +0200104pad_append_ctr(char *s, uint64_t ctr, int minwidth)
Stefan Sperling97d3da22018-05-24 16:42:43 +0200105{
106 s = talloc_asprintf_append(s, "%*" PRIu64, minwidth, ctr);
107 OSMO_ASSERT(s);
108 return s;
109}
110
111static int rate_ctr_handler_fmt(
112 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
113 const struct rate_ctr_desc *desc, void *vctx_)
114{
115 struct vty_out_context *vctx = vctx_;
116 struct vty *vty = vctx->vty;
117 const char *fmt = vctx->prefix;
Harald Welte3b007f82022-04-18 11:39:08 +0200118 char *s;
119
120 if (vctx->skip_zero && ctr->current == 0)
121 return 0;
122
123 s = talloc_strdup(vty, "");
Stefan Sperling97d3da22018-05-24 16:42:43 +0200124 OSMO_ASSERT(s);
125
126 while (*fmt) {
127 int ch, minwidth = 0, sign = 1;
128 char *p = strchr(fmt, '%');
129
130 if (p == NULL) {
131 /* No further % directives in format string. Copy rest verbatim and exit. */
132 s = talloc_strdup_append_buffer(s, fmt);
133 OSMO_ASSERT(s);
134 break;
135 } else {
136 ptrdiff_t len;
137
138 OSMO_ASSERT(p >= fmt);
139 len = p - fmt;
140 if (len) {
141 /* Copy bytes verbatim until next '%' byte. */
142 s = talloc_strndup_append_buffer(s, fmt, len);
143 OSMO_ASSERT(s);
144 }
145 fmt = (const char *)(p + 1); /* skip past '%' */
146 if (*fmt == '\0')
147 break;
148 }
149
150 ch = *fmt++;
151 if (ch == '-' && isdigit(*fmt)) {
152 sign = -1;
153 ch = *fmt++;
154 }
155 while (isdigit(ch) && *fmt != '\0') {
156 minwidth *= 10;
157 minwidth += (ch - '0');
158 ch = *fmt++;
159 }
160 minwidth *= sign;
161
162 switch (ch) {
163 case '%':
164 s = talloc_asprintf_append(s, "%c", ch);
165 OSMO_ASSERT(s);
166 break;
167 case 'd':
168 s = pad_append_str(s, desc->description, minwidth);
169 break;
170 case 'n':
171 s = pad_append_str(s, desc->name, minwidth);
172 break;
173 case 'c':
Stefan Sperling4df22512018-05-25 13:05:58 +0200174 s = pad_append_ctr(s, ctr->current, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200175 break;
176 case 'p':
Stefan Sperling4df22512018-05-25 13:05:58 +0200177 s = pad_append_ctr(s, ctr->previous, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200178 break;
179 case 'S':
Stefan Sperling4df22512018-05-25 13:05:58 +0200180 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_SEC].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200181 break;
182 case 'M':
Stefan Sperling4df22512018-05-25 13:05:58 +0200183 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_MIN].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200184 break;
185 case 'H':
Stefan Sperling4df22512018-05-25 13:05:58 +0200186 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_HOUR].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200187 break;
188 case 'D':
Stefan Sperling4df22512018-05-25 13:05:58 +0200189 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_DAY].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200190 break;
191 default:
192 break;
193 }
194 }
195
196 vty_out(vty, "%s%s", s, VTY_NEWLINE);
197 talloc_free(s);
198
199 return 0;
200}
201
202/*! print a rate counter group to given VTY, formatting the line for each counter according to a format string.
203 *
204 * The following format string directives are supported:
205 * - %d: The description of the counter
206 * - %n: The name of the counter
207 * - %c: The current value of the counter
208 * - %p: The previous value of the counter
209 * - %S: The interval of the counter in seconds
210 * - %M: The interval of the counter in minutes
211 * - %H: The interval of the counter in hours
212 * - %D: The interval of the counter in days
213 * - %%: Print a literal %.
214 *
215 * An optional number between % and the letter in a format directive may be used to set a minimum field width.
216 * If the expanded format directive is smaller than this width (according to strlen()) the string will be
217 * left-padded (if the number is positive) or right-padded (if the number is negative) with spaces.
218 * For example, "%25n" prints the counter name left-padded up to a minimum width of 25 columns.
219 *
220 * VTY_NEWLINE will be appended to the format string when it is printed.
221 *
222 * \param[in] vty The VTY to which it should be printed
223 * \param[in] ctrg Rate counter group to be printed
224 * \param[in] fmt A format which may contain the above directives.
Harald Welte3b007f82022-04-18 11:39:08 +0200225 * \param[in] skip_zero Skip all zero-valued counters
Stefan Sperling97d3da22018-05-24 16:42:43 +0200226 */
Harald Welte3b007f82022-04-18 11:39:08 +0200227void vty_out_rate_ctr_group_fmt2(struct vty *vty, const char *fmt,
228 struct rate_ctr_group *ctrg, bool skip_zero)
Stefan Sperling97d3da22018-05-24 16:42:43 +0200229{
Harald Welte3b007f82022-04-18 11:39:08 +0200230 struct vty_out_context vctx = {vty, fmt, 0, skip_zero};
Stefan Sperling97d3da22018-05-24 16:42:43 +0200231 rate_ctr_for_each_counter(ctrg, rate_ctr_handler_fmt, &vctx);
232}
233
Harald Welte3b007f82022-04-18 11:39:08 +0200234void vty_out_rate_ctr_group_fmt(struct vty *vty, const char *fmt,
235 struct rate_ctr_group *ctrg)
236{
237 vty_out_rate_ctr_group_fmt2(vty, fmt, ctrg, false);
238}
Harald Welte96e2a002017-06-12 21:44:18 +0200239static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
240{
241 struct vty_out_context *vctx = vctx_;
242 struct vty *vty = vctx->vty;
243
244 if (ctrg->desc->class_id > vctx->max_level)
245 return 0;
246
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200247 vty_out(vty, "%s%s (%d)", vctx->prefix, ctrg->desc->group_description, ctrg->idx);
248 if (ctrg->name)
249 vty_out(vty, "('%s')", ctrg->name);
250 vty_out(vty, ":%s", VTY_NEWLINE);
Harald Welte96e2a002017-06-12 21:44:18 +0200251
252 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
253
254 return 0;
255}
256
257/*! @} */
258
259
260/*! \addtogroup stats
261 * @{
262 */
263
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100264static int osmo_stat_item_handler(
265 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200266{
267 struct vty_out_context *vctx = vctx_;
268 struct vty *vty = vctx->vty;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200269 const struct osmo_stat_item_desc *desc = osmo_stat_item_get_desc(item);
Harald Welte3b007f82022-04-18 11:39:08 +0200270 int32_t value = osmo_stat_item_get_last(item);
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200271 const char *unit = (desc->unit != OSMO_STAT_ITEM_NO_UNIT) ? desc->unit : "";
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200272
Harald Welte3b007f82022-04-18 11:39:08 +0200273 if (vctx->skip_zero && value == 0)
274 return 0;
275
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200276 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
Harald Welte3b007f82022-04-18 11:39:08 +0200277 vctx->prefix, desc->description, value, unit, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200278
279 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200280}
Harald Welte7acb30c2011-08-17 17:13:48 +0200281
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200282/*! print a stat item group to given VTY
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200283 * \param[in] vty The VTY to which it should be printed
284 * \param[in] prefix Any additional log prefix ahead of each line
285 * \param[in] statg Stat item group to be printed
Harald Welte3b007f82022-04-18 11:39:08 +0200286 * \param[in] skip_zero Skip all zero-valued counters
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200287 */
Harald Welte3b007f82022-04-18 11:39:08 +0200288void vty_out_stat_item_group2(struct vty *vty, const char *prefix,
289 struct osmo_stat_item_group *statg, bool skip_zero)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200290{
Harald Welte3b007f82022-04-18 11:39:08 +0200291 struct vty_out_context vctx = {vty, prefix, 0, skip_zero};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200292
293 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
294 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100295 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200296}
297
Harald Welte3b007f82022-04-18 11:39:08 +0200298void vty_out_stat_item_group(struct vty *vty, const char *prefix,
299 struct osmo_stat_item_group *statg)
300{
301 return vty_out_stat_item_group2(vty, prefix, statg, false);
302}
303
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100304static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200305{
306 struct vty_out_context *vctx = vctx_;
307 struct vty *vty = vctx->vty;
308
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100309 if (statg->desc->class_id > vctx->max_level)
310 return 0;
311
Pau Espin Pedrol09f075f2021-05-31 13:10:24 +0200312 vty_out(vty, "%s%s (%d)", vctx->prefix, statg->desc->group_description, statg->idx);
313 if (statg->name)
314 vty_out(vty, "('%s')", statg->name);
315 vty_out(vty, ":%s", VTY_NEWLINE);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200316
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100317 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200318
319 return 0;
320}
321
Harald Welte96e2a002017-06-12 21:44:18 +0200322/*! @} */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200323
Harald Welte96e2a002017-06-12 21:44:18 +0200324/*! \addtogroup vty
325 * @{
326 */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200327
328static int handle_counter(struct osmo_counter *counter, void *vctx_)
329{
330 struct vty_out_context *vctx = vctx_;
331 struct vty *vty = vctx->vty;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200332 const char *description = counter->description;
Harald Welte3b007f82022-04-18 11:39:08 +0200333 unsigned long value = osmo_counter_get(counter);
334
335 if (vctx->skip_zero && value == 0)
336 return 0;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200337
338 if (!counter->description)
339 description = counter->name;
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200340
Harald Welte3b007f82022-04-18 11:39:08 +0200341 vty_out(vty, " %s%s: %8lu%s", vctx->prefix, description, value, VTY_NEWLINE);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200342
343 return 0;
344}
345
Harald Welte3b007f82022-04-18 11:39:08 +0200346void vty_out_statistics_partial2(struct vty *vty, const char *prefix, int max_level, bool skip_zero)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200347{
Harald Welte3b007f82022-04-18 11:39:08 +0200348 struct vty_out_context vctx = {vty, prefix, max_level, skip_zero};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200349
350 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
351 osmo_counters_for_each(handle_counter, &vctx);
352 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100353 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200354}
355
Harald Welte3b007f82022-04-18 11:39:08 +0200356void vty_out_statistics_partial(struct vty *vty, const char *prefix, int max_level)
357{
358 return vty_out_statistics_partial2(vty, prefix, max_level, false);
359}
360
361void vty_out_statistics_full2(struct vty *vty, const char *prefix, bool skip_zero)
362{
363 vty_out_statistics_partial2(vty, prefix, INT_MAX, skip_zero);
364}
365
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100366void vty_out_statistics_full(struct vty *vty, const char *prefix)
367{
Harald Welte3b007f82022-04-18 11:39:08 +0200368 vty_out_statistics_full2(vty, prefix, false);
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100369}
370
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200371/*! Generate a VTY command string from value_string */
Harald Weltefab0ae92012-08-17 12:17:38 +0200372char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
373 const char *prefix, const char *sep,
374 const char *end, int do_lower)
375{
376 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200377 int size = strlen(prefix) + strlen(end);
378 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200379 const struct value_string *vs;
380 char *str;
381
382 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200383 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200384
385 rem = size;
386 str = talloc_zero_size(ctx, size);
387 if (!str)
388 return NULL;
389
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200390 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200391 if (ret < 0)
392 goto err;
393 OSMO_SNPRINTF_RET(ret, rem, offset, len);
394
395 for (vs = vals; vs->value || vs->str; vs++) {
396 if (vs->str) {
397 int j, name_len = strlen(vs->str)+1;
398 char name[name_len];
399
400 for (j = 0; j < name_len; j++)
401 name[j] = do_lower ?
402 tolower(vs->str[j]) : vs->str[j];
403
404 name[name_len-1] = '\0';
405 ret = snprintf(str + offset, rem, "%s%s", name, sep);
406 if (ret < 0)
407 goto err;
408 OSMO_SNPRINTF_RET(ret, rem, offset, len);
409 }
410 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200411 offset -= sep_len; /* to remove the trailing sep */
412 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200413
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200414 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200415 if (ret < 0)
416 goto err;
417 OSMO_SNPRINTF_RET(ret, rem, offset, len);
418err:
419 str[size-1] = '\0';
420 return str;
421}
422
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200423/*! @} */