blob: 27c1a8544e954d289028661d6ae55ce576e458c2 [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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010085static int osmo_stat_item_handler(
86 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020087{
88 struct vty_out_context *vctx = vctx_;
89 struct vty *vty = vctx->vty;
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +010090 const char *unit =
91 item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ?
92 item->desc->unit : "";
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020093
94 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
95 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010096 osmo_stat_item_get_last(item),
Jacob Erlbeckaf5bad52015-11-27 18:54:58 +010097 unit, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020098
99 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200100}
Harald Welte7acb30c2011-08-17 17:13:48 +0200101
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200102/*! \brief print a stat item group to given VTY
103 * \param[in] vty The VTY to which it should be printed
104 * \param[in] prefix Any additional log prefix ahead of each line
105 * \param[in] statg Stat item group to be printed
106 */
107void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200109{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200110 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200111
112 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
113 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200115}
116
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200118{
119 struct vty_out_context *vctx = vctx_;
120 struct vty *vty = vctx->vty;
121
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100122 if (statg->desc->class_id > vctx->max_level)
123 return 0;
124
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200125 if (statg->idx)
126 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
127 statg->desc->group_description, statg->idx,
128 VTY_NEWLINE);
129 else
130 vty_out(vty, "%s%s:%s", vctx->prefix,
131 statg->desc->group_description, VTY_NEWLINE);
132
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100133 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200134
135 return 0;
136}
137
138static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
139{
140 struct vty_out_context *vctx = vctx_;
141 struct vty *vty = vctx->vty;
142
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100143 if (ctrg->desc->class_id > vctx->max_level)
144 return 0;
145
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200146 if (ctrg->idx)
147 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
148 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
149 else
150 vty_out(vty, "%s%s:%s", vctx->prefix,
151 ctrg->desc->group_description, VTY_NEWLINE);
152
153 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
154
155 return 0;
156}
157
158static int handle_counter(struct osmo_counter *counter, void *vctx_)
159{
160 struct vty_out_context *vctx = vctx_;
161 struct vty *vty = vctx->vty;
Alexander Couzens3e432e12016-10-04 11:24:02 +0200162 const char *description = counter->description;
163
164 if (!counter->description)
165 description = counter->name;
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200166
167 vty_out(vty, " %s%s: %8lu%s",
Alexander Couzens3e432e12016-10-04 11:24:02 +0200168 vctx->prefix, description,
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200169 osmo_counter_get(counter), VTY_NEWLINE);
170
171 return 0;
172}
173
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100174void vty_out_statistics_partial(struct vty *vty, const char *prefix,
175 int max_level)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200176{
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100177 struct vty_out_context vctx = {vty, prefix, max_level};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200178
179 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
180 osmo_counters_for_each(handle_counter, &vctx);
181 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100182 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200183}
184
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100185void vty_out_statistics_full(struct vty *vty, const char *prefix)
186{
187 vty_out_statistics_partial(vty, prefix, INT_MAX);
188}
189
Harald Weltefab0ae92012-08-17 12:17:38 +0200190/*! \brief Generate a VTY command string from value_string */
191char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
192 const char *prefix, const char *sep,
193 const char *end, int do_lower)
194{
195 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200196 int size = strlen(prefix) + strlen(end);
197 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200198 const struct value_string *vs;
199 char *str;
200
201 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200202 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200203
204 rem = size;
205 str = talloc_zero_size(ctx, size);
206 if (!str)
207 return NULL;
208
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200209 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200210 if (ret < 0)
211 goto err;
212 OSMO_SNPRINTF_RET(ret, rem, offset, len);
213
214 for (vs = vals; vs->value || vs->str; vs++) {
215 if (vs->str) {
216 int j, name_len = strlen(vs->str)+1;
217 char name[name_len];
218
219 for (j = 0; j < name_len; j++)
220 name[j] = do_lower ?
221 tolower(vs->str[j]) : vs->str[j];
222
223 name[name_len-1] = '\0';
224 ret = snprintf(str + offset, rem, "%s%s", name, sep);
225 if (ret < 0)
226 goto err;
227 OSMO_SNPRINTF_RET(ret, rem, offset, len);
228 }
229 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200230 offset -= sep_len; /* to remove the trailing sep */
231 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200232
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200233 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200234 if (ret < 0)
235 goto err;
236 OSMO_SNPRINTF_RET(ret, rem, offset, len);
237err:
238 str[size-1] = '\0';
239 return str;
240}
241
242
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200243/*! @} */