Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1 | /* 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 Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 25 | #include <string.h> |
Holger Hans Peter Freyther | b321b93 | 2012-09-11 10:39:29 +0200 | [diff] [blame] | 26 | #include <ctype.h> |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 27 | #include <limits.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 28 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 29 | #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 Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 33 | #include <osmocom/core/stat_item.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 35 | #include <osmocom/core/statistics.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 36 | |
| 37 | #include <osmocom/vty/vty.h> |
| 38 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 39 | /* \file utils.c */ |
| 40 | |
| 41 | /*! \addtogroup rate_ctr |
| 42 | * @{ |
| 43 | */ |
| 44 | |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 45 | struct vty_out_context { |
| 46 | struct vty *vty; |
| 47 | const char *prefix; |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 48 | int max_level; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static 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 Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 70 | /*! \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 Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 75 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 76 | struct rate_ctr_group *ctrg) |
| 77 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 78 | struct vty_out_context vctx = {vty, prefix}; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 79 | |
| 80 | 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] | 81 | |
| 82 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx); |
| 83 | } |
| 84 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 85 | static int osmo_stat_item_handler( |
| 86 | 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] | 87 | { |
| 88 | struct vty_out_context *vctx = vctx_; |
| 89 | struct vty *vty = vctx->vty; |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 90 | const char *unit = |
| 91 | item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ? |
| 92 | item->desc->unit : ""; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 93 | |
| 94 | vty_out(vty, " %s%s: %8" PRIi32 " %s%s", |
| 95 | vctx->prefix, item->desc->description, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 96 | osmo_stat_item_get_last(item), |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 97 | unit, VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 98 | |
| 99 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 100 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 101 | |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 102 | /*! \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 | */ |
| 107 | void vty_out_stat_item_group(struct vty *vty, const char *prefix, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 108 | struct osmo_stat_item_group *statg) |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 109 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 110 | struct vty_out_context vctx = {vty, prefix}; |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 111 | |
| 112 | vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description, |
| 113 | VTY_NEWLINE); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 114 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx); |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 117 | 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] | 118 | { |
| 119 | struct vty_out_context *vctx = vctx_; |
| 120 | struct vty *vty = vctx->vty; |
| 121 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 122 | if (statg->desc->class_id > vctx->max_level) |
| 123 | return 0; |
| 124 | |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 125 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 133 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static 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 Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 143 | if (ctrg->desc->class_id > vctx->max_level) |
| 144 | return 0; |
| 145 | |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 146 | 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 | |
| 158 | static int handle_counter(struct osmo_counter *counter, void *vctx_) |
| 159 | { |
| 160 | struct vty_out_context *vctx = vctx_; |
| 161 | struct vty *vty = vctx->vty; |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 162 | const char *description = counter->description; |
| 163 | |
| 164 | if (!counter->description) |
| 165 | description = counter->name; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 166 | |
| 167 | vty_out(vty, " %s%s: %8lu%s", |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 168 | vctx->prefix, description, |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 169 | osmo_counter_get(counter), VTY_NEWLINE); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 174 | void vty_out_statistics_partial(struct vty *vty, const char *prefix, |
| 175 | int max_level) |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 176 | { |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 177 | struct vty_out_context vctx = {vty, prefix, max_level}; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 178 | |
| 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 182 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 185 | void vty_out_statistics_full(struct vty *vty, const char *prefix) |
| 186 | { |
| 187 | vty_out_statistics_partial(vty, prefix, INT_MAX); |
| 188 | } |
| 189 | |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 190 | /*! \brief Generate a VTY command string from value_string */ |
| 191 | char *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 Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 196 | int size = strlen(prefix) + strlen(end); |
| 197 | int sep_len = strlen(sep); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 198 | const struct value_string *vs; |
| 199 | char *str; |
| 200 | |
| 201 | for (vs = vals; vs->value || vs->str; vs++) |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 202 | size += strlen(vs->str) + sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 203 | |
| 204 | rem = size; |
| 205 | str = talloc_zero_size(ctx, size); |
| 206 | if (!str) |
| 207 | return NULL; |
| 208 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 209 | ret = snprintf(str + offset, rem, "%s", prefix); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 210 | 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 Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 230 | offset -= sep_len; /* to remove the trailing sep */ |
| 231 | rem += sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 232 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 233 | ret = snprintf(str + offset, rem, "%s", end); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 234 | if (ret < 0) |
| 235 | goto err; |
| 236 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 237 | err: |
| 238 | str[size-1] = '\0'; |
| 239 | return str; |
| 240 | } |
| 241 | |
| 242 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 243 | /*! @} */ |