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