Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \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 Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * (C) 2013,2015 by sysmocom - s.f.m.c. GmbH |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | #include <inttypes.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 29 | #include <string.h> |
Holger Hans Peter Freyther | b321b93 | 2012-09-11 10:39:29 +0200 | [diff] [blame] | 30 | #include <ctype.h> |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 31 | #include <limits.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/linuxlist.h> |
| 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/timer.h> |
| 36 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 37 | #include <osmocom/core/stat_item.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 38 | #include <osmocom/core/utils.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 39 | #include <osmocom/core/counter.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 40 | |
| 41 | #include <osmocom/vty/vty.h> |
| 42 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 43 | /*! \addtogroup rate_ctr |
| 44 | * @{ |
| 45 | */ |
| 46 | |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 47 | struct vty_out_context { |
| 48 | struct vty *vty; |
| 49 | const char *prefix; |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 50 | int max_level; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static int rate_ctr_handler( |
| 54 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 55 | const struct rate_ctr_desc *desc, void *vctx_) |
| 56 | { |
| 57 | struct vty_out_context *vctx = vctx_; |
| 58 | struct vty *vty = vctx->vty; |
| 59 | |
| 60 | vty_out(vty, " %s%s: %8" PRIu64 " " |
| 61 | "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s", |
| 62 | vctx->prefix, desc->description, ctr->current, |
| 63 | ctr->intv[RATE_CTR_INTV_SEC].rate, |
| 64 | ctr->intv[RATE_CTR_INTV_MIN].rate, |
| 65 | ctr->intv[RATE_CTR_INTV_HOUR].rate, |
| 66 | ctr->intv[RATE_CTR_INTV_DAY].rate, |
| 67 | VTY_NEWLINE); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 72 | /*! print a rate counter group to given VTY |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 73 | * \param[in] vty The VTY to which it should be printed |
| 74 | * \param[in] prefix Any additional log prefix ahead of each line |
| 75 | * \param[in] ctrg Rate counter group to be printed |
| 76 | */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 77 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 78 | struct rate_ctr_group *ctrg) |
| 79 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 80 | struct vty_out_context vctx = {vty, prefix}; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 81 | |
| 82 | vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 83 | |
| 84 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx); |
| 85 | } |
| 86 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 87 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_) |
| 88 | { |
| 89 | struct vty_out_context *vctx = vctx_; |
| 90 | struct vty *vty = vctx->vty; |
| 91 | |
| 92 | if (ctrg->desc->class_id > vctx->max_level) |
| 93 | return 0; |
| 94 | |
| 95 | if (ctrg->idx) |
| 96 | vty_out(vty, "%s%s (%d):%s", vctx->prefix, |
| 97 | ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE); |
| 98 | else |
| 99 | vty_out(vty, "%s%s:%s", vctx->prefix, |
| 100 | ctrg->desc->group_description, VTY_NEWLINE); |
| 101 | |
| 102 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /*! @} */ |
| 108 | |
| 109 | |
| 110 | /*! \addtogroup stats |
| 111 | * @{ |
| 112 | */ |
| 113 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 114 | static int osmo_stat_item_handler( |
| 115 | struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_) |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 116 | { |
| 117 | struct vty_out_context *vctx = vctx_; |
| 118 | struct vty *vty = vctx->vty; |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 119 | const char *unit = |
| 120 | item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ? |
| 121 | item->desc->unit : ""; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 122 | |
| 123 | vty_out(vty, " %s%s: %8" PRIi32 " %s%s", |
| 124 | vctx->prefix, item->desc->description, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 125 | osmo_stat_item_get_last(item), |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 126 | unit, VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 127 | |
| 128 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 129 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 130 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 131 | /*! print a stat item group to given VTY |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 132 | * \param[in] vty The VTY to which it should be printed |
| 133 | * \param[in] prefix Any additional log prefix ahead of each line |
| 134 | * \param[in] statg Stat item group to be printed |
| 135 | */ |
| 136 | void vty_out_stat_item_group(struct vty *vty, const char *prefix, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 137 | struct osmo_stat_item_group *statg) |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 138 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 139 | struct vty_out_context vctx = {vty, prefix}; |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 140 | |
| 141 | vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description, |
| 142 | VTY_NEWLINE); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 143 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx); |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 146 | static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_) |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 147 | { |
| 148 | struct vty_out_context *vctx = vctx_; |
| 149 | struct vty *vty = vctx->vty; |
| 150 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 151 | if (statg->desc->class_id > vctx->max_level) |
| 152 | return 0; |
| 153 | |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 154 | if (statg->idx) |
| 155 | vty_out(vty, "%s%s (%d):%s", vctx->prefix, |
| 156 | statg->desc->group_description, statg->idx, |
| 157 | VTY_NEWLINE); |
| 158 | else |
| 159 | vty_out(vty, "%s%s:%s", vctx->prefix, |
| 160 | statg->desc->group_description, VTY_NEWLINE); |
| 161 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 162 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 167 | /*! @} */ |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 168 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 169 | /*! \addtogroup vty |
| 170 | * @{ |
| 171 | */ |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 172 | |
| 173 | static int handle_counter(struct osmo_counter *counter, void *vctx_) |
| 174 | { |
| 175 | struct vty_out_context *vctx = vctx_; |
| 176 | struct vty *vty = vctx->vty; |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 177 | const char *description = counter->description; |
| 178 | |
| 179 | if (!counter->description) |
| 180 | description = counter->name; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 181 | |
| 182 | vty_out(vty, " %s%s: %8lu%s", |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 183 | vctx->prefix, description, |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 184 | osmo_counter_get(counter), VTY_NEWLINE); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 189 | void vty_out_statistics_partial(struct vty *vty, const char *prefix, |
| 190 | int max_level) |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 191 | { |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 192 | struct vty_out_context vctx = {vty, prefix, max_level}; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 193 | |
| 194 | vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE); |
| 195 | osmo_counters_for_each(handle_counter, &vctx); |
| 196 | rate_ctr_for_each_group(rate_ctr_group_handler, &vctx); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 197 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 200 | void vty_out_statistics_full(struct vty *vty, const char *prefix) |
| 201 | { |
| 202 | vty_out_statistics_partial(vty, prefix, INT_MAX); |
| 203 | } |
| 204 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 205 | /*! Generate a VTY command string from value_string */ |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 206 | char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals, |
| 207 | const char *prefix, const char *sep, |
| 208 | const char *end, int do_lower) |
| 209 | { |
| 210 | int len = 0, offset = 0, ret, rem; |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 211 | int size = strlen(prefix) + strlen(end); |
| 212 | int sep_len = strlen(sep); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 213 | const struct value_string *vs; |
| 214 | char *str; |
| 215 | |
| 216 | for (vs = vals; vs->value || vs->str; vs++) |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 217 | size += strlen(vs->str) + sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 218 | |
| 219 | rem = size; |
| 220 | str = talloc_zero_size(ctx, size); |
| 221 | if (!str) |
| 222 | return NULL; |
| 223 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 224 | ret = snprintf(str + offset, rem, "%s", prefix); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 225 | if (ret < 0) |
| 226 | goto err; |
| 227 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 228 | |
| 229 | for (vs = vals; vs->value || vs->str; vs++) { |
| 230 | if (vs->str) { |
| 231 | int j, name_len = strlen(vs->str)+1; |
| 232 | char name[name_len]; |
| 233 | |
| 234 | for (j = 0; j < name_len; j++) |
| 235 | name[j] = do_lower ? |
| 236 | tolower(vs->str[j]) : vs->str[j]; |
| 237 | |
| 238 | name[name_len-1] = '\0'; |
| 239 | ret = snprintf(str + offset, rem, "%s%s", name, sep); |
| 240 | if (ret < 0) |
| 241 | goto err; |
| 242 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 243 | } |
| 244 | } |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 245 | offset -= sep_len; /* to remove the trailing sep */ |
| 246 | rem += sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 247 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 248 | ret = snprintf(str + offset, rem, "%s", end); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 249 | if (ret < 0) |
| 250 | goto err; |
| 251 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 252 | err: |
| 253 | str[size-1] = '\0'; |
| 254 | return str; |
| 255 | } |
| 256 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 257 | /*! @} */ |