Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 1 | /* OpenBSC stats helper for the VTY */ |
| 2 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2009-2014 by Holger Hans Peter Freyther |
| 4 | * (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 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 <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "../../config.h" |
| 27 | |
| 28 | #include <osmocom/vty/command.h> |
| 29 | #include <osmocom/vty/buffer.h> |
| 30 | #include <osmocom/vty/vty.h> |
| 31 | #include <osmocom/vty/telnet_interface.h> |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 32 | #include <osmocom/vty/telnet_interface.h> |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 33 | #include <osmocom/vty/misc.h> |
| 34 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 35 | #include <osmocom/core/stats.h> |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 36 | #include <osmocom/core/statistics.h> |
| 37 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 38 | |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 39 | #define CFG_STATS_STR "Configure stats sub-system\n" |
| 40 | #define CFG_REPORTER_STR "Configure a stats reporter\n" |
| 41 | |
| 42 | #define SHOW_STATS_STR "Show statistical values\n" |
| 43 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 44 | /*! \addtogroup stats |
| 45 | * @{ |
| 46 | * \brief VTY interface for statsd / statistic items |
| 47 | */ |
| 48 | |
Alexander Couzens | e052dc2 | 2016-10-04 18:04:37 +0200 | [diff] [blame] | 49 | /* containing version info */ |
| 50 | extern struct host host; |
| 51 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 52 | struct cmd_node cfg_stats_node = { |
| 53 | CFG_STATS_NODE, |
| 54 | "%s(config-stats)# ", |
| 55 | 1 |
| 56 | }; |
| 57 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 58 | static const struct value_string stats_class_strs[] = { |
| 59 | { OSMO_STATS_CLASS_GLOBAL, "global" }, |
| 60 | { OSMO_STATS_CLASS_PEER, "peer" }, |
| 61 | { OSMO_STATS_CLASS_SUBSCRIBER, "subscriber" }, |
| 62 | { 0, NULL } |
| 63 | }; |
| 64 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 65 | static struct osmo_stats_reporter *osmo_stats_vty2srep(struct vty *vty) |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 66 | { |
| 67 | if (vty->node == CFG_STATS_NODE) |
| 68 | return vty->index; |
| 69 | |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | static int set_srep_parameter_str(struct vty *vty, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 74 | int (*fun)(struct osmo_stats_reporter *, const char *), |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 75 | const char *val, const char *param_name) |
| 76 | { |
| 77 | int rc; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 78 | struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 79 | OSMO_ASSERT(srep); |
| 80 | |
| 81 | rc = fun(srep, val); |
| 82 | if (rc < 0) { |
| 83 | vty_out(vty, "%% Unable to set %s: %s%s", |
| 84 | param_name, strerror(-rc), VTY_NEWLINE); |
| 85 | return CMD_WARNING; |
| 86 | } |
| 87 | |
| 88 | return CMD_SUCCESS; |
| 89 | } |
| 90 | |
| 91 | static int set_srep_parameter_int(struct vty *vty, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 92 | int (*fun)(struct osmo_stats_reporter *, int), |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 93 | const char *val, const char *param_name) |
| 94 | { |
| 95 | int rc; |
| 96 | int int_val; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 97 | struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 98 | OSMO_ASSERT(srep); |
| 99 | |
| 100 | int_val = atoi(val); |
| 101 | |
| 102 | rc = fun(srep, int_val); |
| 103 | if (rc < 0) { |
| 104 | vty_out(vty, "%% Unable to set %s: %s%s", |
| 105 | param_name, strerror(-rc), VTY_NEWLINE); |
| 106 | return CMD_WARNING; |
| 107 | } |
| 108 | |
| 109 | return CMD_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | DEFUN(cfg_stats_reporter_local_ip, cfg_stats_reporter_local_ip_cmd, |
| 113 | "local-ip ADDR", |
| 114 | "Set the IP address to which we bind locally\n" |
| 115 | "IP Address\n") |
| 116 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 117 | return set_srep_parameter_str(vty, osmo_stats_reporter_set_local_addr, |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 118 | argv[0], "local address"); |
| 119 | } |
| 120 | |
| 121 | DEFUN(cfg_no_stats_reporter_local_ip, cfg_no_stats_reporter_local_ip_cmd, |
| 122 | "no local-ip", |
| 123 | NO_STR |
| 124 | "Set the IP address to which we bind locally\n") |
| 125 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 126 | return set_srep_parameter_str(vty, osmo_stats_reporter_set_local_addr, |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 127 | NULL, "local address"); |
| 128 | } |
| 129 | |
| 130 | DEFUN(cfg_stats_reporter_remote_ip, cfg_stats_reporter_remote_ip_cmd, |
| 131 | "remote-ip ADDR", |
| 132 | "Set the remote IP address to which we connect\n" |
| 133 | "IP Address\n") |
| 134 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 135 | return set_srep_parameter_str(vty, osmo_stats_reporter_set_remote_addr, |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 136 | argv[0], "remote address"); |
| 137 | } |
| 138 | |
| 139 | DEFUN(cfg_stats_reporter_remote_port, cfg_stats_reporter_remote_port_cmd, |
| 140 | "remote-port <1-65535>", |
| 141 | "Set the remote port to which we connect\n" |
| 142 | "Remote port number\n") |
| 143 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 144 | return set_srep_parameter_int(vty, osmo_stats_reporter_set_remote_port, |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 145 | argv[0], "remote port"); |
| 146 | } |
| 147 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 148 | DEFUN(cfg_stats_reporter_mtu, cfg_stats_reporter_mtu_cmd, |
| 149 | "mtu <100-65535>", |
| 150 | "Set the maximum packet size\n" |
| 151 | "Size in byte\n") |
| 152 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 153 | return set_srep_parameter_int(vty, osmo_stats_reporter_set_mtu, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 154 | argv[0], "mtu"); |
| 155 | } |
| 156 | |
| 157 | DEFUN(cfg_no_stats_reporter_mtu, cfg_no_stats_reporter_mtu_cmd, |
| 158 | "no mtu", |
| 159 | NO_STR "Set the maximum packet size\n") |
| 160 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 161 | return set_srep_parameter_int(vty, osmo_stats_reporter_set_mtu, |
Holger Hans Peter Freyther | 8f0374f | 2015-11-02 15:53:09 +0100 | [diff] [blame] | 162 | "0", "mtu"); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 165 | DEFUN(cfg_stats_reporter_prefix, cfg_stats_reporter_prefix_cmd, |
| 166 | "prefix PREFIX", |
| 167 | "Set the item name prefix\n" |
| 168 | "The prefix string\n") |
| 169 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 170 | return set_srep_parameter_str(vty, osmo_stats_reporter_set_name_prefix, |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 171 | argv[0], "prefix string"); |
| 172 | } |
| 173 | |
| 174 | DEFUN(cfg_no_stats_reporter_prefix, cfg_no_stats_reporter_prefix_cmd, |
| 175 | "no prefix", |
| 176 | NO_STR |
| 177 | "Set the item name prefix\n") |
| 178 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 179 | return set_srep_parameter_str(vty, osmo_stats_reporter_set_name_prefix, |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 180 | "", "prefix string"); |
| 181 | } |
| 182 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 183 | DEFUN(cfg_stats_reporter_level, cfg_stats_reporter_level_cmd, |
| 184 | "level (global|peer|subscriber)", |
| 185 | "Set the maximum group level\n" |
| 186 | "Report global groups only\n" |
| 187 | "Report global and network peer related groups\n" |
| 188 | "Report global, peer, and subscriber groups\n") |
| 189 | { |
| 190 | int level = get_string_value(stats_class_strs, argv[0]); |
| 191 | int rc; |
| 192 | struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty); |
| 193 | |
| 194 | OSMO_ASSERT(srep); |
| 195 | rc = osmo_stats_reporter_set_max_class(srep, level); |
| 196 | if (rc < 0) { |
| 197 | vty_out(vty, "%% Unable to set level: %s%s", |
| 198 | strerror(-rc), VTY_NEWLINE); |
| 199 | return CMD_WARNING; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 205 | DEFUN(cfg_stats_reporter_enable, cfg_stats_reporter_enable_cmd, |
| 206 | "enable", |
| 207 | "Enable the reporter\n") |
| 208 | { |
| 209 | int rc; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 210 | struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 211 | OSMO_ASSERT(srep); |
| 212 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 213 | rc = osmo_stats_reporter_enable(srep); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 214 | if (rc < 0) { |
| 215 | vty_out(vty, "%% Unable to enable the reporter: %s%s", |
| 216 | strerror(-rc), VTY_NEWLINE); |
| 217 | return CMD_WARNING; |
| 218 | } |
| 219 | |
| 220 | return CMD_SUCCESS; |
| 221 | } |
| 222 | |
| 223 | DEFUN(cfg_stats_reporter_disable, cfg_stats_reporter_disable_cmd, |
| 224 | "disable", |
| 225 | "Disable the reporter\n") |
| 226 | { |
| 227 | int rc; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 228 | struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 229 | OSMO_ASSERT(srep); |
| 230 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 231 | rc = osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 232 | if (rc < 0) { |
| 233 | vty_out(vty, "%% Unable to disable the reporter: %s%s", |
| 234 | strerror(-rc), VTY_NEWLINE); |
| 235 | return CMD_WARNING; |
| 236 | } |
| 237 | |
| 238 | return CMD_SUCCESS; |
| 239 | } |
| 240 | |
| 241 | DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd, |
| 242 | "stats reporter statsd", |
| 243 | CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n") |
| 244 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 245 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 246 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 247 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 248 | if (!srep) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 249 | srep = osmo_stats_reporter_create_statsd(NULL); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 250 | if (!srep) { |
| 251 | vty_out(vty, "%% Unable to create statsd reporter%s", |
| 252 | VTY_NEWLINE); |
| 253 | return CMD_WARNING; |
| 254 | } |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 255 | srep->max_class = OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 256 | /* TODO: if needed, add osmo_stats_add_reporter(srep); */ |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | vty->index = srep; |
| 260 | vty->node = CFG_STATS_NODE; |
| 261 | |
| 262 | return CMD_SUCCESS; |
| 263 | } |
| 264 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 265 | DEFUN(cfg_stats_interval, cfg_stats_interval_cmd, |
| 266 | "stats interval <1-65535>", |
| 267 | CFG_STATS_STR "Set the reporting interval\n" |
| 268 | "Interval in seconds\n") |
| 269 | { |
| 270 | int rc; |
| 271 | int interval = atoi(argv[0]); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 272 | rc = osmo_stats_set_interval(interval); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 273 | if (rc < 0) { |
| 274 | vty_out(vty, "%% Unable to set interval: %s%s", |
| 275 | strerror(-rc), VTY_NEWLINE); |
| 276 | return CMD_WARNING; |
| 277 | } |
| 278 | |
| 279 | return CMD_SUCCESS; |
| 280 | } |
| 281 | |
| 282 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 283 | DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd, |
| 284 | "no stats reporter statsd", |
| 285 | NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n") |
| 286 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 287 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 288 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 289 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 290 | if (!srep) { |
| 291 | vty_out(vty, "%% No statsd logging active%s", |
| 292 | VTY_NEWLINE); |
| 293 | return CMD_WARNING; |
| 294 | } |
| 295 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 296 | osmo_stats_reporter_free(srep); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 297 | |
| 298 | return CMD_SUCCESS; |
| 299 | } |
| 300 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 301 | DEFUN(cfg_stats_reporter_log, cfg_stats_reporter_log_cmd, |
| 302 | "stats reporter log", |
| 303 | CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n") |
| 304 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 305 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 306 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 307 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 308 | if (!srep) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 309 | srep = osmo_stats_reporter_create_log(NULL); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 310 | if (!srep) { |
| 311 | vty_out(vty, "%% Unable to create log reporter%s", |
| 312 | VTY_NEWLINE); |
| 313 | return CMD_WARNING; |
| 314 | } |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 315 | srep->max_class = OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 316 | /* TODO: if needed, add osmo_stats_add_reporter(srep); */ |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | vty->index = srep; |
| 320 | vty->node = CFG_STATS_NODE; |
| 321 | |
| 322 | return CMD_SUCCESS; |
| 323 | } |
| 324 | |
| 325 | DEFUN(cfg_no_stats_reporter_log, cfg_no_stats_reporter_log_cmd, |
| 326 | "no stats reporter log", |
| 327 | NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n") |
| 328 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 329 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 330 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 331 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 332 | if (!srep) { |
| 333 | vty_out(vty, "%% No log reporting active%s", |
| 334 | VTY_NEWLINE); |
| 335 | return CMD_WARNING; |
| 336 | } |
| 337 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 338 | osmo_stats_reporter_free(srep); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 339 | |
| 340 | return CMD_SUCCESS; |
| 341 | } |
| 342 | |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 343 | DEFUN(show_stats, |
| 344 | show_stats_cmd, |
| 345 | "show stats", |
| 346 | SHOW_STR SHOW_STATS_STR) |
| 347 | { |
| 348 | vty_out_statistics_full(vty, ""); |
| 349 | |
| 350 | return CMD_SUCCESS; |
| 351 | } |
| 352 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 353 | DEFUN(show_stats_level, |
| 354 | show_stats_level_cmd, |
| 355 | "show stats level (global|peer|subscriber)", |
| 356 | SHOW_STR SHOW_STATS_STR |
Holger Hans Peter Freyther | 8348194 | 2015-11-07 21:10:01 +0100 | [diff] [blame] | 357 | "Set the maximum group level\n" |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 358 | "Show global groups only\n" |
| 359 | "Show global and network peer related groups\n" |
| 360 | "Show global, peer, and subscriber groups\n") |
| 361 | { |
| 362 | int level = get_string_value(stats_class_strs, argv[0]); |
| 363 | vty_out_statistics_partial(vty, "", level); |
| 364 | |
| 365 | return CMD_SUCCESS; |
| 366 | } |
| 367 | |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 368 | static int asciidoc_handle_counter(struct osmo_counter *counter, void *sctx_) |
| 369 | { |
| 370 | struct vty *vty = sctx_; |
| 371 | char *name = osmo_asciidoc_escape(counter->name); |
| 372 | char *description = osmo_asciidoc_escape(counter->description); |
| 373 | |
| 374 | /* | name | This document & | description | */ |
| 375 | vty_out(vty, "| %s | <<ungroup_counter_%s>> | %s%s", |
| 376 | name, |
| 377 | name, |
| 378 | description ? description : "", |
| 379 | VTY_NEWLINE); |
| 380 | |
| 381 | talloc_free(name); |
| 382 | talloc_free(description); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static void asciidoc_counter_generate(struct vty *vty) |
| 388 | { |
| 389 | vty_out(vty, "// ungrouped osmo_counters%s", VTY_NEWLINE); |
| 390 | vty_out(vty, ".ungrouped osmo counters%s", VTY_NEWLINE); |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 391 | vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 392 | vty_out(vty, "|===%s", VTY_NEWLINE); |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 393 | vty_out(vty, "| Name | Reference | Description%s", VTY_NEWLINE); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 394 | osmo_counters_for_each(asciidoc_handle_counter, vty); |
| 395 | vty_out(vty, "|===%s", VTY_NEWLINE); |
| 396 | } |
| 397 | |
| 398 | static int asciidoc_rate_ctr_handler( |
| 399 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 400 | const struct rate_ctr_desc *desc, void *sctx_) |
| 401 | { |
| 402 | struct vty *vty = sctx_; |
| 403 | char *name = osmo_asciidoc_escape(desc->name); |
| 404 | char *description = osmo_asciidoc_escape(desc->description); |
| 405 | char *group_name_prefix = osmo_asciidoc_escape(ctrg->desc->group_name_prefix); |
| 406 | |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 407 | /* | Name | This document & | Description | */ |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 408 | vty_out(vty, "| %s | <<%s_%s>> | %s%s", |
| 409 | name, |
| 410 | group_name_prefix, |
| 411 | name, |
| 412 | description ? description : NULL, |
| 413 | VTY_NEWLINE); |
| 414 | |
| 415 | /* description seems to be optional */ |
| 416 | talloc_free(name); |
| 417 | talloc_free(group_name_prefix); |
| 418 | talloc_free(description); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int asciidoc_rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) |
| 424 | { |
| 425 | struct vty *vty = sctx_; |
| 426 | |
| 427 | char *group_description = osmo_asciidoc_escape(ctrg->desc->group_description); |
| 428 | char *group_name_prefix = osmo_asciidoc_escape(ctrg->desc->group_name_prefix); |
| 429 | |
| 430 | vty_out(vty, "// rate_ctr_group table %s%s", group_description, VTY_NEWLINE); |
| 431 | vty_out(vty, ".%s - %s %s", group_name_prefix, group_description, VTY_NEWLINE); |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 432 | vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 433 | vty_out(vty, "|===%s", VTY_NEWLINE); |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 434 | vty_out(vty, "| Name | Reference | Description%s", VTY_NEWLINE); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 435 | rate_ctr_for_each_counter(ctrg, asciidoc_rate_ctr_handler, sctx_); |
| 436 | vty_out(vty, "|===%s", VTY_NEWLINE); |
| 437 | |
| 438 | talloc_free(group_name_prefix); |
| 439 | talloc_free(group_description); |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int asciidoc_osmo_stat_item_handler( |
| 445 | struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_) |
| 446 | { |
| 447 | struct vty *vty = sctx_; |
| 448 | |
| 449 | char *name = osmo_asciidoc_escape(item->desc->name); |
| 450 | char *description = osmo_asciidoc_escape(item->desc->description); |
| 451 | char *group_name_prefix = osmo_asciidoc_escape(statg->desc->group_name_prefix); |
| 452 | char *unit = osmo_asciidoc_escape(item->desc->unit); |
| 453 | |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 454 | /* | Name | Reference | Description | Unit | */ |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 455 | vty_out(vty, "| %s | <<%s_%s>> | %s | %s%s", |
| 456 | name, |
| 457 | group_name_prefix, |
| 458 | name, |
| 459 | description ? description : "", |
| 460 | unit ? unit : "", |
| 461 | VTY_NEWLINE); |
| 462 | |
| 463 | talloc_free(name); |
| 464 | talloc_free(group_name_prefix); |
| 465 | talloc_free(description); |
| 466 | talloc_free(unit); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int asciidoc_osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_) |
| 472 | { |
| 473 | char *group_name_prefix = osmo_asciidoc_escape(statg->desc->group_name_prefix); |
| 474 | char *group_description = osmo_asciidoc_escape(statg->desc->group_description); |
| 475 | |
| 476 | struct vty *vty = sctx_; |
| 477 | vty_out(vty, "%s%s", group_description ? group_description : "" , VTY_NEWLINE); |
| 478 | |
| 479 | vty_out(vty, "// osmo_stat_item_group table %s%s", group_description ? group_description : "", VTY_NEWLINE); |
| 480 | vty_out(vty, ".%s - %s %s", group_name_prefix, group_description ? group_description : "", VTY_NEWLINE); |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 481 | vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 482 | vty_out(vty, "|===%s", VTY_NEWLINE); |
Alexander Couzens | eb604cf | 2016-10-14 13:54:47 +0200 | [diff] [blame] | 483 | vty_out(vty, "| Name | Reference | Description | Unit%s", VTY_NEWLINE); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 484 | osmo_stat_item_for_each_item(statg, asciidoc_osmo_stat_item_handler, sctx_); |
| 485 | vty_out(vty, "|===%s", VTY_NEWLINE); |
| 486 | |
| 487 | talloc_free(group_name_prefix); |
| 488 | talloc_free(group_description); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | DEFUN(show_stats_asciidoc_table, |
| 494 | show_stats_asciidoc_table_cmd, |
| 495 | "show asciidoc counters", |
Harald Welte | f624c33 | 2016-06-15 11:35:16 +0200 | [diff] [blame] | 496 | SHOW_STR "Asciidoc generation\n" "Generate table of all registered counters\n") |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 497 | { |
Alexander Couzens | e052dc2 | 2016-10-04 18:04:37 +0200 | [diff] [blame] | 498 | vty_out(vty, "// autogenerated by show asciidoc counters%s", VTY_NEWLINE); |
| 499 | vty_out(vty, "These counters and their description based on %s %s (%s).%s%s", |
| 500 | host.app_info->name, |
| 501 | host.app_info->version, |
| 502 | host.app_info->name ? host.app_info->name : "", VTY_NEWLINE, VTY_NEWLINE); |
| 503 | /* 2x VTY_NEWLINE are intentional otherwise it would interpret the first table header |
| 504 | * as usual text*/ |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 505 | vty_out(vty, "// generating tables for rate_ctr_group%s", VTY_NEWLINE); |
| 506 | rate_ctr_for_each_group(asciidoc_rate_ctr_group_handler, vty); |
| 507 | |
| 508 | vty_out(vty, "// generating tables for osmo_stat_items%s", VTY_NEWLINE); |
| 509 | osmo_stat_item_for_each_group(asciidoc_osmo_stat_item_group_handler, vty); |
| 510 | |
| 511 | vty_out(vty, "// generating tables for osmo_counters%s", VTY_NEWLINE); |
| 512 | asciidoc_counter_generate(vty); |
| 513 | return CMD_SUCCESS; |
| 514 | } |
| 515 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 516 | static int config_write_stats_reporter(struct vty *vty, struct osmo_stats_reporter *srep) |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 517 | { |
| 518 | if (srep == NULL) |
| 519 | return 0; |
| 520 | |
| 521 | switch (srep->type) { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 522 | case OSMO_STATS_REPORTER_STATSD: |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 523 | vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE); |
| 524 | break; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 525 | case OSMO_STATS_REPORTER_LOG: |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 526 | vty_out(vty, "stats reporter log%s", VTY_NEWLINE); |
| 527 | break; |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | vty_out(vty, " disable%s", VTY_NEWLINE); |
| 531 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 532 | if (srep->have_net_config) { |
| 533 | if (srep->dest_addr_str) |
| 534 | vty_out(vty, " remote-ip %s%s", |
| 535 | srep->dest_addr_str, VTY_NEWLINE); |
| 536 | if (srep->dest_port) |
| 537 | vty_out(vty, " remote-port %d%s", |
| 538 | srep->dest_port, VTY_NEWLINE); |
| 539 | if (srep->bind_addr_str) |
| 540 | vty_out(vty, " local-ip %s%s", |
| 541 | srep->bind_addr_str, VTY_NEWLINE); |
| 542 | if (srep->mtu) |
| 543 | vty_out(vty, " mtu %d%s", |
| 544 | srep->mtu, VTY_NEWLINE); |
| 545 | } |
| 546 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 547 | if (srep->max_class) |
| 548 | vty_out(vty, " level %s%s", |
| 549 | get_value_string(stats_class_strs, srep->max_class), |
| 550 | VTY_NEWLINE); |
| 551 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 552 | if (srep->name_prefix && *srep->name_prefix) |
| 553 | vty_out(vty, " prefix %s%s", |
| 554 | srep->name_prefix, VTY_NEWLINE); |
| 555 | else |
| 556 | vty_out(vty, " no prefix%s", VTY_NEWLINE); |
| 557 | |
| 558 | if (srep->enabled) |
| 559 | vty_out(vty, " enable%s", VTY_NEWLINE); |
| 560 | |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | static int config_write_stats(struct vty *vty) |
| 565 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 566 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 567 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 568 | /* TODO: loop through all reporters */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 569 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 570 | config_write_stats_reporter(vty, srep); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 571 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 572 | config_write_stats_reporter(vty, srep); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 573 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 574 | vty_out(vty, "stats interval %d%s", osmo_stats_config->interval, VTY_NEWLINE); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 575 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 576 | return 1; |
| 577 | } |
| 578 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 579 | /*! \brief Add stats related commands to the VTY |
| 580 | * Call this once during your application initialization if you would |
| 581 | * like to have stats VTY commands enabled. |
| 582 | */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 583 | void osmo_stats_vty_add_cmds() |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 584 | { |
| 585 | install_element_ve(&show_stats_cmd); |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 586 | install_element_ve(&show_stats_level_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 587 | |
| 588 | install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd); |
| 589 | install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 590 | install_element(CONFIG_NODE, &cfg_stats_reporter_log_cmd); |
| 591 | install_element(CONFIG_NODE, &cfg_no_stats_reporter_log_cmd); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 592 | install_element(CONFIG_NODE, &cfg_stats_interval_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 593 | |
| 594 | install_node(&cfg_stats_node, config_write_stats); |
| 595 | vty_install_default(CFG_STATS_NODE); |
| 596 | |
| 597 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd); |
| 598 | install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd); |
| 599 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd); |
| 600 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 601 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_mtu_cmd); |
| 602 | install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_mtu_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 603 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd); |
| 604 | install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd); |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 605 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_level_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 606 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd); |
| 607 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd); |
Alexander Couzens | ad580ba | 2016-05-16 16:01:45 +0200 | [diff] [blame] | 608 | |
| 609 | install_element_ve(&show_stats_asciidoc_table_cmd); |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 610 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 611 | |
| 612 | /*! @} */ |