diff options
-rw-r--r-- | include/osmocom/core/stats.h | 3 | ||||
-rw-r--r-- | src/stats.c | 63 | ||||
-rw-r--r-- | src/vty/stats_vty.c | 49 |
3 files changed, 115 insertions, 0 deletions
diff --git a/include/osmocom/core/stats.h b/include/osmocom/core/stats.h index beeee16e..68c2e016 100644 --- a/include/osmocom/core/stats.h +++ b/include/osmocom/core/stats.h @@ -30,6 +30,7 @@ struct rate_ctr_desc; enum stats_reporter_type { STATS_REPORTER_STATSD, + STATS_REPORTER_LOG, }; struct stats_reporter { @@ -83,7 +84,9 @@ int stats_set_interval(int interval); struct stats_reporter *stats_reporter_alloc(enum stats_reporter_type type, const char *name); void stats_reporter_free(struct stats_reporter *srep); + struct stats_reporter *stats_reporter_create_statsd(const char *name); +struct stats_reporter *stats_reporter_create_log(const char *name); struct stats_reporter *stats_reporter_find(enum stats_reporter_type type, const char *name); diff --git a/src/stats.c b/src/stats.c index f4c0b623..5027a621 100644 --- a/src/stats.c +++ b/src/stats.c @@ -68,6 +68,14 @@ static int stats_reporter_statsd_send_item(struct stats_reporter *srep, const struct stat_item_group *statg, const struct stat_item_desc *desc, int value); +static int stats_reporter_log_send_counter(struct stats_reporter *srep, + const struct rate_ctr_group *ctrg, + const struct rate_ctr_desc *desc, + int64_t value, int64_t delta); +static int stats_reporter_log_send_item(struct stats_reporter *srep, + const struct stat_item_group *statg, + const struct stat_item_desc *desc, int value); + static int stats_reporter_send(struct stats_reporter *srep, const char *data, int data_len); static int stats_reporter_send_buffer(struct stats_reporter *srep); @@ -309,6 +317,61 @@ static int stats_reporter_send_buffer(struct stats_reporter *srep) return rc; } +/*** log reporter ***/ + +struct stats_reporter *stats_reporter_create_log(const char *name) +{ + struct stats_reporter *srep; + srep = stats_reporter_alloc(STATS_REPORTER_LOG, name); + + srep->have_net_config = 0; + + srep->send_counter = stats_reporter_log_send_counter; + srep->send_item = stats_reporter_log_send_item; + + return srep; +} + +static int stats_reporter_log_send(struct stats_reporter *srep, + const char *type, + const char *name1, int index1, const char *name2, int value, + const char *unit) +{ + LOGP(DSTATS, LOGL_INFO, + "stats t=%s p=%s g=%s i=%d n=%s v=%d u=%s\n", + type, srep->name_prefix ? srep->name_prefix : "", + name1 ? name1 : "", index1, + name2, value, unit ? unit : ""); + + return 0; +} + + +static int stats_reporter_log_send_counter(struct stats_reporter *srep, + const struct rate_ctr_group *ctrg, + const struct rate_ctr_desc *desc, + int64_t value, int64_t delta) +{ + if (ctrg) + return stats_reporter_log_send(srep, "c", + ctrg->desc->group_name_prefix, + ctrg->idx, + desc->name, value, NULL); + else + return stats_reporter_log_send(srep, "c", + NULL, -1, + desc->name, value, NULL); +} + +static int stats_reporter_log_send_item(struct stats_reporter *srep, + const struct stat_item_group *statg, + const struct stat_item_desc *desc, int value) +{ + return stats_reporter_log_send(srep, "i", + statg->desc->group_name_prefix, statg->idx, + desc->name, value, desc->unit); +} + /*** statsd reporter ***/ struct stats_reporter *stats_reporter_create_statsd(const char *name) diff --git a/src/vty/stats_vty.c b/src/vty/stats_vty.c index 18ad2835..839dc829 100644 --- a/src/vty/stats_vty.c +++ b/src/vty/stats_vty.c @@ -258,6 +258,47 @@ DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd, return CMD_SUCCESS; } +DEFUN(cfg_stats_reporter_log, cfg_stats_reporter_log_cmd, + "stats reporter log", + CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n") +{ + struct stats_reporter *srep; + + srep = stats_reporter_find(STATS_REPORTER_LOG, NULL); + if (!srep) { + srep = stats_reporter_create_log(NULL); + if (!srep) { + vty_out(vty, "%% Unable to create log reporter%s", + VTY_NEWLINE); + return CMD_WARNING; + } + /* TODO: if needed, add stats_add_reporter(srep); */ + } + + vty->index = srep; + vty->node = CFG_STATS_NODE; + + return CMD_SUCCESS; +} + +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") +{ + struct stats_reporter *srep; + + srep = stats_reporter_find(STATS_REPORTER_LOG, NULL); + if (!srep) { + vty_out(vty, "%% No log reporting active%s", + VTY_NEWLINE); + return CMD_WARNING; + } + + stats_reporter_free(srep); + + return CMD_SUCCESS; +} + DEFUN(show_stats, show_stats_cmd, "show stats", @@ -277,6 +318,9 @@ static int config_write_stats_reporter(struct vty *vty, struct stats_reporter *s case STATS_REPORTER_STATSD: vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE); break; + case STATS_REPORTER_LOG: + vty_out(vty, "stats reporter log%s", VTY_NEWLINE); + break; } vty_out(vty, " disable%s", VTY_NEWLINE); @@ -312,8 +356,11 @@ static int config_write_stats(struct vty *vty) { struct stats_reporter *srep; + /* TODO: loop through all reporters */ srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL); config_write_stats_reporter(vty, srep); + srep = stats_reporter_find(STATS_REPORTER_LOG, NULL); + config_write_stats_reporter(vty, srep); vty_out(vty, "stats interval %d%s", stats_config->interval, VTY_NEWLINE); @@ -326,6 +373,8 @@ void stats_vty_add_cmds() 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_node(&cfg_stats_node, config_write_stats); |