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> |
| 36 | |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 37 | #define CFG_STATS_STR "Configure stats sub-system\n" |
| 38 | #define CFG_REPORTER_STR "Configure a stats reporter\n" |
| 39 | |
| 40 | #define SHOW_STATS_STR "Show statistical values\n" |
| 41 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 42 | struct cmd_node cfg_stats_node = { |
| 43 | CFG_STATS_NODE, |
| 44 | "%s(config-stats)# ", |
| 45 | 1 |
| 46 | }; |
| 47 | |
| 48 | static struct stats_reporter *osmo_stats_vty2srep(struct vty *vty) |
| 49 | { |
| 50 | if (vty->node == CFG_STATS_NODE) |
| 51 | return vty->index; |
| 52 | |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | static int set_srep_parameter_str(struct vty *vty, |
| 57 | int (*fun)(struct stats_reporter *, const char *), |
| 58 | const char *val, const char *param_name) |
| 59 | { |
| 60 | int rc; |
| 61 | struct stats_reporter *srep = osmo_stats_vty2srep(vty); |
| 62 | OSMO_ASSERT(srep); |
| 63 | |
| 64 | rc = fun(srep, val); |
| 65 | if (rc < 0) { |
| 66 | vty_out(vty, "%% Unable to set %s: %s%s", |
| 67 | param_name, strerror(-rc), VTY_NEWLINE); |
| 68 | return CMD_WARNING; |
| 69 | } |
| 70 | |
| 71 | return CMD_SUCCESS; |
| 72 | } |
| 73 | |
| 74 | static int set_srep_parameter_int(struct vty *vty, |
| 75 | int (*fun)(struct stats_reporter *, int), |
| 76 | const char *val, const char *param_name) |
| 77 | { |
| 78 | int rc; |
| 79 | int int_val; |
| 80 | struct stats_reporter *srep = osmo_stats_vty2srep(vty); |
| 81 | OSMO_ASSERT(srep); |
| 82 | |
| 83 | int_val = atoi(val); |
| 84 | |
| 85 | rc = fun(srep, int_val); |
| 86 | if (rc < 0) { |
| 87 | vty_out(vty, "%% Unable to set %s: %s%s", |
| 88 | param_name, strerror(-rc), VTY_NEWLINE); |
| 89 | return CMD_WARNING; |
| 90 | } |
| 91 | |
| 92 | return CMD_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | DEFUN(cfg_stats_reporter_local_ip, cfg_stats_reporter_local_ip_cmd, |
| 96 | "local-ip ADDR", |
| 97 | "Set the IP address to which we bind locally\n" |
| 98 | "IP Address\n") |
| 99 | { |
| 100 | return set_srep_parameter_str(vty, stats_reporter_set_local_addr, |
| 101 | argv[0], "local address"); |
| 102 | } |
| 103 | |
| 104 | DEFUN(cfg_no_stats_reporter_local_ip, cfg_no_stats_reporter_local_ip_cmd, |
| 105 | "no local-ip", |
| 106 | NO_STR |
| 107 | "Set the IP address to which we bind locally\n") |
| 108 | { |
| 109 | return set_srep_parameter_str(vty, stats_reporter_set_local_addr, |
| 110 | NULL, "local address"); |
| 111 | } |
| 112 | |
| 113 | DEFUN(cfg_stats_reporter_remote_ip, cfg_stats_reporter_remote_ip_cmd, |
| 114 | "remote-ip ADDR", |
| 115 | "Set the remote IP address to which we connect\n" |
| 116 | "IP Address\n") |
| 117 | { |
| 118 | return set_srep_parameter_str(vty, stats_reporter_set_remote_addr, |
| 119 | argv[0], "remote address"); |
| 120 | } |
| 121 | |
| 122 | DEFUN(cfg_stats_reporter_remote_port, cfg_stats_reporter_remote_port_cmd, |
| 123 | "remote-port <1-65535>", |
| 124 | "Set the remote port to which we connect\n" |
| 125 | "Remote port number\n") |
| 126 | { |
| 127 | return set_srep_parameter_int(vty, stats_reporter_set_remote_port, |
| 128 | argv[0], "remote port"); |
| 129 | } |
| 130 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 131 | DEFUN(cfg_stats_reporter_mtu, cfg_stats_reporter_mtu_cmd, |
| 132 | "mtu <100-65535>", |
| 133 | "Set the maximum packet size\n" |
| 134 | "Size in byte\n") |
| 135 | { |
| 136 | return set_srep_parameter_int(vty, stats_reporter_set_mtu, |
| 137 | argv[0], "mtu"); |
| 138 | } |
| 139 | |
| 140 | DEFUN(cfg_no_stats_reporter_mtu, cfg_no_stats_reporter_mtu_cmd, |
| 141 | "no mtu", |
| 142 | NO_STR "Set the maximum packet size\n") |
| 143 | { |
| 144 | return set_srep_parameter_int(vty, stats_reporter_set_mtu, |
| 145 | 0, "mtu"); |
| 146 | } |
| 147 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 148 | DEFUN(cfg_stats_reporter_prefix, cfg_stats_reporter_prefix_cmd, |
| 149 | "prefix PREFIX", |
| 150 | "Set the item name prefix\n" |
| 151 | "The prefix string\n") |
| 152 | { |
| 153 | return set_srep_parameter_str(vty, stats_reporter_set_name_prefix, |
| 154 | argv[0], "prefix string"); |
| 155 | } |
| 156 | |
| 157 | DEFUN(cfg_no_stats_reporter_prefix, cfg_no_stats_reporter_prefix_cmd, |
| 158 | "no prefix", |
| 159 | NO_STR |
| 160 | "Set the item name prefix\n") |
| 161 | { |
| 162 | return set_srep_parameter_str(vty, stats_reporter_set_name_prefix, |
| 163 | "", "prefix string"); |
| 164 | } |
| 165 | |
| 166 | DEFUN(cfg_stats_reporter_enable, cfg_stats_reporter_enable_cmd, |
| 167 | "enable", |
| 168 | "Enable the reporter\n") |
| 169 | { |
| 170 | int rc; |
| 171 | struct stats_reporter *srep = osmo_stats_vty2srep(vty); |
| 172 | OSMO_ASSERT(srep); |
| 173 | |
| 174 | rc = stats_reporter_enable(srep); |
| 175 | if (rc < 0) { |
| 176 | vty_out(vty, "%% Unable to enable the reporter: %s%s", |
| 177 | strerror(-rc), VTY_NEWLINE); |
| 178 | return CMD_WARNING; |
| 179 | } |
| 180 | |
| 181 | return CMD_SUCCESS; |
| 182 | } |
| 183 | |
| 184 | DEFUN(cfg_stats_reporter_disable, cfg_stats_reporter_disable_cmd, |
| 185 | "disable", |
| 186 | "Disable the reporter\n") |
| 187 | { |
| 188 | int rc; |
| 189 | struct stats_reporter *srep = osmo_stats_vty2srep(vty); |
| 190 | OSMO_ASSERT(srep); |
| 191 | |
| 192 | rc = stats_reporter_disable(srep); |
| 193 | if (rc < 0) { |
| 194 | vty_out(vty, "%% Unable to disable the reporter: %s%s", |
| 195 | strerror(-rc), VTY_NEWLINE); |
| 196 | return CMD_WARNING; |
| 197 | } |
| 198 | |
| 199 | return CMD_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd, |
| 203 | "stats reporter statsd", |
| 204 | CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n") |
| 205 | { |
| 206 | struct stats_reporter *srep; |
| 207 | |
| 208 | srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL); |
| 209 | if (!srep) { |
| 210 | srep = stats_reporter_create_statsd(NULL); |
| 211 | if (!srep) { |
| 212 | vty_out(vty, "%% Unable to create statsd reporter%s", |
| 213 | VTY_NEWLINE); |
| 214 | return CMD_WARNING; |
| 215 | } |
| 216 | /* TODO: if needed, add stats_add_reporter(srep); */ |
| 217 | } |
| 218 | |
| 219 | vty->index = srep; |
| 220 | vty->node = CFG_STATS_NODE; |
| 221 | |
| 222 | return CMD_SUCCESS; |
| 223 | } |
| 224 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 225 | DEFUN(cfg_stats_interval, cfg_stats_interval_cmd, |
| 226 | "stats interval <1-65535>", |
| 227 | CFG_STATS_STR "Set the reporting interval\n" |
| 228 | "Interval in seconds\n") |
| 229 | { |
| 230 | int rc; |
| 231 | int interval = atoi(argv[0]); |
| 232 | rc = stats_set_interval(interval); |
| 233 | if (rc < 0) { |
| 234 | vty_out(vty, "%% Unable to set interval: %s%s", |
| 235 | strerror(-rc), VTY_NEWLINE); |
| 236 | return CMD_WARNING; |
| 237 | } |
| 238 | |
| 239 | return CMD_SUCCESS; |
| 240 | } |
| 241 | |
| 242 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 243 | DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd, |
| 244 | "no stats reporter statsd", |
| 245 | NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n") |
| 246 | { |
| 247 | struct stats_reporter *srep; |
| 248 | |
| 249 | srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL); |
| 250 | if (!srep) { |
| 251 | vty_out(vty, "%% No statsd logging active%s", |
| 252 | VTY_NEWLINE); |
| 253 | return CMD_WARNING; |
| 254 | } |
| 255 | |
| 256 | stats_reporter_free(srep); |
| 257 | |
| 258 | return CMD_SUCCESS; |
| 259 | } |
| 260 | |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 261 | DEFUN(show_stats, |
| 262 | show_stats_cmd, |
| 263 | "show stats", |
| 264 | SHOW_STR SHOW_STATS_STR) |
| 265 | { |
| 266 | vty_out_statistics_full(vty, ""); |
| 267 | |
| 268 | return CMD_SUCCESS; |
| 269 | } |
| 270 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 271 | static int config_write_stats_reporter(struct vty *vty, struct stats_reporter *srep) |
| 272 | { |
| 273 | if (srep == NULL) |
| 274 | return 0; |
| 275 | |
| 276 | switch (srep->type) { |
| 277 | case STATS_REPORTER_STATSD: |
| 278 | vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | vty_out(vty, " disable%s", VTY_NEWLINE); |
| 283 | |
| 284 | if (srep->dest_addr_str) |
| 285 | vty_out(vty, " remote-ip %s%s", |
| 286 | srep->dest_addr_str, VTY_NEWLINE); |
| 287 | if (srep->dest_port) |
| 288 | vty_out(vty, " remote-port %d%s", |
| 289 | srep->dest_port, VTY_NEWLINE); |
| 290 | if (srep->bind_addr_str) |
| 291 | vty_out(vty, " local-ip %s%s", |
| 292 | srep->bind_addr_str, VTY_NEWLINE); |
Jacob Erlbeck | 4aa1177 | 2015-10-27 11:55:20 +0100 | [diff] [blame^] | 293 | if (srep->mtu) |
| 294 | vty_out(vty, " mtu %d%s", |
| 295 | srep->mtu, VTY_NEWLINE); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 296 | if (srep->name_prefix && *srep->name_prefix) |
| 297 | vty_out(vty, " prefix %s%s", |
| 298 | srep->name_prefix, VTY_NEWLINE); |
| 299 | else |
| 300 | vty_out(vty, " no prefix%s", VTY_NEWLINE); |
| 301 | |
| 302 | if (srep->enabled) |
| 303 | vty_out(vty, " enable%s", VTY_NEWLINE); |
| 304 | |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | static int config_write_stats(struct vty *vty) |
| 309 | { |
| 310 | struct stats_reporter *srep; |
| 311 | |
| 312 | srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL); |
| 313 | config_write_stats_reporter(vty, srep); |
| 314 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 315 | vty_out(vty, "stats interval %d%s", stats_config->interval, VTY_NEWLINE); |
| 316 | |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | void stats_vty_add_cmds() |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 321 | { |
| 322 | install_element_ve(&show_stats_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 323 | |
| 324 | install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd); |
| 325 | install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 326 | install_element(CONFIG_NODE, &cfg_stats_interval_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 327 | |
| 328 | install_node(&cfg_stats_node, config_write_stats); |
| 329 | vty_install_default(CFG_STATS_NODE); |
| 330 | |
| 331 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd); |
| 332 | install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd); |
| 333 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd); |
| 334 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 335 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_mtu_cmd); |
| 336 | install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_mtu_cmd); |
Jacob Erlbeck | adc900e | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 337 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd); |
| 338 | install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd); |
| 339 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd); |
| 340 | install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd); |
Jacob Erlbeck | 45513e6 | 2015-10-19 15:14:13 +0200 | [diff] [blame] | 341 | } |