Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file stats.c */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 2 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | * (C) 2015 by sysmocom - s.f.m.c. GmbH |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 4 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 21 | /*! \addtogroup stats |
| 22 | * @{ |
| 23 | * |
| 24 | * This module implements periodic reporting of statistics / counters. |
| 25 | * It supports the notion of multiple \ref osmo_stats_reporter objects |
| 26 | * which independently of each other can report statistics at different |
| 27 | * configurable intervals to different destinations. |
| 28 | * |
| 29 | * In order to use this facility, you have to call \ref |
| 30 | * osmo_stats_init() once at application start-up and then create one or |
| 31 | * more \ref osmo_stats_reporter, either using the direct API functions |
| 32 | * or by using the optional VTY bindings: |
| 33 | * |
| 34 | * - reporting to any of the libosmocore log targets |
| 35 | * \ref osmo_stats_reporter_create_log() creates a new stats_reporter |
| 36 | * which reports to the libosmcoore \ref logging subsystem. |
| 37 | * |
| 38 | * - reporting to statsd (a front-end proxy for the Graphite/Carbon |
| 39 | * metrics server |
| 40 | * \ref osmo_stats_reporter_create_statsd() creates a new stats_reporter |
| 41 | * which reports via UDP to statsd. |
| 42 | * |
| 43 | * You can either use the above API functions directly to create \ref |
| 44 | * osmo_stats_reporter instances, or you can use the VTY support |
| 45 | * contained in libosmovty. See the "stats" configuration node |
| 46 | * installed by osmo_stats_vty_Add_cmds(). |
| 47 | * |
| 48 | * An \ref osmo_stats_reporter reports statistics on all of the following |
| 49 | * libosmocore internal counter/statistics objects: |
| 50 | * - \ref osmo_counter |
| 51 | * - \ref rate_ctr |
| 52 | * - \ref osmo_stat_item |
| 53 | * |
| 54 | * You do not need to do anything in particular to expose a given |
| 55 | * counter or stat_item, they are all exported automatically via any |
| 56 | * \ref osmo_stats_reporter. If you have multiple \ref |
| 57 | * osmo_stats_reporter, they will each report all counters/stat_items. |
| 58 | * |
| 59 | * \file stats.c */ |
| 60 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 61 | #include "config.h" |
| 62 | #if !defined(EMBEDDED) |
| 63 | |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 64 | #include <osmocom/core/byteswap.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 65 | #include <osmocom/core/stats.h> |
| 66 | |
| 67 | #include <unistd.h> |
| 68 | #include <string.h> |
| 69 | #include <stdint.h> |
| 70 | #include <errno.h> |
| 71 | #include <stdio.h> |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 72 | #include <sys/types.h> |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 73 | #include <inttypes.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 74 | |
| 75 | #ifdef HAVE_SYS_SOCKET_H |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 76 | #include <sys/socket.h> |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 77 | #include <netinet/in.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 78 | #include <arpa/inet.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 79 | #endif |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 80 | |
| 81 | #include <osmocom/core/utils.h> |
| 82 | #include <osmocom/core/logging.h> |
| 83 | #include <osmocom/core/rate_ctr.h> |
| 84 | #include <osmocom/core/stat_item.h> |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 85 | #include <osmocom/core/select.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 86 | #include <osmocom/core/counter.h> |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 87 | #include <osmocom/core/msgb.h> |
Philipp Maier | b1ef8f5 | 2021-12-06 16:31:02 +0100 | [diff] [blame] | 88 | #include <osmocom/core/stats_tcp.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 89 | |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 90 | #ifdef HAVE_SYSTEMTAP |
| 91 | /* include the generated probes header and put markers in code */ |
| 92 | #include "probes.h" |
| 93 | #define TRACE(probe) probe |
| 94 | #define TRACE_ENABLED(probe) probe ## _ENABLED() |
| 95 | #else |
| 96 | /* Wrap the probe to allow it to be removed when no systemtap available */ |
| 97 | #define TRACE(probe) |
| 98 | #define TRACE_ENABLED(probe) (0) |
| 99 | #endif /* HAVE_SYSTEMTAP */ |
| 100 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 101 | #include <stat_item_internal.h> |
| 102 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 103 | #define STATS_DEFAULT_INTERVAL 5 /* secs */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 104 | #define STATS_DEFAULT_BUFLEN 256 |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 105 | |
Vadim Yanitskiy | bfc8377 | 2021-11-08 23:14:29 +0300 | [diff] [blame] | 106 | LLIST_HEAD(osmo_stats_reporter_list); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | static void *osmo_stats_ctx = NULL; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 108 | static int is_initialised = 0; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 109 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 110 | static struct osmo_stats_config s_stats_config = { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 111 | .interval = STATS_DEFAULT_INTERVAL, |
| 112 | }; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 113 | struct osmo_stats_config *osmo_stats_config = &s_stats_config; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 114 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 115 | static struct osmo_fd osmo_stats_timer = { .fd = -1 }; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 116 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 117 | static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 118 | const struct rate_ctr_group *ctrg, |
| 119 | const struct rate_ctr_desc *desc, |
| 120 | int64_t value, int64_t delta); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 121 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 122 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 123 | const struct osmo_stat_item_desc *desc, int64_t value); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 124 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 125 | static int update_srep_config(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 126 | { |
| 127 | int rc = 0; |
| 128 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 129 | if (srep->running) { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 130 | if (srep->close) |
| 131 | rc = srep->close(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 132 | srep->running = 0; |
| 133 | } |
| 134 | |
| 135 | if (!srep->enabled) |
| 136 | return rc; |
| 137 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 138 | if (srep->open) |
| 139 | rc = srep->open(srep); |
| 140 | else |
| 141 | rc = 0; |
| 142 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 143 | if (rc < 0) |
| 144 | srep->enabled = 0; |
| 145 | else |
| 146 | srep->running = 1; |
| 147 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 148 | srep->force_single_flush = 1; |
| 149 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 150 | return rc; |
| 151 | } |
| 152 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 153 | static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 154 | { |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 155 | uint64_t expire_count; |
| 156 | int rc; |
| 157 | |
| 158 | /* check that the timer has actually expired */ |
| 159 | if (!(what & OSMO_FD_READ)) |
| 160 | return 0; |
| 161 | |
| 162 | /* read from timerfd: number of expirations of periodic timer */ |
| 163 | rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count)); |
| 164 | if (rc < 0 && errno == EAGAIN) |
| 165 | return 0; |
| 166 | OSMO_ASSERT(rc == sizeof(expire_count)); |
| 167 | |
| 168 | if (expire_count > 1) |
| 169 | LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n", |
| 170 | expire_count, expire_count-1); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 171 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 172 | if (!llist_empty(&osmo_stats_reporter_list)) |
| 173 | osmo_stats_report(); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 174 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 175 | return 0; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Harald Welte | 1e1436c | 2022-05-08 09:57:04 +0200 | [diff] [blame] | 178 | static int start_timer(void) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 179 | { |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 180 | int rc; |
| 181 | int interval = osmo_stats_config->interval; |
| 182 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 183 | if (!is_initialised) |
| 184 | return -ESRCH; |
| 185 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 186 | struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000}; |
| 187 | struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0}; |
| 188 | |
| 189 | rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL); |
| 190 | if (rc < 0) |
| 191 | LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n", |
| 192 | rc, osmo_stats_timer.fd); |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 193 | |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 194 | if (interval == 0) { |
| 195 | rc = osmo_timerfd_disable(&osmo_stats_timer); |
| 196 | if (rc < 0) |
| 197 | LOGP(DLSTATS, LOGL_ERROR, "Failed to disable the timer with error code %d (fd=%d)\n", |
| 198 | rc, osmo_stats_timer.fd); |
| 199 | } else { |
| 200 | |
| 201 | rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval); |
| 202 | if (rc < 0) |
| 203 | LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n", |
| 204 | rc, osmo_stats_timer.fd, interval); |
| 205 | |
| 206 | LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval); |
| 207 | } |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 212 | struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 213 | const char *name) |
| 214 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 215 | struct osmo_stats_reporter *srep; |
| 216 | srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter); |
Harald Welte | e8e24c7 | 2022-05-08 10:01:52 +0200 | [diff] [blame] | 217 | if (!srep) |
| 218 | return NULL; |
| 219 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 220 | srep->type = type; |
| 221 | if (name) |
| 222 | srep->name = talloc_strdup(srep, name); |
| 223 | srep->fd = -1; |
| 224 | |
Vadim Yanitskiy | 4e92472 | 2021-11-09 03:58:35 +0300 | [diff] [blame] | 225 | llist_add_tail(&srep->list, &osmo_stats_reporter_list); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 226 | |
| 227 | return srep; |
| 228 | } |
| 229 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 230 | /*! Destroy a given stats_reporter. Takes care of first disabling it. |
| 231 | * \param[in] srep stats_reporter that shall be disabled + destroyed */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 232 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 233 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 234 | osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 235 | llist_del(&srep->list); |
| 236 | talloc_free(srep); |
| 237 | } |
| 238 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 239 | /*! Initialize the stats reporting module; call this once in your program. |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 240 | * \param[in] ctx Talloc context from which stats related memory is allocated */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 241 | void osmo_stats_init(void *ctx) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 242 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 243 | osmo_stats_ctx = ctx; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 244 | is_initialised = 1; |
| 245 | start_timer(); |
Philipp Maier | b1ef8f5 | 2021-12-06 16:31:02 +0100 | [diff] [blame] | 246 | |
| 247 | /* Make sure that the tcp-stats interval timer also runs at its |
| 248 | * preconfigured rate. The vty might change this setting later. */ |
| 249 | osmo_stats_tcp_set_interval(osmo_tcp_stats_config->interval); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 252 | /*! Find a stats_reporter of given \a type and \a name. |
| 253 | * \param[in] type Type of stats_reporter to find |
| 254 | * \param[in] name Name of stats_reporter to find |
| 255 | * \returns stats_reporter matching \a type and \a name; NULL otherwise */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 256 | struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 257 | const char *name) |
| 258 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 259 | struct osmo_stats_reporter *srep; |
| 260 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 261 | if (srep->type != type) |
| 262 | continue; |
| 263 | if (srep->name != name) { |
| 264 | if (name == NULL || srep->name == NULL || |
| 265 | strcmp(name, srep->name) != 0) |
| 266 | continue; |
| 267 | } |
| 268 | return srep; |
| 269 | } |
| 270 | return NULL; |
| 271 | } |
| 272 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 273 | #ifdef HAVE_SYS_SOCKET_H |
| 274 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 275 | /*! Set the remote (IP) address of a given stats_reporter. |
| 276 | * \param[in] srep stats_reporter whose remote address is to be set |
| 277 | * \param[in] addr String representation of remote IPv4 address |
| 278 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 279 | int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 280 | { |
| 281 | int rc; |
| 282 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 283 | struct in_addr inaddr; |
| 284 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 285 | if (!srep->have_net_config) |
| 286 | return -ENOTSUP; |
| 287 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 288 | OSMO_ASSERT(addr != NULL); |
| 289 | |
| 290 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 291 | if (rc <= 0) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | sock_addr->sin_addr = inaddr; |
| 295 | sock_addr->sin_family = AF_INET; |
| 296 | srep->dest_addr_len = sizeof(*sock_addr); |
| 297 | |
| 298 | talloc_free(srep->dest_addr_str); |
| 299 | srep->dest_addr_str = talloc_strdup(srep, addr); |
| 300 | |
| 301 | return update_srep_config(srep); |
| 302 | } |
| 303 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 304 | /*! Set the remote (UDP) port of a given stats_reporter |
| 305 | * \param[in] srep stats_reporter whose remote address is to be set |
| 306 | * \param[in] port UDP port of remote statsd to which we report |
| 307 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 308 | int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 309 | { |
| 310 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 311 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 312 | if (!srep->have_net_config) |
| 313 | return -ENOTSUP; |
| 314 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 315 | srep->dest_port = port; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 316 | sock_addr->sin_port = osmo_htons(port); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 317 | |
| 318 | return update_srep_config(srep); |
| 319 | } |
| 320 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 321 | /*! Set the local (IP) address of a given stats_reporter. |
| 322 | * \param[in] srep stats_reporter whose remote address is to be set |
| 323 | * \param[in] addr String representation of local IP address |
| 324 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 325 | int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 326 | { |
| 327 | int rc; |
| 328 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr; |
| 329 | struct in_addr inaddr; |
| 330 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 331 | if (!srep->have_net_config) |
| 332 | return -ENOTSUP; |
| 333 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 334 | if (addr) { |
| 335 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 336 | if (rc <= 0) |
| 337 | return -EINVAL; |
| 338 | } else { |
Holger Hans Peter Freyther | 7921975 | 2015-11-02 15:50:32 +0100 | [diff] [blame] | 339 | inaddr.s_addr = INADDR_ANY; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | sock_addr->sin_addr = inaddr; |
| 343 | sock_addr->sin_family = AF_INET; |
| 344 | srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0; |
| 345 | |
| 346 | talloc_free(srep->bind_addr_str); |
| 347 | srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL; |
| 348 | |
| 349 | return update_srep_config(srep); |
| 350 | } |
| 351 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 352 | /*! Set the maximum transmission unit of a given stats_reporter. |
| 353 | * \param[in] srep stats_reporter whose remote address is to be set |
| 354 | * \param[in] mtu Maximum Transmission Unit of \a srep |
| 355 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 356 | int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 357 | { |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 358 | if (!srep->have_net_config) |
| 359 | return -ENOTSUP; |
| 360 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 361 | if (mtu < 0) |
| 362 | return -EINVAL; |
| 363 | |
| 364 | srep->mtu = mtu; |
| 365 | |
| 366 | return update_srep_config(srep); |
| 367 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 368 | #endif /* HAVE_SYS_SOCKETS_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 369 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 370 | int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, |
| 371 | enum osmo_stats_class class_id) |
| 372 | { |
| 373 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | srep->max_class = class_id; |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
Alexander Chemeris | f203ed3 | 2020-05-08 19:09:22 +0300 | [diff] [blame] | 381 | /*! Set the reporting interval (common for all reporters) |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 382 | * \param[in] interval Reporting interval in seconds |
| 383 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 384 | int osmo_stats_set_interval(int interval) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 385 | { |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 386 | if (interval < 0) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 387 | return -EINVAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 388 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 389 | osmo_stats_config->interval = interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 390 | if (is_initialised) |
| 391 | start_timer(); |
| 392 | |
| 393 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 396 | /*! Set the regular flush period for a given stats_reporter |
| 397 | * |
| 398 | * Send all stats even if they have not changed (i.e. force the flush) |
| 399 | * every N-th reporting interval. Set to 0 to disable regular flush, |
| 400 | * set to 1 to flush every time, set to 2 to flush every 2nd time, etc. |
| 401 | * \param[in] srep stats_reporter to set flush period for |
| 402 | * \param[in] period Reporting interval in seconds |
| 403 | * \returns 0 on success; negative on error */ |
| 404 | int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period) |
| 405 | { |
| 406 | srep->flush_period = period; |
| 407 | srep->flush_period_counter = 0; |
| 408 | /* force the flush now if it's not disabled by period=0 */ |
| 409 | if (period > 0) |
| 410 | srep->force_single_flush = 1; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 415 | /*! Set the name prefix of a given stats_reporter. |
| 416 | * \param[in] srep stats_reporter whose name prefix is to be set |
Philipp Maier | 2672895 | 2021-12-14 18:49:05 +0100 | [diff] [blame] | 417 | * \param[in] prefix Name prefix to pre-pend for any reported value |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 418 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 419 | int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 420 | { |
| 421 | talloc_free(srep->name_prefix); |
Jacob Erlbeck | 916423e | 2015-11-09 10:52:19 +0100 | [diff] [blame] | 422 | srep->name_prefix = prefix && strlen(prefix) > 0 ? |
| 423 | talloc_strdup(srep, prefix) : NULL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 424 | |
| 425 | return update_srep_config(srep); |
| 426 | } |
| 427 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 428 | |
| 429 | /*! Enable the given stats_reporter. |
| 430 | * \param[in] srep stats_reporter who is to be enabled |
| 431 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 432 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 433 | { |
| 434 | srep->enabled = 1; |
| 435 | |
| 436 | return update_srep_config(srep); |
| 437 | } |
| 438 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 439 | /*! Disable the given stats_reporter. |
| 440 | * \param[in] srep stats_reporter who is to be disabled |
| 441 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 442 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 443 | { |
| 444 | srep->enabled = 0; |
| 445 | |
| 446 | return update_srep_config(srep); |
| 447 | } |
| 448 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 449 | /*** i/o helper functions ***/ |
| 450 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 451 | #ifdef HAVE_SYS_SOCKET_H |
| 452 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 453 | /*! Open the UDP socket for given stats_reporter. |
| 454 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 455 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 456 | int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep) |
| 457 | { |
| 458 | int sock; |
| 459 | int rc; |
| 460 | int buffer_size = STATS_DEFAULT_BUFLEN; |
| 461 | |
| 462 | if (srep->fd != -1 && srep->close) |
| 463 | srep->close(srep); |
| 464 | |
| 465 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 466 | if (sock == -1) |
| 467 | return -errno; |
| 468 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 469 | #if defined(__APPLE__) && !defined(MSG_NOSIGNAL) |
| 470 | { |
| 471 | static int val = 1; |
| 472 | |
| 473 | rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val)); |
| 474 | goto failed; |
| 475 | } |
| 476 | #endif |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 477 | if (srep->bind_addr_len > 0) { |
| 478 | rc = bind(sock, &srep->bind_addr, srep->bind_addr_len); |
| 479 | if (rc == -1) |
| 480 | goto failed; |
| 481 | } |
| 482 | |
| 483 | srep->fd = sock; |
| 484 | |
| 485 | if (srep->mtu > 0) { |
| 486 | buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */; |
| 487 | srep->agg_enabled = 1; |
| 488 | } |
| 489 | |
| 490 | srep->buffer = msgb_alloc(buffer_size, "stats buffer"); |
Harald Welte | e8e24c7 | 2022-05-08 10:01:52 +0200 | [diff] [blame] | 491 | if (!srep->buffer) |
| 492 | goto failed; |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | |
| 496 | failed: |
| 497 | rc = -errno; |
| 498 | close(sock); |
| 499 | |
| 500 | return rc; |
| 501 | } |
| 502 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 503 | /*! Closee the UDP socket for given stats_reporter. |
| 504 | * \param[in] srep stats_reporter whose UDP socket is to be closed |
| 505 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 506 | int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep) |
| 507 | { |
| 508 | int rc; |
| 509 | if (srep->fd == -1) |
| 510 | return -EBADF; |
| 511 | |
| 512 | osmo_stats_reporter_send_buffer(srep); |
| 513 | |
| 514 | rc = close(srep->fd); |
| 515 | srep->fd = -1; |
| 516 | msgb_free(srep->buffer); |
| 517 | srep->buffer = NULL; |
| 518 | return rc == -1 ? -errno : 0; |
| 519 | } |
| 520 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 521 | /*! Send given date to given stats_reporter. |
| 522 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 523 | * \param[in] data string data to be sent |
| 524 | * \param[in] data_len Length of \a data in bytes |
| 525 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 526 | int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 527 | int data_len) |
| 528 | { |
| 529 | int rc; |
| 530 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 531 | rc = sendto(srep->fd, data, data_len, |
| 532 | #ifdef MSG_NOSIGNAL |
| 533 | MSG_NOSIGNAL | |
| 534 | #endif |
| 535 | MSG_DONTWAIT, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 536 | &srep->dest_addr, srep->dest_addr_len); |
| 537 | |
| 538 | if (rc == -1) |
| 539 | rc = -errno; |
| 540 | |
| 541 | return rc; |
| 542 | } |
| 543 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 544 | /*! Send current accumulated buffer to given stats_reporter. |
| 545 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 546 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 547 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 548 | { |
| 549 | int rc; |
| 550 | |
| 551 | if (!srep->buffer || msgb_length(srep->buffer) == 0) |
| 552 | return 0; |
| 553 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 554 | rc = osmo_stats_reporter_send(srep, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 555 | (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer)); |
| 556 | |
| 557 | msgb_trim(srep->buffer, 0); |
| 558 | |
| 559 | return rc; |
| 560 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 561 | #endif /* HAVE_SYS_SOCKET_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 562 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 563 | /*** log reporter ***/ |
| 564 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 565 | /*! Create a stats_reporter that logs via libosmocore logging. |
| 566 | * A stats_reporter created via this function will simply print the statistics |
| 567 | * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO |
| 568 | * priority. The configuration of the libosmocore log targets define where this |
| 569 | * information will end up (ignored, text file, stderr, syslog, ...). |
| 570 | * \param[in] name Name of the to-be-created stats_reporter |
| 571 | * \returns stats_reporter on success; NULL on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 572 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 573 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 574 | struct osmo_stats_reporter *srep; |
| 575 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name); |
Harald Welte | e8e24c7 | 2022-05-08 10:01:52 +0200 | [diff] [blame] | 576 | if (!srep) |
| 577 | return NULL; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 578 | |
| 579 | srep->have_net_config = 0; |
| 580 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 581 | srep->send_counter = osmo_stats_reporter_log_send_counter; |
| 582 | srep->send_item = osmo_stats_reporter_log_send_item; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 583 | |
| 584 | return srep; |
| 585 | } |
| 586 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 587 | static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 588 | const char *type, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 589 | const char *name1, unsigned int index1, const char *name2, int value, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 590 | const char *unit) |
| 591 | { |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 592 | LOGP(DLSTATS, LOGL_INFO, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 593 | "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 594 | type, srep->name_prefix ? srep->name_prefix : "", |
| 595 | name1 ? name1 : "", index1, |
| 596 | name2, value, unit ? unit : ""); |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 602 | static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 603 | const struct rate_ctr_group *ctrg, |
| 604 | const struct rate_ctr_desc *desc, |
| 605 | int64_t value, int64_t delta) |
| 606 | { |
| 607 | if (ctrg) |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 608 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 609 | ctrg->desc->group_name_prefix, |
| 610 | ctrg->idx, |
| 611 | desc->name, value, NULL); |
| 612 | else |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 613 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 614 | NULL, 0, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 615 | desc->name, value, NULL); |
| 616 | } |
| 617 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 618 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 619 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 620 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 621 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 622 | return osmo_stats_reporter_log_send(srep, "i", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 623 | statg->desc->group_name_prefix, statg->idx, |
| 624 | desc->name, value, desc->unit); |
| 625 | } |
| 626 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 627 | /*** helper for reporting ***/ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 628 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 629 | static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep, |
| 630 | unsigned int index, int class_id) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 631 | { |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 632 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 633 | class_id = index != 0 ? |
| 634 | OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 635 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 636 | return class_id <= srep->max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 639 | /*** generic rate counter support ***/ |
| 640 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 641 | static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 642 | const struct rate_ctr_group *ctrg, |
| 643 | const struct rate_ctr_desc *desc, |
| 644 | int64_t value, int64_t delta) |
| 645 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 646 | if (!srep->send_counter) |
| 647 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 648 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 649 | return srep->send_counter(srep, ctrg, desc, value, delta); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static int rate_ctr_handler( |
| 653 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 654 | const struct rate_ctr_desc *desc, void *sctx_) |
| 655 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 656 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 657 | int64_t delta = rate_ctr_difference(ctr); |
| 658 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 659 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 660 | if (!srep->running) |
| 661 | continue; |
| 662 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 663 | if (delta == 0 && !srep->force_single_flush) |
| 664 | continue; |
| 665 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 666 | if (!osmo_stats_reporter_check_config(srep, |
| 667 | ctrg->idx, ctrg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 668 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 669 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 670 | osmo_stats_reporter_send_counter(srep, ctrg, desc, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 671 | ctr->current, delta); |
| 672 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 673 | /* TODO: handle result (log?, inc counter(!)?) or remove it */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) |
| 680 | { |
| 681 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 686 | /*** stat item support ***/ |
| 687 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 688 | static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep, |
| 689 | const struct osmo_stat_item_group *statg, |
| 690 | const struct osmo_stat_item_desc *desc, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 691 | int32_t value) |
| 692 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 693 | if (!srep->send_item) |
| 694 | return 0; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 695 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 696 | return srep->send_item(srep, statg, desc, value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 699 | static int osmo_stat_item_handler( |
| 700 | struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_) |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 701 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 702 | struct osmo_stats_reporter *srep; |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 703 | int32_t prev_reported_value = item->reported.max; |
| 704 | int32_t new_value = item->value.max; |
Oliver Smith | 11da4a4 | 2021-08-19 11:58:09 +0200 | [diff] [blame] | 705 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 706 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
| 707 | if (!srep->running) |
| 708 | continue; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 709 | |
Neels Hofmeyr | 6a59407 | 2021-09-14 21:49:00 +0200 | [diff] [blame] | 710 | /* If the previously reported value is the same as the current value, skip resending the value. |
| 711 | * However, if the stats reporter is set to resend all values, do resend the current value regardless of |
| 712 | * repetitions. |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 713 | */ |
Neels Hofmeyr | 6a59407 | 2021-09-14 21:49:00 +0200 | [diff] [blame] | 714 | if (new_value == prev_reported_value && !srep->force_single_flush) |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 715 | continue; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 716 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 717 | if (!osmo_stats_reporter_check_config(srep, |
| 718 | statg->idx, statg->desc->class_id)) |
| 719 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 720 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 721 | osmo_stats_reporter_send_item(srep, statg, item->desc, new_value); |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 722 | } |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 723 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 724 | osmo_stat_item_flush(item); |
| 725 | |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 729 | static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_) |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 730 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 731 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 732 | |
| 733 | return 0; |
| 734 | } |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 735 | |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 736 | /*** osmo counter support ***/ |
| 737 | |
| 738 | static int handle_counter(struct osmo_counter *counter, void *sctx_) |
| 739 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 740 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 741 | struct rate_ctr_desc desc = {0}; |
| 742 | /* Fake a rate counter description */ |
| 743 | desc.name = counter->name; |
| 744 | desc.description = counter->description; |
| 745 | |
| 746 | int delta = osmo_counter_difference(counter); |
| 747 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 748 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 749 | if (!srep->running) |
| 750 | continue; |
| 751 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 752 | if (delta == 0 && !srep->force_single_flush) |
| 753 | continue; |
| 754 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 755 | osmo_stats_reporter_send_counter(srep, NULL, &desc, |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 756 | counter->value, delta); |
| 757 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 758 | /* TODO: handle result (log?, inc counter(!)?) */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 765 | /*** main reporting function ***/ |
| 766 | |
Harald Welte | 1e1436c | 2022-05-08 09:57:04 +0200 | [diff] [blame] | 767 | static void flush_all_reporters(void) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 768 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 769 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 770 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 771 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 772 | if (!srep->running) |
| 773 | continue; |
| 774 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 775 | osmo_stats_reporter_send_buffer(srep); |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 776 | |
| 777 | /* reset force_single_flush first */ |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 778 | srep->force_single_flush = 0; |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 779 | /* and schedule a new flush if it's time for it */ |
| 780 | if (srep->flush_period > 0) { |
| 781 | srep->flush_period_counter++; |
| 782 | if (srep->flush_period_counter >= srep->flush_period) { |
| 783 | srep->force_single_flush = 1; |
| 784 | srep->flush_period_counter = 0; |
| 785 | } |
| 786 | } |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Harald Welte | 1e1436c | 2022-05-08 09:57:04 +0200 | [diff] [blame] | 790 | int osmo_stats_report(void) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 791 | { |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 792 | /* per group actions */ |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 793 | TRACE(LIBOSMOCORE_STATS_START()); |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 794 | osmo_counters_for_each(handle_counter, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 795 | rate_ctr_for_each_group(rate_ctr_group_handler, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 796 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 797 | |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 798 | /* global actions */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 799 | flush_all_reporters(); |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 800 | TRACE(LIBOSMOCORE_STATS_DONE()); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 801 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 802 | return 0; |
| 803 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 804 | |
| 805 | #endif /* !EMBEDDED */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 806 | |
| 807 | /*! @} */ |