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> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 27 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 28 | #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 Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 32 | #include <osmocom/core/stat_item.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 33 | #include <osmocom/core/utils.h> |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 34 | #include <osmocom/core/statistics.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 35 | |
| 36 | #include <osmocom/vty/vty.h> |
| 37 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 38 | /* \file utils.c */ |
| 39 | |
| 40 | /*! \addtogroup rate_ctr |
| 41 | * @{ |
| 42 | */ |
| 43 | |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 44 | struct vty_out_context { |
| 45 | struct vty *vty; |
| 46 | const char *prefix; |
| 47 | }; |
| 48 | |
| 49 | static 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 Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 68 | /*! \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 Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 73 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 74 | struct rate_ctr_group *ctrg) |
| 75 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 76 | struct vty_out_context vctx = {vty, prefix}; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 77 | |
| 78 | 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] | 79 | |
| 80 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx); |
| 81 | } |
| 82 | |
| 83 | static int stat_item_handler( |
| 84 | struct stat_item_group *statg, struct stat_item *item, void *vctx_) |
| 85 | { |
| 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, |
| 91 | stat_item_get_last(item), |
| 92 | item->desc->unit, VTY_NEWLINE); |
| 93 | |
| 94 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 95 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 96 | |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 97 | /*! \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 | */ |
| 102 | void vty_out_stat_item_group(struct vty *vty, const char *prefix, |
| 103 | struct stat_item_group *statg) |
| 104 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 105 | struct vty_out_context vctx = {vty, prefix}; |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 106 | |
| 107 | vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description, |
| 108 | VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 109 | stat_item_for_each_item(statg, stat_item_handler, &vctx); |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 112 | static int stat_item_group_handler(struct stat_item_group *statg, void *vctx_) |
| 113 | { |
| 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 | |
| 125 | stat_item_for_each_item(statg, stat_item_handler, vctx); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static 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 | |
| 147 | static 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 | |
| 159 | void 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); |
| 166 | stat_item_for_each_group(stat_item_group_handler, &vctx); |
| 167 | } |
| 168 | |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 169 | /*! \brief Generate a VTY command string from value_string */ |
| 170 | char *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 Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 175 | int size = strlen(prefix) + strlen(end); |
| 176 | int sep_len = strlen(sep); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 177 | const struct value_string *vs; |
| 178 | char *str; |
| 179 | |
| 180 | for (vs = vals; vs->value || vs->str; vs++) |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 181 | size += strlen(vs->str) + sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 182 | |
| 183 | rem = size; |
| 184 | str = talloc_zero_size(ctx, size); |
| 185 | if (!str) |
| 186 | return NULL; |
| 187 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 188 | ret = snprintf(str + offset, rem, "%s", prefix); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 189 | 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 Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 209 | offset -= sep_len; /* to remove the trailing sep */ |
| 210 | rem += sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 211 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 212 | ret = snprintf(str + offset, rem, "%s", end); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 213 | if (ret < 0) |
| 214 | goto err; |
| 215 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 216 | err: |
| 217 | str[size-1] = '\0'; |
| 218 | return str; |
| 219 | } |
| 220 | |
| 221 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 222 | /*! @} */ |