blob: 9d1bdf9ef315f83eacfbed2d87cfc147b55232a0 [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 Welte3fb0b6f2010-05-19 19:02:52 +02005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdint.h>
25#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>
Jacob Erlbeck7211fe12015-10-19 15:11:50 +020036#include <osmocom/core/statistics.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;
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020048};
49
50static int rate_ctr_handler(
51 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
52 const struct rate_ctr_desc *desc, void *vctx_)
53{
54 struct vty_out_context *vctx = vctx_;
55 struct vty *vty = vctx->vty;
56
57 vty_out(vty, " %s%s: %8" PRIu64 " "
58 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
59 vctx->prefix, desc->description, ctr->current,
60 ctr->intv[RATE_CTR_INTV_SEC].rate,
61 ctr->intv[RATE_CTR_INTV_MIN].rate,
62 ctr->intv[RATE_CTR_INTV_HOUR].rate,
63 ctr->intv[RATE_CTR_INTV_DAY].rate,
64 VTY_NEWLINE);
65
66 return 0;
67}
68
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069/*! print a rate counter group to given VTY
Harald Welte7acb30c2011-08-17 17:13:48 +020070 * \param[in] vty The VTY to which it should be printed
71 * \param[in] prefix Any additional log prefix ahead of each line
72 * \param[in] ctrg Rate counter group to be printed
73 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020074void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
75 struct rate_ctr_group *ctrg)
76{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020077 struct vty_out_context vctx = {vty, prefix};
Harald Welte3fb0b6f2010-05-19 19:02:52 +020078
79 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020080
81 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx);
82}
83
Harald Welte96e2a002017-06-12 21:44:18 +020084static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
85{
86 struct vty_out_context *vctx = vctx_;
87 struct vty *vty = vctx->vty;
88
89 if (ctrg->desc->class_id > vctx->max_level)
90 return 0;
91
92 if (ctrg->idx)
93 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
94 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
95 else
96 vty_out(vty, "%s%s:%s", vctx->prefix,
97 ctrg->desc->group_description, VTY_NEWLINE);
98
99 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
100
101 return 0;
102}
103
104/*! @} */
105
106
107/*! \addtogroup stats
108 * @{
109 */
110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111static int osmo_stat_item_handler(
112 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200113{
114 struct vty_out_context *vctx = vctx_;
115 struct vty *vty = vctx->vty;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100116 const char *unit =
117 item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ?
118 item->desc->unit : "";
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200119
120 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
121 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122 osmo_stat_item_get_last(item),
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100123 unit, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200124
125 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126}
Harald Welte7acb30c2011-08-17 17:13:48 +0200127
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200128/*! print a stat item group to given VTY
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200129 * \param[in] vty The VTY to which it should be printed
130 * \param[in] prefix Any additional log prefix ahead of each line
131 * \param[in] statg Stat item group to be printed
132 */
133void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100134 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200135{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200136 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200137
138 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
139 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100140 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200141}
142
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100143static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200144{
145 struct vty_out_context *vctx = vctx_;
146 struct vty *vty = vctx->vty;
147
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100148 if (statg->desc->class_id > vctx->max_level)
149 return 0;
150
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200151 if (statg->idx)
152 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
153 statg->desc->group_description, statg->idx,
154 VTY_NEWLINE);
155 else
156 vty_out(vty, "%s%s:%s", vctx->prefix,
157 statg->desc->group_description, VTY_NEWLINE);
158
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100159 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200160
161 return 0;
162}
163
Harald Welte96e2a002017-06-12 21:44:18 +0200164/*! @} */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200165
Harald Welte96e2a002017-06-12 21:44:18 +0200166/*! \addtogroup vty
167 * @{
168 */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200169
170static int handle_counter(struct osmo_counter *counter, void *vctx_)
171{
172 struct vty_out_context *vctx = vctx_;
173 struct vty *vty = vctx->vty;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200174 const char *description = counter->description;
175
176 if (!counter->description)
177 description = counter->name;
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200178
179 vty_out(vty, " %s%s: %8lu%s",
Alexander Couzens3e432e12016-10-04 11:24:02 +0200180 vctx->prefix, description,
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200181 osmo_counter_get(counter), VTY_NEWLINE);
182
183 return 0;
184}
185
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100186void vty_out_statistics_partial(struct vty *vty, const char *prefix,
187 int max_level)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200188{
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100189 struct vty_out_context vctx = {vty, prefix, max_level};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200190
191 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
192 osmo_counters_for_each(handle_counter, &vctx);
193 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100194 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200195}
196
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100197void vty_out_statistics_full(struct vty *vty, const char *prefix)
198{
199 vty_out_statistics_partial(vty, prefix, INT_MAX);
200}
201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! Generate a VTY command string from value_string */
Harald Weltefab0ae92012-08-17 12:17:38 +0200203char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
204 const char *prefix, const char *sep,
205 const char *end, int do_lower)
206{
207 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200208 int size = strlen(prefix) + strlen(end);
209 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200210 const struct value_string *vs;
211 char *str;
212
213 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200214 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200215
216 rem = size;
217 str = talloc_zero_size(ctx, size);
218 if (!str)
219 return NULL;
220
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200221 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200222 if (ret < 0)
223 goto err;
224 OSMO_SNPRINTF_RET(ret, rem, offset, len);
225
226 for (vs = vals; vs->value || vs->str; vs++) {
227 if (vs->str) {
228 int j, name_len = strlen(vs->str)+1;
229 char name[name_len];
230
231 for (j = 0; j < name_len; j++)
232 name[j] = do_lower ?
233 tolower(vs->str[j]) : vs->str[j];
234
235 name[name_len-1] = '\0';
236 ret = snprintf(str + offset, rem, "%s%s", name, sep);
237 if (ret < 0)
238 goto err;
239 OSMO_SNPRINTF_RET(ret, rem, offset, len);
240 }
241 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200242 offset -= sep_len; /* to remove the trailing sep */
243 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200244
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200245 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200246 if (ret < 0)
247 goto err;
248 OSMO_SNPRINTF_RET(ret, rem, offset, len);
249err:
250 str[size-1] = '\0';
251 return str;
252}
253
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200254/*! @} */