blob: 0358d9bd6fc346d7e826d6e95e51267376a2b6f1 [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 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
27#include <stdint.h>
28#include <inttypes.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020029#include <string.h>
Holger Hans Peter Freytherb321b932012-09-11 10:39:29 +020030#include <ctype.h>
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +010031#include <limits.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020032
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/core/linuxlist.h>
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/timer.h>
36#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020037#include <osmocom/core/stat_item.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020038#include <osmocom/core/utils.h>
Harald Welte216338c2017-10-15 19:46:19 +020039#include <osmocom/core/counter.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020040
41#include <osmocom/vty/vty.h>
42
Harald Welte7acb30c2011-08-17 17:13:48 +020043/*! \addtogroup rate_ctr
44 * @{
45 */
46
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020047struct vty_out_context {
48 struct vty *vty;
49 const char *prefix;
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +010050 int max_level;
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020051};
52
53static int rate_ctr_handler(
54 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
55 const struct rate_ctr_desc *desc, void *vctx_)
56{
57 struct vty_out_context *vctx = vctx_;
58 struct vty *vty = vctx->vty;
59
60 vty_out(vty, " %s%s: %8" PRIu64 " "
61 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
62 vctx->prefix, desc->description, ctr->current,
63 ctr->intv[RATE_CTR_INTV_SEC].rate,
64 ctr->intv[RATE_CTR_INTV_MIN].rate,
65 ctr->intv[RATE_CTR_INTV_HOUR].rate,
66 ctr->intv[RATE_CTR_INTV_DAY].rate,
67 VTY_NEWLINE);
68
69 return 0;
70}
71
Neels Hofmeyr87e45502017-06-20 00:17:59 +020072/*! print a rate counter group to given VTY
Harald Welte7acb30c2011-08-17 17:13:48 +020073 * \param[in] vty The VTY to which it should be printed
74 * \param[in] prefix Any additional log prefix ahead of each line
75 * \param[in] ctrg Rate counter group to be printed
76 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020077void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
78 struct rate_ctr_group *ctrg)
79{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020080 struct vty_out_context vctx = {vty, prefix};
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081
82 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020083
84 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx);
85}
86
Stefan Sperling97d3da22018-05-24 16:42:43 +020087static char *
88pad_append_str(char *s, const char *a, int minwidth)
89{
90 s = talloc_asprintf_append(s, "%*s", minwidth, a);
91 OSMO_ASSERT(s);
92 return s;
93}
94
95static char *
Stefan Sperling4df22512018-05-25 13:05:58 +020096pad_append_ctr(char *s, uint64_t ctr, int minwidth)
Stefan Sperling97d3da22018-05-24 16:42:43 +020097{
98 s = talloc_asprintf_append(s, "%*" PRIu64, minwidth, ctr);
99 OSMO_ASSERT(s);
100 return s;
101}
102
103static int rate_ctr_handler_fmt(
104 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
105 const struct rate_ctr_desc *desc, void *vctx_)
106{
107 struct vty_out_context *vctx = vctx_;
108 struct vty *vty = vctx->vty;
109 const char *fmt = vctx->prefix;
110 char *s = talloc_strdup(vty, "");
111 OSMO_ASSERT(s);
112
113 while (*fmt) {
114 int ch, minwidth = 0, sign = 1;
115 char *p = strchr(fmt, '%');
116
117 if (p == NULL) {
118 /* No further % directives in format string. Copy rest verbatim and exit. */
119 s = talloc_strdup_append_buffer(s, fmt);
120 OSMO_ASSERT(s);
121 break;
122 } else {
123 ptrdiff_t len;
124
125 OSMO_ASSERT(p >= fmt);
126 len = p - fmt;
127 if (len) {
128 /* Copy bytes verbatim until next '%' byte. */
129 s = talloc_strndup_append_buffer(s, fmt, len);
130 OSMO_ASSERT(s);
131 }
132 fmt = (const char *)(p + 1); /* skip past '%' */
133 if (*fmt == '\0')
134 break;
135 }
136
137 ch = *fmt++;
138 if (ch == '-' && isdigit(*fmt)) {
139 sign = -1;
140 ch = *fmt++;
141 }
142 while (isdigit(ch) && *fmt != '\0') {
143 minwidth *= 10;
144 minwidth += (ch - '0');
145 ch = *fmt++;
146 }
147 minwidth *= sign;
148
149 switch (ch) {
150 case '%':
151 s = talloc_asprintf_append(s, "%c", ch);
152 OSMO_ASSERT(s);
153 break;
154 case 'd':
155 s = pad_append_str(s, desc->description, minwidth);
156 break;
157 case 'n':
158 s = pad_append_str(s, desc->name, minwidth);
159 break;
160 case 'c':
Stefan Sperling4df22512018-05-25 13:05:58 +0200161 s = pad_append_ctr(s, ctr->current, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200162 break;
163 case 'p':
Stefan Sperling4df22512018-05-25 13:05:58 +0200164 s = pad_append_ctr(s, ctr->previous, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200165 break;
166 case 'S':
Stefan Sperling4df22512018-05-25 13:05:58 +0200167 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_SEC].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200168 break;
169 case 'M':
Stefan Sperling4df22512018-05-25 13:05:58 +0200170 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_MIN].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200171 break;
172 case 'H':
Stefan Sperling4df22512018-05-25 13:05:58 +0200173 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_HOUR].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200174 break;
175 case 'D':
Stefan Sperling4df22512018-05-25 13:05:58 +0200176 s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_DAY].rate, minwidth);
Stefan Sperling97d3da22018-05-24 16:42:43 +0200177 break;
178 default:
179 break;
180 }
181 }
182
183 vty_out(vty, "%s%s", s, VTY_NEWLINE);
184 talloc_free(s);
185
186 return 0;
187}
188
189/*! print a rate counter group to given VTY, formatting the line for each counter according to a format string.
190 *
191 * The following format string directives are supported:
192 * - %d: The description of the counter
193 * - %n: The name of the counter
194 * - %c: The current value of the counter
195 * - %p: The previous value of the counter
196 * - %S: The interval of the counter in seconds
197 * - %M: The interval of the counter in minutes
198 * - %H: The interval of the counter in hours
199 * - %D: The interval of the counter in days
200 * - %%: Print a literal %.
201 *
202 * An optional number between % and the letter in a format directive may be used to set a minimum field width.
203 * If the expanded format directive is smaller than this width (according to strlen()) the string will be
204 * left-padded (if the number is positive) or right-padded (if the number is negative) with spaces.
205 * For example, "%25n" prints the counter name left-padded up to a minimum width of 25 columns.
206 *
207 * VTY_NEWLINE will be appended to the format string when it is printed.
208 *
209 * \param[in] vty The VTY to which it should be printed
210 * \param[in] ctrg Rate counter group to be printed
211 * \param[in] fmt A format which may contain the above directives.
212 */
213void vty_out_rate_ctr_group_fmt(struct vty *vty, const char *fmt,
214 struct rate_ctr_group *ctrg)
215{
216 struct vty_out_context vctx = {vty, fmt};
Stefan Sperling97d3da22018-05-24 16:42:43 +0200217 rate_ctr_for_each_counter(ctrg, rate_ctr_handler_fmt, &vctx);
218}
219
Harald Welte96e2a002017-06-12 21:44:18 +0200220static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
221{
222 struct vty_out_context *vctx = vctx_;
223 struct vty *vty = vctx->vty;
224
225 if (ctrg->desc->class_id > vctx->max_level)
226 return 0;
227
228 if (ctrg->idx)
229 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
230 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
231 else
232 vty_out(vty, "%s%s:%s", vctx->prefix,
233 ctrg->desc->group_description, VTY_NEWLINE);
234
235 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
236
237 return 0;
238}
239
240/*! @} */
241
242
243/*! \addtogroup stats
244 * @{
245 */
246
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100247static int osmo_stat_item_handler(
248 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200249{
250 struct vty_out_context *vctx = vctx_;
251 struct vty *vty = vctx->vty;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100252 const char *unit =
253 item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ?
254 item->desc->unit : "";
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200255
256 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
257 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100258 osmo_stat_item_get_last(item),
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100259 unit, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200260
261 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200262}
Harald Welte7acb30c2011-08-17 17:13:48 +0200263
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200264/*! print a stat item group to given VTY
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200265 * \param[in] vty The VTY to which it should be printed
266 * \param[in] prefix Any additional log prefix ahead of each line
267 * \param[in] statg Stat item group to be printed
268 */
269void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100270 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200271{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200272 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200273
274 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
275 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100276 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200277}
278
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100279static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200280{
281 struct vty_out_context *vctx = vctx_;
282 struct vty *vty = vctx->vty;
283
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100284 if (statg->desc->class_id > vctx->max_level)
285 return 0;
286
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200287 if (statg->idx)
288 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
289 statg->desc->group_description, statg->idx,
290 VTY_NEWLINE);
291 else
292 vty_out(vty, "%s%s:%s", vctx->prefix,
293 statg->desc->group_description, VTY_NEWLINE);
294
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100295 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200296
297 return 0;
298}
299
Harald Welte96e2a002017-06-12 21:44:18 +0200300/*! @} */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200301
Harald Welte96e2a002017-06-12 21:44:18 +0200302/*! \addtogroup vty
303 * @{
304 */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200305
306static int handle_counter(struct osmo_counter *counter, void *vctx_)
307{
308 struct vty_out_context *vctx = vctx_;
309 struct vty *vty = vctx->vty;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200310 const char *description = counter->description;
311
312 if (!counter->description)
313 description = counter->name;
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200314
315 vty_out(vty, " %s%s: %8lu%s",
Alexander Couzens3e432e12016-10-04 11:24:02 +0200316 vctx->prefix, description,
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200317 osmo_counter_get(counter), VTY_NEWLINE);
318
319 return 0;
320}
321
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100322void vty_out_statistics_partial(struct vty *vty, const char *prefix,
323 int max_level)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200324{
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100325 struct vty_out_context vctx = {vty, prefix, max_level};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200326
327 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
328 osmo_counters_for_each(handle_counter, &vctx);
329 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100330 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200331}
332
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100333void vty_out_statistics_full(struct vty *vty, const char *prefix)
334{
335 vty_out_statistics_partial(vty, prefix, INT_MAX);
336}
337
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200338/*! Generate a VTY command string from value_string */
Harald Weltefab0ae92012-08-17 12:17:38 +0200339char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
340 const char *prefix, const char *sep,
341 const char *end, int do_lower)
342{
343 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200344 int size = strlen(prefix) + strlen(end);
345 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200346 const struct value_string *vs;
347 char *str;
348
349 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200350 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200351
352 rem = size;
353 str = talloc_zero_size(ctx, size);
354 if (!str)
355 return NULL;
356
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200357 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200358 if (ret < 0)
359 goto err;
360 OSMO_SNPRINTF_RET(ret, rem, offset, len);
361
362 for (vs = vals; vs->value || vs->str; vs++) {
363 if (vs->str) {
364 int j, name_len = strlen(vs->str)+1;
365 char name[name_len];
366
367 for (j = 0; j < name_len; j++)
368 name[j] = do_lower ?
369 tolower(vs->str[j]) : vs->str[j];
370
371 name[name_len-1] = '\0';
372 ret = snprintf(str + offset, rem, "%s%s", name, sep);
373 if (ret < 0)
374 goto err;
375 OSMO_SNPRINTF_RET(ret, rem, offset, len);
376 }
377 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200378 offset -= sep_len; /* to remove the trailing sep */
379 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200380
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200381 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200382 if (ret < 0)
383 goto err;
384 OSMO_SNPRINTF_RET(ret, rem, offset, len);
385err:
386 str[size-1] = '\0';
387 return str;
388}
389
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200390/*! @} */