blob: 8cd0b35ad1e05375261b37a2427cb20cc31571ad [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
Harald Welte96e2a002017-06-12 21:44:18 +020087static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
88{
89 struct vty_out_context *vctx = vctx_;
90 struct vty *vty = vctx->vty;
91
92 if (ctrg->desc->class_id > vctx->max_level)
93 return 0;
94
95 if (ctrg->idx)
96 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
97 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
98 else
99 vty_out(vty, "%s%s:%s", vctx->prefix,
100 ctrg->desc->group_description, VTY_NEWLINE);
101
102 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
103
104 return 0;
105}
106
107/*! @} */
108
109
110/*! \addtogroup stats
111 * @{
112 */
113
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114static int osmo_stat_item_handler(
115 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200116{
117 struct vty_out_context *vctx = vctx_;
118 struct vty *vty = vctx->vty;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100119 const char *unit =
120 item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ?
121 item->desc->unit : "";
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200122
123 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
124 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100125 osmo_stat_item_get_last(item),
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100126 unit, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200127
128 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129}
Harald Welte7acb30c2011-08-17 17:13:48 +0200130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! print a stat item group to given VTY
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200132 * \param[in] vty The VTY to which it should be printed
133 * \param[in] prefix Any additional log prefix ahead of each line
134 * \param[in] statg Stat item group to be printed
135 */
136void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100137 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200138{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200139 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200140
141 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
142 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100143 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200144}
145
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100146static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200147{
148 struct vty_out_context *vctx = vctx_;
149 struct vty *vty = vctx->vty;
150
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100151 if (statg->desc->class_id > vctx->max_level)
152 return 0;
153
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200154 if (statg->idx)
155 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
156 statg->desc->group_description, statg->idx,
157 VTY_NEWLINE);
158 else
159 vty_out(vty, "%s%s:%s", vctx->prefix,
160 statg->desc->group_description, VTY_NEWLINE);
161
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100162 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200163
164 return 0;
165}
166
Harald Welte96e2a002017-06-12 21:44:18 +0200167/*! @} */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200168
Harald Welte96e2a002017-06-12 21:44:18 +0200169/*! \addtogroup vty
170 * @{
171 */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200172
173static int handle_counter(struct osmo_counter *counter, void *vctx_)
174{
175 struct vty_out_context *vctx = vctx_;
176 struct vty *vty = vctx->vty;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200177 const char *description = counter->description;
178
179 if (!counter->description)
180 description = counter->name;
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200181
182 vty_out(vty, " %s%s: %8lu%s",
Alexander Couzens3e432e12016-10-04 11:24:02 +0200183 vctx->prefix, description,
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200184 osmo_counter_get(counter), VTY_NEWLINE);
185
186 return 0;
187}
188
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100189void vty_out_statistics_partial(struct vty *vty, const char *prefix,
190 int max_level)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200191{
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100192 struct vty_out_context vctx = {vty, prefix, max_level};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200193
194 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
195 osmo_counters_for_each(handle_counter, &vctx);
196 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100197 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200198}
199
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100200void vty_out_statistics_full(struct vty *vty, const char *prefix)
201{
202 vty_out_statistics_partial(vty, prefix, INT_MAX);
203}
204
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200205/*! Generate a VTY command string from value_string */
Harald Weltefab0ae92012-08-17 12:17:38 +0200206char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
207 const char *prefix, const char *sep,
208 const char *end, int do_lower)
209{
210 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200211 int size = strlen(prefix) + strlen(end);
212 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200213 const struct value_string *vs;
214 char *str;
215
216 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200217 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200218
219 rem = size;
220 str = talloc_zero_size(ctx, size);
221 if (!str)
222 return NULL;
223
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200224 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200225 if (ret < 0)
226 goto err;
227 OSMO_SNPRINTF_RET(ret, rem, offset, len);
228
229 for (vs = vals; vs->value || vs->str; vs++) {
230 if (vs->str) {
231 int j, name_len = strlen(vs->str)+1;
232 char name[name_len];
233
234 for (j = 0; j < name_len; j++)
235 name[j] = do_lower ?
236 tolower(vs->str[j]) : vs->str[j];
237
238 name[name_len-1] = '\0';
239 ret = snprintf(str + offset, rem, "%s%s", name, sep);
240 if (ret < 0)
241 goto err;
242 OSMO_SNPRINTF_RET(ret, rem, offset, len);
243 }
244 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200245 offset -= sep_len; /* to remove the trailing sep */
246 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200247
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200248 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200249 if (ret < 0)
250 goto err;
251 OSMO_SNPRINTF_RET(ret, rem, offset, len);
252err:
253 str[size-1] = '\0';
254 return str;
255}
256
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200257/*! @} */