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 | * |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 24 | #include <stdbool.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 25 | #include <inttypes.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 26 | #include <string.h> |
Holger Hans Peter Freyther | b321b93 | 2012-09-11 10:39:29 +0200 | [diff] [blame] | 27 | #include <ctype.h> |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 28 | #include <limits.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/linuxlist.h> |
| 31 | #include <osmocom/core/talloc.h> |
| 32 | #include <osmocom/core/timer.h> |
| 33 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 34 | #include <osmocom/core/stat_item.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 35 | #include <osmocom/core/utils.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 36 | #include <osmocom/core/counter.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 37 | |
| 38 | #include <osmocom/vty/vty.h> |
| 39 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 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; |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 47 | int max_level; |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 48 | bool skip_zero; |
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 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 58 | if (vctx->skip_zero && ctr->current == 0) |
| 59 | return 0; |
| 60 | |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 61 | vty_out(vty, " %s%s: %8" PRIu64 " " |
| 62 | "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s", |
| 63 | vctx->prefix, desc->description, ctr->current, |
| 64 | ctr->intv[RATE_CTR_INTV_SEC].rate, |
| 65 | ctr->intv[RATE_CTR_INTV_MIN].rate, |
| 66 | ctr->intv[RATE_CTR_INTV_HOUR].rate, |
| 67 | ctr->intv[RATE_CTR_INTV_DAY].rate, |
| 68 | VTY_NEWLINE); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 73 | /*! print a rate counter group to given VTY |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 74 | * \param[in] vty The VTY to which it should be printed |
| 75 | * \param[in] prefix Any additional log prefix ahead of each line |
| 76 | * \param[in] ctrg Rate counter group to be printed |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 77 | * \param[in] skip_zero Skip all zero-valued counters |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 78 | */ |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 79 | void vty_out_rate_ctr_group2(struct vty *vty, const char *prefix, |
| 80 | struct rate_ctr_group *ctrg, bool skip_zero) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 81 | { |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 82 | struct vty_out_context vctx = {vty, prefix, 0, skip_zero}; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 83 | |
| 84 | 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] | 85 | |
| 86 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx); |
| 87 | } |
| 88 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 89 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 90 | struct rate_ctr_group *ctrg) |
| 91 | { |
| 92 | vty_out_rate_ctr_group2(vty, prefix, ctrg, false); |
| 93 | } |
| 94 | |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 95 | static char * |
| 96 | pad_append_str(char *s, const char *a, int minwidth) |
| 97 | { |
| 98 | s = talloc_asprintf_append(s, "%*s", minwidth, a); |
| 99 | OSMO_ASSERT(s); |
| 100 | return s; |
| 101 | } |
| 102 | |
| 103 | static char * |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 104 | pad_append_ctr(char *s, uint64_t ctr, int minwidth) |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 105 | { |
| 106 | s = talloc_asprintf_append(s, "%*" PRIu64, minwidth, ctr); |
| 107 | OSMO_ASSERT(s); |
| 108 | return s; |
| 109 | } |
| 110 | |
| 111 | static int rate_ctr_handler_fmt( |
| 112 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 113 | const struct rate_ctr_desc *desc, void *vctx_) |
| 114 | { |
| 115 | struct vty_out_context *vctx = vctx_; |
| 116 | struct vty *vty = vctx->vty; |
| 117 | const char *fmt = vctx->prefix; |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 118 | char *s; |
| 119 | |
| 120 | if (vctx->skip_zero && ctr->current == 0) |
| 121 | return 0; |
| 122 | |
| 123 | s = talloc_strdup(vty, ""); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 124 | OSMO_ASSERT(s); |
| 125 | |
| 126 | while (*fmt) { |
| 127 | int ch, minwidth = 0, sign = 1; |
| 128 | char *p = strchr(fmt, '%'); |
| 129 | |
| 130 | if (p == NULL) { |
| 131 | /* No further % directives in format string. Copy rest verbatim and exit. */ |
| 132 | s = talloc_strdup_append_buffer(s, fmt); |
| 133 | OSMO_ASSERT(s); |
| 134 | break; |
| 135 | } else { |
| 136 | ptrdiff_t len; |
| 137 | |
| 138 | OSMO_ASSERT(p >= fmt); |
| 139 | len = p - fmt; |
| 140 | if (len) { |
| 141 | /* Copy bytes verbatim until next '%' byte. */ |
| 142 | s = talloc_strndup_append_buffer(s, fmt, len); |
| 143 | OSMO_ASSERT(s); |
| 144 | } |
| 145 | fmt = (const char *)(p + 1); /* skip past '%' */ |
| 146 | if (*fmt == '\0') |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | ch = *fmt++; |
| 151 | if (ch == '-' && isdigit(*fmt)) { |
| 152 | sign = -1; |
| 153 | ch = *fmt++; |
| 154 | } |
| 155 | while (isdigit(ch) && *fmt != '\0') { |
| 156 | minwidth *= 10; |
| 157 | minwidth += (ch - '0'); |
| 158 | ch = *fmt++; |
| 159 | } |
| 160 | minwidth *= sign; |
| 161 | |
| 162 | switch (ch) { |
| 163 | case '%': |
| 164 | s = talloc_asprintf_append(s, "%c", ch); |
| 165 | OSMO_ASSERT(s); |
| 166 | break; |
| 167 | case 'd': |
| 168 | s = pad_append_str(s, desc->description, minwidth); |
| 169 | break; |
| 170 | case 'n': |
| 171 | s = pad_append_str(s, desc->name, minwidth); |
| 172 | break; |
| 173 | case 'c': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 174 | s = pad_append_ctr(s, ctr->current, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 175 | break; |
| 176 | case 'p': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 177 | s = pad_append_ctr(s, ctr->previous, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 178 | break; |
| 179 | case 'S': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 180 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_SEC].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 181 | break; |
| 182 | case 'M': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 183 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_MIN].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 184 | break; |
| 185 | case 'H': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 186 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_HOUR].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 187 | break; |
| 188 | case 'D': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 189 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_DAY].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 190 | break; |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | vty_out(vty, "%s%s", s, VTY_NEWLINE); |
| 197 | talloc_free(s); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /*! print a rate counter group to given VTY, formatting the line for each counter according to a format string. |
| 203 | * |
| 204 | * The following format string directives are supported: |
| 205 | * - %d: The description of the counter |
| 206 | * - %n: The name of the counter |
| 207 | * - %c: The current value of the counter |
| 208 | * - %p: The previous value of the counter |
| 209 | * - %S: The interval of the counter in seconds |
| 210 | * - %M: The interval of the counter in minutes |
| 211 | * - %H: The interval of the counter in hours |
| 212 | * - %D: The interval of the counter in days |
| 213 | * - %%: Print a literal %. |
| 214 | * |
| 215 | * An optional number between % and the letter in a format directive may be used to set a minimum field width. |
| 216 | * If the expanded format directive is smaller than this width (according to strlen()) the string will be |
| 217 | * left-padded (if the number is positive) or right-padded (if the number is negative) with spaces. |
| 218 | * For example, "%25n" prints the counter name left-padded up to a minimum width of 25 columns. |
| 219 | * |
| 220 | * VTY_NEWLINE will be appended to the format string when it is printed. |
| 221 | * |
| 222 | * \param[in] vty The VTY to which it should be printed |
| 223 | * \param[in] ctrg Rate counter group to be printed |
| 224 | * \param[in] fmt A format which may contain the above directives. |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 225 | * \param[in] skip_zero Skip all zero-valued counters |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 226 | */ |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 227 | void vty_out_rate_ctr_group_fmt2(struct vty *vty, const char *fmt, |
| 228 | struct rate_ctr_group *ctrg, bool skip_zero) |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 229 | { |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 230 | struct vty_out_context vctx = {vty, fmt, 0, skip_zero}; |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 231 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler_fmt, &vctx); |
| 232 | } |
| 233 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 234 | void vty_out_rate_ctr_group_fmt(struct vty *vty, const char *fmt, |
| 235 | struct rate_ctr_group *ctrg) |
| 236 | { |
| 237 | vty_out_rate_ctr_group_fmt2(vty, fmt, ctrg, false); |
| 238 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 239 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_) |
| 240 | { |
| 241 | struct vty_out_context *vctx = vctx_; |
| 242 | struct vty *vty = vctx->vty; |
| 243 | |
| 244 | if (ctrg->desc->class_id > vctx->max_level) |
| 245 | return 0; |
| 246 | |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 247 | vty_out(vty, "%s%s (%d)", vctx->prefix, ctrg->desc->group_description, ctrg->idx); |
| 248 | if (ctrg->name) |
| 249 | vty_out(vty, "('%s')", ctrg->name); |
| 250 | vty_out(vty, ":%s", VTY_NEWLINE); |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 251 | |
| 252 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /*! @} */ |
| 258 | |
| 259 | |
| 260 | /*! \addtogroup stats |
| 261 | * @{ |
| 262 | */ |
| 263 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 264 | static int osmo_stat_item_handler( |
| 265 | 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] | 266 | { |
| 267 | struct vty_out_context *vctx = vctx_; |
| 268 | struct vty *vty = vctx->vty; |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 269 | const struct osmo_stat_item_desc *desc = osmo_stat_item_get_desc(item); |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 270 | int32_t value = osmo_stat_item_get_last(item); |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 271 | const char *unit = (desc->unit != OSMO_STAT_ITEM_NO_UNIT) ? desc->unit : ""; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 272 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 273 | if (vctx->skip_zero && value == 0) |
| 274 | return 0; |
| 275 | |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 276 | vty_out(vty, " %s%s: %8" PRIi32 " %s%s", |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 277 | vctx->prefix, desc->description, value, unit, VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 278 | |
| 279 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 280 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 281 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 282 | /*! print a stat item group to given VTY |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 283 | * \param[in] vty The VTY to which it should be printed |
| 284 | * \param[in] prefix Any additional log prefix ahead of each line |
| 285 | * \param[in] statg Stat item group to be printed |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 286 | * \param[in] skip_zero Skip all zero-valued counters |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 287 | */ |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 288 | void vty_out_stat_item_group2(struct vty *vty, const char *prefix, |
| 289 | struct osmo_stat_item_group *statg, bool skip_zero) |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 290 | { |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 291 | struct vty_out_context vctx = {vty, prefix, 0, skip_zero}; |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 292 | |
| 293 | vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description, |
| 294 | VTY_NEWLINE); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 295 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx); |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 298 | void vty_out_stat_item_group(struct vty *vty, const char *prefix, |
| 299 | struct osmo_stat_item_group *statg) |
| 300 | { |
| 301 | return vty_out_stat_item_group2(vty, prefix, statg, false); |
| 302 | } |
| 303 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
| 306 | struct vty_out_context *vctx = vctx_; |
| 307 | struct vty *vty = vctx->vty; |
| 308 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 309 | if (statg->desc->class_id > vctx->max_level) |
| 310 | return 0; |
| 311 | |
Pau Espin Pedrol | 09f075f | 2021-05-31 13:10:24 +0200 | [diff] [blame] | 312 | vty_out(vty, "%s%s (%d)", vctx->prefix, statg->desc->group_description, statg->idx); |
| 313 | if (statg->name) |
| 314 | vty_out(vty, "('%s')", statg->name); |
| 315 | vty_out(vty, ":%s", VTY_NEWLINE); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 316 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 317 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 322 | /*! @} */ |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 323 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 324 | /*! \addtogroup vty |
| 325 | * @{ |
| 326 | */ |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 327 | |
| 328 | static int handle_counter(struct osmo_counter *counter, void *vctx_) |
| 329 | { |
| 330 | struct vty_out_context *vctx = vctx_; |
| 331 | struct vty *vty = vctx->vty; |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 332 | const char *description = counter->description; |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 333 | unsigned long value = osmo_counter_get(counter); |
| 334 | |
| 335 | if (vctx->skip_zero && value == 0) |
| 336 | return 0; |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 337 | |
| 338 | if (!counter->description) |
| 339 | description = counter->name; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 340 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 341 | vty_out(vty, " %s%s: %8lu%s", vctx->prefix, description, value, VTY_NEWLINE); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 346 | void vty_out_statistics_partial2(struct vty *vty, const char *prefix, int max_level, bool skip_zero) |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 347 | { |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 348 | struct vty_out_context vctx = {vty, prefix, max_level, skip_zero}; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 349 | |
| 350 | vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE); |
| 351 | osmo_counters_for_each(handle_counter, &vctx); |
| 352 | rate_ctr_for_each_group(rate_ctr_group_handler, &vctx); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 353 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 356 | void vty_out_statistics_partial(struct vty *vty, const char *prefix, int max_level) |
| 357 | { |
| 358 | return vty_out_statistics_partial2(vty, prefix, max_level, false); |
| 359 | } |
| 360 | |
| 361 | void vty_out_statistics_full2(struct vty *vty, const char *prefix, bool skip_zero) |
| 362 | { |
| 363 | vty_out_statistics_partial2(vty, prefix, INT_MAX, skip_zero); |
| 364 | } |
| 365 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 366 | void vty_out_statistics_full(struct vty *vty, const char *prefix) |
| 367 | { |
Harald Welte | 3b007f8 | 2022-04-18 11:39:08 +0200 | [diff] [blame] | 368 | vty_out_statistics_full2(vty, prefix, false); |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 371 | /*! Generate a VTY command string from value_string */ |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 372 | char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals, |
| 373 | const char *prefix, const char *sep, |
| 374 | const char *end, int do_lower) |
| 375 | { |
| 376 | int len = 0, offset = 0, ret, rem; |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 377 | int size = strlen(prefix) + strlen(end); |
| 378 | int sep_len = strlen(sep); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 379 | const struct value_string *vs; |
| 380 | char *str; |
| 381 | |
| 382 | for (vs = vals; vs->value || vs->str; vs++) |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 383 | size += strlen(vs->str) + sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 384 | |
| 385 | rem = size; |
| 386 | str = talloc_zero_size(ctx, size); |
| 387 | if (!str) |
| 388 | return NULL; |
| 389 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 390 | ret = snprintf(str + offset, rem, "%s", prefix); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 391 | if (ret < 0) |
| 392 | goto err; |
| 393 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 394 | |
| 395 | for (vs = vals; vs->value || vs->str; vs++) { |
| 396 | if (vs->str) { |
| 397 | int j, name_len = strlen(vs->str)+1; |
| 398 | char name[name_len]; |
| 399 | |
| 400 | for (j = 0; j < name_len; j++) |
| 401 | name[j] = do_lower ? |
| 402 | tolower(vs->str[j]) : vs->str[j]; |
| 403 | |
| 404 | name[name_len-1] = '\0'; |
| 405 | ret = snprintf(str + offset, rem, "%s%s", name, sep); |
| 406 | if (ret < 0) |
| 407 | goto err; |
| 408 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 409 | } |
| 410 | } |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 411 | offset -= sep_len; /* to remove the trailing sep */ |
| 412 | rem += sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 413 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 414 | ret = snprintf(str + offset, rem, "%s", end); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 415 | if (ret < 0) |
| 416 | goto err; |
| 417 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 418 | err: |
| 419 | str[size-1] = '\0'; |
| 420 | return str; |
| 421 | } |
| 422 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 423 | /*! @} */ |