blob: 6772593d939d6b6da50ed9678b360832e50a1366 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/* utility routines for printing common objects in the Osmocom world */
2
3/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
4 *
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 <stdint.h>
24#include <inttypes.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020025#include <string.h>
Holger Hans Peter Freytherb321b932012-09-11 10:39:29 +020026#include <ctype.h>
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +010027#include <limits.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020028
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/linuxlist.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/timer.h>
32#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020033#include <osmocom/core/stat_item.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020034#include <osmocom/core/utils.h>
Jacob Erlbeck7211fe12015-10-19 15:11:50 +020035#include <osmocom/core/statistics.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020036
37#include <osmocom/vty/vty.h>
38
Harald Welte7acb30c2011-08-17 17:13:48 +020039/* \file utils.c */
40
41/*! \addtogroup rate_ctr
42 * @{
43 */
44
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020045struct vty_out_context {
46 struct vty *vty;
47 const char *prefix;
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +010048 int max_level;
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
58 vty_out(vty, " %s%s: %8" PRIu64 " "
59 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
60 vctx->prefix, desc->description, ctr->current,
61 ctr->intv[RATE_CTR_INTV_SEC].rate,
62 ctr->intv[RATE_CTR_INTV_MIN].rate,
63 ctr->intv[RATE_CTR_INTV_HOUR].rate,
64 ctr->intv[RATE_CTR_INTV_DAY].rate,
65 VTY_NEWLINE);
66
67 return 0;
68}
69
Harald Welte7acb30c2011-08-17 17:13:48 +020070/*! \brief print a rate counter group to given VTY
71 * \param[in] vty The VTY to which it should be printed
72 * \param[in] prefix Any additional log prefix ahead of each line
73 * \param[in] ctrg Rate counter group to be printed
74 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020075void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
76 struct rate_ctr_group *ctrg)
77{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020078 struct vty_out_context vctx = {vty, prefix};
Harald Welte3fb0b6f2010-05-19 19:02:52 +020079
80 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020081
82 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx);
83}
84
Harald Welte96e2a002017-06-12 21:44:18 +020085static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
86{
87 struct vty_out_context *vctx = vctx_;
88 struct vty *vty = vctx->vty;
89
90 if (ctrg->desc->class_id > vctx->max_level)
91 return 0;
92
93 if (ctrg->idx)
94 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
95 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
96 else
97 vty_out(vty, "%s%s:%s", vctx->prefix,
98 ctrg->desc->group_description, VTY_NEWLINE);
99
100 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
101
102 return 0;
103}
104
105/*! @} */
106
107
108/*! \addtogroup stats
109 * @{
110 */
111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112static int osmo_stat_item_handler(
113 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200114{
115 struct vty_out_context *vctx = vctx_;
116 struct vty *vty = vctx->vty;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100117 const char *unit =
118 item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ?
119 item->desc->unit : "";
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200120
121 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
122 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100123 osmo_stat_item_get_last(item),
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +0100124 unit, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200125
126 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127}
Harald Welte7acb30c2011-08-17 17:13:48 +0200128
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200129/*! \brief print a stat item group to given VTY
130 * \param[in] vty The VTY to which it should be printed
131 * \param[in] prefix Any additional log prefix ahead of each line
132 * \param[in] statg Stat item group to be printed
133 */
134void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100135 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200136{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200137 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200138
139 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
140 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100141 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200142}
143
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100144static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200145{
146 struct vty_out_context *vctx = vctx_;
147 struct vty *vty = vctx->vty;
148
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100149 if (statg->desc->class_id > vctx->max_level)
150 return 0;
151
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200152 if (statg->idx)
153 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
154 statg->desc->group_description, statg->idx,
155 VTY_NEWLINE);
156 else
157 vty_out(vty, "%s%s:%s", vctx->prefix,
158 statg->desc->group_description, VTY_NEWLINE);
159
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100160 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200161
162 return 0;
163}
164
Harald Welte96e2a002017-06-12 21:44:18 +0200165/*! @} */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200166
Harald Welte96e2a002017-06-12 21:44:18 +0200167/*! \addtogroup vty
168 * @{
169 */
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200170
171static int handle_counter(struct osmo_counter *counter, void *vctx_)
172{
173 struct vty_out_context *vctx = vctx_;
174 struct vty *vty = vctx->vty;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200175 const char *description = counter->description;
176
177 if (!counter->description)
178 description = counter->name;
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200179
180 vty_out(vty, " %s%s: %8lu%s",
Alexander Couzens3e432e12016-10-04 11:24:02 +0200181 vctx->prefix, description,
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200182 osmo_counter_get(counter), VTY_NEWLINE);
183
184 return 0;
185}
186
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100187void vty_out_statistics_partial(struct vty *vty, const char *prefix,
188 int max_level)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200189{
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100190 struct vty_out_context vctx = {vty, prefix, max_level};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200191
192 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
193 osmo_counters_for_each(handle_counter, &vctx);
194 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100195 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200196}
197
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100198void vty_out_statistics_full(struct vty *vty, const char *prefix)
199{
200 vty_out_statistics_partial(vty, prefix, INT_MAX);
201}
202
Harald Weltefab0ae92012-08-17 12:17:38 +0200203/*! \brief Generate a VTY command string from value_string */
204char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
205 const char *prefix, const char *sep,
206 const char *end, int do_lower)
207{
208 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200209 int size = strlen(prefix) + strlen(end);
210 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200211 const struct value_string *vs;
212 char *str;
213
214 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200215 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200216
217 rem = size;
218 str = talloc_zero_size(ctx, size);
219 if (!str)
220 return NULL;
221
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200222 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200223 if (ret < 0)
224 goto err;
225 OSMO_SNPRINTF_RET(ret, rem, offset, len);
226
227 for (vs = vals; vs->value || vs->str; vs++) {
228 if (vs->str) {
229 int j, name_len = strlen(vs->str)+1;
230 char name[name_len];
231
232 for (j = 0; j < name_len; j++)
233 name[j] = do_lower ?
234 tolower(vs->str[j]) : vs->str[j];
235
236 name[name_len-1] = '\0';
237 ret = snprintf(str + offset, rem, "%s%s", name, sep);
238 if (ret < 0)
239 goto err;
240 OSMO_SNPRINTF_RET(ret, rem, offset, len);
241 }
242 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200243 offset -= sep_len; /* to remove the trailing sep */
244 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200245
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200246 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200247 if (ret < 0)
248 goto err;
249 OSMO_SNPRINTF_RET(ret, rem, offset, len);
250err:
251 str[size-1] = '\0';
252 return str;
253}
254
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200255/*! @} */