blob: 8df44ae19135f18512d3d9710ddcb123c91a6e97 [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>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020027
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/talloc.h>
30#include <osmocom/core/timer.h>
31#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020032#include <osmocom/core/stat_item.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020033#include <osmocom/core/utils.h>
Jacob Erlbeck7211fe12015-10-19 15:11:50 +020034#include <osmocom/core/statistics.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020035
36#include <osmocom/vty/vty.h>
37
Harald Welte7acb30c2011-08-17 17:13:48 +020038/* \file utils.c */
39
40/*! \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;
47};
48
49static int rate_ctr_handler(
50 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
51 const struct rate_ctr_desc *desc, void *vctx_)
52{
53 struct vty_out_context *vctx = vctx_;
54 struct vty *vty = vctx->vty;
55
56 vty_out(vty, " %s%s: %8" PRIu64 " "
57 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
58 vctx->prefix, desc->description, ctr->current,
59 ctr->intv[RATE_CTR_INTV_SEC].rate,
60 ctr->intv[RATE_CTR_INTV_MIN].rate,
61 ctr->intv[RATE_CTR_INTV_HOUR].rate,
62 ctr->intv[RATE_CTR_INTV_DAY].rate,
63 VTY_NEWLINE);
64
65 return 0;
66}
67
Harald Welte7acb30c2011-08-17 17:13:48 +020068/*! \brief print a rate counter group to given VTY
69 * \param[in] vty The VTY to which it should be printed
70 * \param[in] prefix Any additional log prefix ahead of each line
71 * \param[in] ctrg Rate counter group to be printed
72 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
74 struct rate_ctr_group *ctrg)
75{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020076 struct vty_out_context vctx = {vty, prefix};
Harald Welte3fb0b6f2010-05-19 19:02:52 +020077
78 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020079
80 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx);
81}
82
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010083static int osmo_stat_item_handler(
84 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_)
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020085{
86 struct vty_out_context *vctx = vctx_;
87 struct vty *vty = vctx->vty;
88
89 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
90 vctx->prefix, item->desc->description,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010091 osmo_stat_item_get_last(item),
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020092 item->desc->unit, VTY_NEWLINE);
93
94 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095}
Harald Welte7acb30c2011-08-17 17:13:48 +020096
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020097/*! \brief print a stat item group to given VTY
98 * \param[in] vty The VTY to which it should be printed
99 * \param[in] prefix Any additional log prefix ahead of each line
100 * \param[in] statg Stat item group to be printed
101 */
102void vty_out_stat_item_group(struct vty *vty, const char *prefix,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103 struct osmo_stat_item_group *statg)
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200104{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200105 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200106
107 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
108 VTY_NEWLINE);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200110}
111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_)
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200113{
114 struct vty_out_context *vctx = vctx_;
115 struct vty *vty = vctx->vty;
116
117 if (statg->idx)
118 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
119 statg->desc->group_description, statg->idx,
120 VTY_NEWLINE);
121 else
122 vty_out(vty, "%s%s:%s", vctx->prefix,
123 statg->desc->group_description, VTY_NEWLINE);
124
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100125 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200126
127 return 0;
128}
129
130static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
131{
132 struct vty_out_context *vctx = vctx_;
133 struct vty *vty = vctx->vty;
134
135 if (ctrg->idx)
136 vty_out(vty, "%s%s (%d):%s", vctx->prefix,
137 ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
138 else
139 vty_out(vty, "%s%s:%s", vctx->prefix,
140 ctrg->desc->group_description, VTY_NEWLINE);
141
142 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
143
144 return 0;
145}
146
147static int handle_counter(struct osmo_counter *counter, void *vctx_)
148{
149 struct vty_out_context *vctx = vctx_;
150 struct vty *vty = vctx->vty;
151
152 vty_out(vty, " %s%s: %8lu%s",
153 vctx->prefix, counter->description,
154 osmo_counter_get(counter), VTY_NEWLINE);
155
156 return 0;
157}
158
159void vty_out_statistics_full(struct vty *vty, const char *prefix)
160{
161 struct vty_out_context vctx = {vty, prefix};
162
163 vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE);
164 osmo_counters_for_each(handle_counter, &vctx);
165 rate_ctr_for_each_group(rate_ctr_group_handler, &vctx);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100166 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx);
Jacob Erlbeck7211fe12015-10-19 15:11:50 +0200167}
168
Harald Weltefab0ae92012-08-17 12:17:38 +0200169/*! \brief Generate a VTY command string from value_string */
170char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
171 const char *prefix, const char *sep,
172 const char *end, int do_lower)
173{
174 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200175 int size = strlen(prefix) + strlen(end);
176 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200177 const struct value_string *vs;
178 char *str;
179
180 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200181 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200182
183 rem = size;
184 str = talloc_zero_size(ctx, size);
185 if (!str)
186 return NULL;
187
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200188 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200189 if (ret < 0)
190 goto err;
191 OSMO_SNPRINTF_RET(ret, rem, offset, len);
192
193 for (vs = vals; vs->value || vs->str; vs++) {
194 if (vs->str) {
195 int j, name_len = strlen(vs->str)+1;
196 char name[name_len];
197
198 for (j = 0; j < name_len; j++)
199 name[j] = do_lower ?
200 tolower(vs->str[j]) : vs->str[j];
201
202 name[name_len-1] = '\0';
203 ret = snprintf(str + offset, rem, "%s%s", name, sep);
204 if (ret < 0)
205 goto err;
206 OSMO_SNPRINTF_RET(ret, rem, offset, len);
207 }
208 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200209 offset -= sep_len; /* to remove the trailing sep */
210 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200211
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200212 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200213 if (ret < 0)
214 goto err;
215 OSMO_SNPRINTF_RET(ret, rem, offset, len);
216err:
217 str[size-1] = '\0';
218 return str;
219}
220
221
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200222/*! @} */