blob: b15c8d844d137b7c7848bec44502e994b2696572 [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;
90
91 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
92 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010093 osmo_stat_item_get_last(item),
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020094 item->desc->unit, VTY_NEWLINE);
95
96 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097}
Harald Welte7acb30c2011-08-17 17:13:48 +020098
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020099/*! \brief print a stat item group to given VTY
100 * \param[in] vty The VTY to which it should be printed
101 * \param[in] prefix Any additional log prefix ahead of each line
102 * \param[in] statg Stat item group to be printed
103 */
104void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200106{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200107 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200108
109 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
110 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200112}
113
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200115{
116 struct vty_out_context *vctx = vctx_;
117 struct vty *vty = vctx->vty;
118
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100119 if (statg->desc->class_id > vctx->max_level)
120 return 0;
121
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200122 if (statg->idx)
123 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
124 statg->desc->group_description, statg->idx,
125 VTY_NEWLINE);
126 else
127 vty_out(vty, "%s%s:%s", vctx->prefix,
128 statg->desc->group_description, VTY_NEWLINE);
129
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100130 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200131
132 return 0;
133}
134
135static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
136{
137 struct vty_out_context *vctx = vctx_;
138 struct vty *vty = vctx->vty;
139
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100140 if (ctrg->desc->class_id > vctx->max_level)
141 return 0;
142
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200143 if (ctrg->idx)
144 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
145 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
146 else
147 vty_out(vty, "%s%s:%s", vctx->prefix,
148 ctrg->desc->group_description, VTY_NEWLINE);
149
150 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
151
152 return 0;
153}
154
155static int handle_counter(struct osmo_counter *counter, void *vctx_)
156{
157 struct vty_out_context *vctx = vctx_;
158 struct vty *vty = vctx->vty;
159
160 vty_out(vty, " %s%s: %8lu%s",
161 vctx->prefix, counter->description,
162 osmo_counter_get(counter), VTY_NEWLINE);
163
164 return 0;
165}
166
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100167void vty_out_statistics_partial(struct vty *vty, const char *prefix,
168 int max_level)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200169{
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100170 struct vty_out_context vctx = {vty, prefix, max_level};
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200171
172 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
173 osmo_counters_for_each(handle_counter, &vctx);
174 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100175 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200176}
177
Jacob Erlbeck59b90bc2015-11-03 16:21:40 +0100178void vty_out_statistics_full(struct vty *vty, const char *prefix)
179{
180 vty_out_statistics_partial(vty, prefix, INT_MAX);
181}
182
Harald Weltefab0ae92012-08-17 12:17:38 +0200183/*! \brief Generate a VTY command string from value_string */
184char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
185 const char *prefix, const char *sep,
186 const char *end, int do_lower)
187{
188 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200189 int size = strlen(prefix) + strlen(end);
190 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200191 const struct value_string *vs;
192 char *str;
193
194 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200195 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200196
197 rem = size;
198 str = talloc_zero_size(ctx, size);
199 if (!str)
200 return NULL;
201
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200202 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200203 if (ret < 0)
204 goto err;
205 OSMO_SNPRINTF_RET(ret, rem, offset, len);
206
207 for (vs = vals; vs->value || vs->str; vs++) {
208 if (vs->str) {
209 int j, name_len = strlen(vs->str)+1;
210 char name[name_len];
211
212 for (j = 0; j < name_len; j++)
213 name[j] = do_lower ?
214 tolower(vs->str[j]) : vs->str[j];
215
216 name[name_len-1] = '\0';
217 ret = snprintf(str + offset, rem, "%s%s", name, sep);
218 if (ret < 0)
219 goto err;
220 OSMO_SNPRINTF_RET(ret, rem, offset, len);
221 }
222 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200223 offset -= sep_len; /* to remove the trailing sep */
224 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200225
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200226 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200227 if (ret < 0)
228 goto err;
229 OSMO_SNPRINTF_RET(ret, rem, offset, len);
230err:
231 str[size-1] = '\0';
232 return str;
233}
234
235
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200236/*! @} */