diff options
Diffstat (limited to 'src/vty/stats_vty.c')
-rw-r--r-- | src/vty/stats_vty.c | 281 |
1 files changed, 195 insertions, 86 deletions
diff --git a/src/vty/stats_vty.c b/src/vty/stats_vty.c index 46282817..f940018f 100644 --- a/src/vty/stats_vty.c +++ b/src/vty/stats_vty.c @@ -1,5 +1,5 @@ /* - * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> + * (C) 2009-2022 by Harald Welte <laforge@gnumonks.org> * (C) 2009-2014 by Holger Hans Peter Freyther * (C) 2015 by sysmocom - s.f.m.c. GmbH * All Rights Reserved @@ -16,16 +16,12 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * */ #include <stdlib.h> #include <string.h> -#include "../../config.h" +#include "config.h" #include <osmocom/vty/command.h> #include <osmocom/vty/buffer.h> @@ -37,11 +33,15 @@ #include <osmocom/core/stats.h> #include <osmocom/core/counter.h> #include <osmocom/core/rate_ctr.h> +#include <osmocom/core/stats_tcp.h> #define CFG_STATS_STR "Configure stats sub-system\n" #define CFG_REPORTER_STR "Configure a stats reporter\n" #define SHOW_STATS_STR "Show statistical values\n" +#define SKIP_ZERO_STR "Skip items with total count zero\n" + +#define STATS_STR "Stats related commands\n" /*! \file stats_vty.c * VTY interface for statsd / statistic items @@ -267,14 +267,20 @@ DEFUN(cfg_stats_reporter_flush_period, cfg_stats_reporter_flush_period_cmd, } DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd, - "stats reporter statsd", - CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n") + "stats reporter statsd [NAME]", + CFG_STATS_STR CFG_REPORTER_STR + "Report to a STATSD server\n" + "Name of the reporter\n") { struct osmo_stats_reporter *srep; + const char *name = NULL; - srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); + if (argc > 0) + name = argv[0]; + + srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, name); if (!srep) { - srep = osmo_stats_reporter_create_statsd(NULL); + srep = osmo_stats_reporter_create_statsd(name); if (!srep) { vty_out(vty, "%% Unable to create statsd reporter%s", VTY_NEWLINE); @@ -291,15 +297,21 @@ DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd, } DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd, - "no stats reporter statsd", - NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n") + "no stats reporter statsd [NAME]", + NO_STR CFG_STATS_STR CFG_REPORTER_STR + "Report to a STATSD server\n" + "Name of the reporter\n") { struct osmo_stats_reporter *srep; + const char *name = NULL; + + if (argc > 0) + name = argv[0]; - srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); + srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, name); if (!srep) { - vty_out(vty, "%% No statsd logging active%s", - VTY_NEWLINE); + vty_out(vty, "%% There is no such statsd reporter with name '%s'%s", + name ? name : "", VTY_NEWLINE); return CMD_WARNING; } @@ -309,14 +321,20 @@ DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd, } DEFUN(cfg_stats_reporter_log, cfg_stats_reporter_log_cmd, - "stats reporter log", - CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n") + "stats reporter log [NAME]", + CFG_STATS_STR CFG_REPORTER_STR + "Report to the logger\n" + "Name of the reporter\n") { struct osmo_stats_reporter *srep; + const char *name = NULL; + + if (argc > 0) + name = argv[0]; - srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); + srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, name); if (!srep) { - srep = osmo_stats_reporter_create_log(NULL); + srep = osmo_stats_reporter_create_log(name); if (!srep) { vty_out(vty, "%% Unable to create log reporter%s", VTY_NEWLINE); @@ -333,15 +351,21 @@ DEFUN(cfg_stats_reporter_log, cfg_stats_reporter_log_cmd, } DEFUN(cfg_no_stats_reporter_log, cfg_no_stats_reporter_log_cmd, - "no stats reporter log", - NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n") + "no stats reporter log [NAME]", + NO_STR CFG_STATS_STR CFG_REPORTER_STR + "Report to the logger\n" + "Name of the reporter\n") { struct osmo_stats_reporter *srep; + const char *name = NULL; - srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); + if (argc > 0) + name = argv[0]; + + srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, name); if (!srep) { - vty_out(vty, "%% No log reporting active%s", - VTY_NEWLINE); + vty_out(vty, "%% There is no such log reporter with name '%s'%s", + name ? name : "", VTY_NEWLINE); return CMD_WARNING; } @@ -351,9 +375,9 @@ DEFUN(cfg_no_stats_reporter_log, cfg_no_stats_reporter_log_cmd, } DEFUN(cfg_stats_interval, cfg_stats_interval_cmd, - "stats interval <1-65535>", + "stats interval <0-65535>", CFG_STATS_STR "Set the reporting interval\n" - "Interval in seconds\n") + "Interval in seconds (0 disables the reporting interval)\n") { int rc; int interval = atoi(argv[0]); @@ -367,27 +391,60 @@ DEFUN(cfg_stats_interval, cfg_stats_interval_cmd, return CMD_SUCCESS; } +DEFUN(cfg_tcp_stats_interval, cfg_tcp_stats_interval_cmd, + "stats-tcp interval <0-65535>", + CFG_STATS_STR "Set the tcp socket stats polling interval\n" + "Interval in seconds (0 disables the polling interval)\n") +{ + int rc; + int interval = atoi(argv[0]); + rc = osmo_stats_tcp_set_interval(interval); + if (rc < 0) { + vty_out(vty, "%% Unable to set interval: %s%s", + strerror(-rc), VTY_NEWLINE); + return CMD_WARNING; + } + + return CMD_SUCCESS; +} + +DEFUN(cfg_tcp_stats_batch_size, cfg_tcp_stats_batch_size_cmd, + "stats-tcp batch-size <1-65535>", + CFG_STATS_STR "Set the number of tcp sockets that are processed per stats polling interval\n" + "Number of sockets per interval\n") +{ + osmo_tcp_stats_config->batch_size = atoi(argv[0]); + return CMD_SUCCESS; +} + DEFUN(show_stats, show_stats_cmd, - "show stats", - SHOW_STR SHOW_STATS_STR) + "show stats [skip-zero]", + SHOW_STR SHOW_STATS_STR SKIP_ZERO_STR) { - vty_out_statistics_full(vty, ""); + bool skip_zero = false; + if (argc > 0) + skip_zero = true; + + vty_out_statistics_full2(vty, "", skip_zero); return CMD_SUCCESS; } DEFUN(show_stats_level, show_stats_level_cmd, - "show stats level (global|peer|subscriber)", + "show stats level (global|peer|subscriber) [skip-zero]", SHOW_STR SHOW_STATS_STR "Set the maximum group level\n" "Show global groups only\n" "Show global and network peer related groups\n" - "Show global, peer, and subscriber groups\n") + "Show global, peer, and subscriber groups\n" SKIP_ZERO_STR) { int level = get_string_value(stats_class_strs, argv[0]); - vty_out_statistics_partial(vty, "", level); + bool skip_zero = false; + if (argc > 1) + skip_zero = true; + vty_out_statistics_partial2(vty, "", level, skip_zero); return CMD_SUCCESS; } @@ -413,13 +470,6 @@ static int asciidoc_handle_counter(struct osmo_counter *counter, void *sctx_) static void asciidoc_counter_generate(struct vty *vty) { - if (osmo_counters_count() == 0) - { - vty_out(vty, "// there are no ungrouped osmo_counters%s", - VTY_NEWLINE); - return; - } - vty_out(vty, "// ungrouped osmo_counters%s", VTY_NEWLINE); vty_out(vty, ".ungrouped osmo counters%s", VTY_NEWLINE); vty_out(vty, "[options=\"header\"]%s", VTY_NEWLINE); @@ -480,10 +530,11 @@ static int asciidoc_osmo_stat_item_handler( { struct vty *vty = sctx_; - char *name = osmo_asciidoc_escape(item->desc->name); - char *description = osmo_asciidoc_escape(item->desc->description); + const struct osmo_stat_item_desc *desc = osmo_stat_item_get_desc(item); + char *name = osmo_asciidoc_escape(desc->name); + char *description = osmo_asciidoc_escape(desc->description); char *group_name_prefix = osmo_asciidoc_escape(statg->desc->group_name_prefix); - char *unit = osmo_asciidoc_escape(item->desc->unit); + char *unit = osmo_asciidoc_escape(desc->unit); /* | Name | Reference | Description | Unit | */ vty_out(vty, "| %s | <<%s_%s>> | %s | %s%s", @@ -540,48 +591,97 @@ DEFUN(show_stats_asciidoc_table, vty_out(vty, "// generating tables for rate_ctr_group%s", VTY_NEWLINE); rate_ctr_for_each_group(asciidoc_rate_ctr_group_handler, vty); - vty_out(vty, "== Osmo Stat Items%s%s", VTY_NEWLINE, VTY_NEWLINE); + vty_out(vty, "=== Osmo Stat Items%s%s", VTY_NEWLINE, VTY_NEWLINE); vty_out(vty, "// generating tables for osmo_stat_items%s", VTY_NEWLINE); osmo_stat_item_for_each_group(asciidoc_osmo_stat_item_group_handler, vty); - vty_out(vty, "== Osmo Counters%s%s", VTY_NEWLINE, VTY_NEWLINE); - vty_out(vty, "// generating tables for osmo_counters%s", VTY_NEWLINE); - asciidoc_counter_generate(vty); + if (osmo_counters_count() == 0) + { + vty_out(vty, "// there are no ungrouped osmo_counters%s", + VTY_NEWLINE); + } else { + vty_out(vty, "=== Osmo Counters%s%s", VTY_NEWLINE, VTY_NEWLINE); + vty_out(vty, "// generating tables for osmo_counters%s", VTY_NEWLINE); + asciidoc_counter_generate(vty); + } return CMD_SUCCESS; } +struct rctr_vty_ctx { + struct vty *vty; + bool skip_zero; +}; + static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) { - struct vty *vty = sctx_; - vty_out(vty, "%s %u:%s", ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE); - vty_out_rate_ctr_group_fmt(vty, "%25n: %10c (%S/s %M/m %H/h %D/d) %d", ctrg); + struct rctr_vty_ctx *sctx = sctx_; + struct vty *vty = sctx->vty; + vty_out(vty, "%s %u", ctrg->desc->group_description, ctrg->idx); + if (ctrg->name != NULL) + vty_out(vty, " (%s)", ctrg->name); + vty_out(vty, ":%s", VTY_NEWLINE); + vty_out_rate_ctr_group_fmt2(vty, "%25n: %10c (%S/s %M/m %H/h %D/d) %d", ctrg, sctx->skip_zero); return 0; } DEFUN(show_rate_counters, show_rate_counters_cmd, - "show rate-counters", - SHOW_STR "Show all rate counters\n") + "show rate-counters [skip-zero]", + SHOW_STR "Show all rate counters\n" SKIP_ZERO_STR) +{ + struct rctr_vty_ctx rctx = { .vty = vty, .skip_zero = false }; + if (argc > 0) + rctx.skip_zero = true; + rate_ctr_for_each_group(rate_ctr_group_handler, &rctx); + return CMD_SUCCESS; +} + +DEFUN(stats_report, + stats_report_cmd, + "stats report", + STATS_STR "Manurally trigger reporting of stats\n") +{ + osmo_stats_report(); + return CMD_SUCCESS; +} + +static int reset_rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) +{ + rate_ctr_group_reset(ctrg); + return 0; +} + +DEFUN(stats_reset, + stats_reset_cmd, + "stats reset", + STATS_STR "Reset all rate counter stats\n") { - rate_ctr_for_each_group(rate_ctr_group_handler, vty); + rate_ctr_for_each_group(reset_rate_ctr_group_handler, NULL); return CMD_SUCCESS; } static int config_write_stats_reporter(struct vty *vty, struct osmo_stats_reporter *srep) { - if (srep == NULL) - return 0; + const char *type = NULL; switch (srep->type) { case OSMO_STATS_REPORTER_STATSD: - vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE); + type = "statsd"; break; case OSMO_STATS_REPORTER_LOG: - vty_out(vty, "stats reporter log%s", VTY_NEWLINE); + type = "log"; break; + default: + /* don't try to save unknown stats reporters to the VTY. Imagine some + * application registering a new application specific stats reporter that + * this VTY code knows nothing about! */ + return 0; } - vty_out(vty, " disable%s", VTY_NEWLINE); + vty_out(vty, "stats reporter %s", type); + if (srep->name != NULL) + vty_out(vty, " %s", srep->name); + vty_out(vty, "%s", VTY_NEWLINE); if (srep->have_net_config) { if (srep->dest_addr_str) @@ -615,6 +715,8 @@ static int config_write_stats_reporter(struct vty *vty, struct osmo_stats_report if (srep->enabled) vty_out(vty, " enable%s", VTY_NEWLINE); + else + vty_out(vty, " disable%s", VTY_NEWLINE); return 1; } @@ -623,13 +725,15 @@ static int config_write_stats(struct vty *vty) { struct osmo_stats_reporter *srep; - /* TODO: loop through all reporters */ - srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); - config_write_stats_reporter(vty, srep); - srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); - config_write_stats_reporter(vty, srep); - vty_out(vty, "stats interval %d%s", osmo_stats_config->interval, VTY_NEWLINE); + if (osmo_tcp_stats_config->interval != TCP_STATS_DEFAULT_INTERVAL) + vty_out(vty, "stats-tcp interval %d%s", osmo_tcp_stats_config->interval, VTY_NEWLINE); + if (osmo_tcp_stats_config->batch_size != TCP_STATS_DEFAULT_BATCH_SIZE) + vty_out(vty, "stats-tcp batch-size %d%s", osmo_tcp_stats_config->batch_size, VTY_NEWLINE); + + /* Loop through all reporters */ + llist_for_each_entry(srep, &osmo_stats_reporter_list, list) + config_write_stats_reporter(vty, srep); return 1; } @@ -638,32 +742,37 @@ static int config_write_stats(struct vty *vty) * Call this once during your application initialization if you would * like to have stats VTY commands enabled. */ -void osmo_stats_vty_add_cmds() +void osmo_stats_vty_add_cmds(void) { - install_element_ve(&show_stats_cmd); - install_element_ve(&show_stats_level_cmd); + install_lib_element_ve(&show_stats_cmd); + install_lib_element_ve(&show_stats_level_cmd); - install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd); - install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd); - install_element(CONFIG_NODE, &cfg_stats_reporter_log_cmd); - install_element(CONFIG_NODE, &cfg_no_stats_reporter_log_cmd); - install_element(CONFIG_NODE, &cfg_stats_interval_cmd); + install_lib_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd); + install_lib_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd); + install_lib_element(CONFIG_NODE, &cfg_stats_reporter_log_cmd); + install_lib_element(CONFIG_NODE, &cfg_no_stats_reporter_log_cmd); + install_lib_element(CONFIG_NODE, &cfg_stats_interval_cmd); + install_lib_element(CONFIG_NODE, &cfg_tcp_stats_interval_cmd); + install_lib_element(CONFIG_NODE, &cfg_tcp_stats_batch_size_cmd); install_node(&cfg_stats_node, config_write_stats); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd); - install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_mtu_cmd); - install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_mtu_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd); - install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_level_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd); - install_element(CFG_STATS_NODE, &cfg_stats_reporter_flush_period_cmd); - - install_element_ve(&show_stats_asciidoc_table_cmd); - install_element_ve(&show_rate_counters_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_mtu_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_no_stats_reporter_mtu_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_level_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd); + install_lib_element(CFG_STATS_NODE, &cfg_stats_reporter_flush_period_cmd); + + install_lib_element_ve(&show_stats_asciidoc_table_cmd); + install_lib_element_ve(&show_rate_counters_cmd); + + install_lib_element(ENABLE_NODE, &stats_report_cmd); + install_lib_element(ENABLE_NODE, &stats_reset_cmd); } |