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 | |
| 178 | static int start_timer() |
| 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); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 217 | OSMO_ASSERT(srep); |
| 218 | srep->type = type; |
| 219 | if (name) |
| 220 | srep->name = talloc_strdup(srep, name); |
| 221 | srep->fd = -1; |
| 222 | |
Vadim Yanitskiy | 4e92472 | 2021-11-09 03:58:35 +0300 | [diff] [blame] | 223 | llist_add_tail(&srep->list, &osmo_stats_reporter_list); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 224 | |
| 225 | return srep; |
| 226 | } |
| 227 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 228 | /*! Destroy a given stats_reporter. Takes care of first disabling it. |
| 229 | * \param[in] srep stats_reporter that shall be disabled + destroyed */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 230 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 231 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 232 | osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 233 | llist_del(&srep->list); |
| 234 | talloc_free(srep); |
| 235 | } |
| 236 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 237 | /*! Initialize the stats reporting module; call this once in your program. |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 238 | * \param[in] ctx Talloc context from which stats related memory is allocated */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 239 | void osmo_stats_init(void *ctx) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 240 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 241 | osmo_stats_ctx = ctx; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 242 | is_initialised = 1; |
| 243 | start_timer(); |
Philipp Maier | b1ef8f5 | 2021-12-06 16:31:02 +0100 | [diff] [blame] | 244 | |
| 245 | /* Make sure that the tcp-stats interval timer also runs at its |
| 246 | * preconfigured rate. The vty might change this setting later. */ |
| 247 | osmo_stats_tcp_set_interval(osmo_tcp_stats_config->interval); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 250 | /*! Find a stats_reporter of given \a type and \a name. |
| 251 | * \param[in] type Type of stats_reporter to find |
| 252 | * \param[in] name Name of stats_reporter to find |
| 253 | * \returns stats_reporter matching \a type and \a name; NULL otherwise */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 254 | 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] | 255 | const char *name) |
| 256 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 257 | struct osmo_stats_reporter *srep; |
| 258 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 259 | if (srep->type != type) |
| 260 | continue; |
| 261 | if (srep->name != name) { |
| 262 | if (name == NULL || srep->name == NULL || |
| 263 | strcmp(name, srep->name) != 0) |
| 264 | continue; |
| 265 | } |
| 266 | return srep; |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 271 | #ifdef HAVE_SYS_SOCKET_H |
| 272 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 273 | /*! Set the remote (IP) address of a given stats_reporter. |
| 274 | * \param[in] srep stats_reporter whose remote address is to be set |
| 275 | * \param[in] addr String representation of remote IPv4 address |
| 276 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 277 | 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] | 278 | { |
| 279 | int rc; |
| 280 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 281 | struct in_addr inaddr; |
| 282 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 283 | if (!srep->have_net_config) |
| 284 | return -ENOTSUP; |
| 285 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 286 | OSMO_ASSERT(addr != NULL); |
| 287 | |
| 288 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 289 | if (rc <= 0) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | sock_addr->sin_addr = inaddr; |
| 293 | sock_addr->sin_family = AF_INET; |
| 294 | srep->dest_addr_len = sizeof(*sock_addr); |
| 295 | |
| 296 | talloc_free(srep->dest_addr_str); |
| 297 | srep->dest_addr_str = talloc_strdup(srep, addr); |
| 298 | |
| 299 | return update_srep_config(srep); |
| 300 | } |
| 301 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 302 | /*! Set the remote (UDP) port of a given stats_reporter |
| 303 | * \param[in] srep stats_reporter whose remote address is to be set |
| 304 | * \param[in] port UDP port of remote statsd to which we report |
| 305 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 306 | 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] | 307 | { |
| 308 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 309 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 310 | if (!srep->have_net_config) |
| 311 | return -ENOTSUP; |
| 312 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 313 | srep->dest_port = port; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 314 | sock_addr->sin_port = osmo_htons(port); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 315 | |
| 316 | return update_srep_config(srep); |
| 317 | } |
| 318 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 319 | /*! Set the local (IP) address of a given stats_reporter. |
| 320 | * \param[in] srep stats_reporter whose remote address is to be set |
| 321 | * \param[in] addr String representation of local IP address |
| 322 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 323 | 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] | 324 | { |
| 325 | int rc; |
| 326 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr; |
| 327 | struct in_addr inaddr; |
| 328 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 329 | if (!srep->have_net_config) |
| 330 | return -ENOTSUP; |
| 331 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 332 | if (addr) { |
| 333 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 334 | if (rc <= 0) |
| 335 | return -EINVAL; |
| 336 | } else { |
Holger Hans Peter Freyther | 7921975 | 2015-11-02 15:50:32 +0100 | [diff] [blame] | 337 | inaddr.s_addr = INADDR_ANY; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | sock_addr->sin_addr = inaddr; |
| 341 | sock_addr->sin_family = AF_INET; |
| 342 | srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0; |
| 343 | |
| 344 | talloc_free(srep->bind_addr_str); |
| 345 | srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL; |
| 346 | |
| 347 | return update_srep_config(srep); |
| 348 | } |
| 349 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 350 | /*! Set the maximum transmission unit of a given stats_reporter. |
| 351 | * \param[in] srep stats_reporter whose remote address is to be set |
| 352 | * \param[in] mtu Maximum Transmission Unit of \a srep |
| 353 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 354 | 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] | 355 | { |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 356 | if (!srep->have_net_config) |
| 357 | return -ENOTSUP; |
| 358 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 359 | if (mtu < 0) |
| 360 | return -EINVAL; |
| 361 | |
| 362 | srep->mtu = mtu; |
| 363 | |
| 364 | return update_srep_config(srep); |
| 365 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 366 | #endif /* HAVE_SYS_SOCKETS_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 367 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 368 | int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, |
| 369 | enum osmo_stats_class class_id) |
| 370 | { |
| 371 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 372 | return -EINVAL; |
| 373 | |
| 374 | srep->max_class = class_id; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Alexander Chemeris | f203ed3 | 2020-05-08 19:09:22 +0300 | [diff] [blame] | 379 | /*! Set the reporting interval (common for all reporters) |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 380 | * \param[in] interval Reporting interval in seconds |
| 381 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 382 | int osmo_stats_set_interval(int interval) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 383 | { |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 384 | if (interval < 0) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 385 | return -EINVAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 386 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 387 | osmo_stats_config->interval = interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 388 | if (is_initialised) |
| 389 | start_timer(); |
| 390 | |
| 391 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 394 | /*! Set the regular flush period for a given stats_reporter |
| 395 | * |
| 396 | * Send all stats even if they have not changed (i.e. force the flush) |
| 397 | * every N-th reporting interval. Set to 0 to disable regular flush, |
| 398 | * set to 1 to flush every time, set to 2 to flush every 2nd time, etc. |
| 399 | * \param[in] srep stats_reporter to set flush period for |
| 400 | * \param[in] period Reporting interval in seconds |
| 401 | * \returns 0 on success; negative on error */ |
| 402 | int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period) |
| 403 | { |
| 404 | srep->flush_period = period; |
| 405 | srep->flush_period_counter = 0; |
| 406 | /* force the flush now if it's not disabled by period=0 */ |
| 407 | if (period > 0) |
| 408 | srep->force_single_flush = 1; |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 413 | /*! Set the name prefix of a given stats_reporter. |
| 414 | * \param[in] srep stats_reporter whose name prefix is to be set |
Philipp Maier | 2672895 | 2021-12-14 18:49:05 +0100 | [diff] [blame] | 415 | * \param[in] prefix Name prefix to pre-pend for any reported value |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 416 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 417 | 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] | 418 | { |
| 419 | talloc_free(srep->name_prefix); |
Jacob Erlbeck | 916423e | 2015-11-09 10:52:19 +0100 | [diff] [blame] | 420 | srep->name_prefix = prefix && strlen(prefix) > 0 ? |
| 421 | talloc_strdup(srep, prefix) : NULL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 422 | |
| 423 | return update_srep_config(srep); |
| 424 | } |
| 425 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 426 | |
| 427 | /*! Enable the given stats_reporter. |
| 428 | * \param[in] srep stats_reporter who is to be enabled |
| 429 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 430 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 431 | { |
| 432 | srep->enabled = 1; |
| 433 | |
| 434 | return update_srep_config(srep); |
| 435 | } |
| 436 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 437 | /*! Disable the given stats_reporter. |
| 438 | * \param[in] srep stats_reporter who is to be disabled |
| 439 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 440 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 441 | { |
| 442 | srep->enabled = 0; |
| 443 | |
| 444 | return update_srep_config(srep); |
| 445 | } |
| 446 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 447 | /*** i/o helper functions ***/ |
| 448 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 449 | #ifdef HAVE_SYS_SOCKET_H |
| 450 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 451 | /*! Open the UDP socket for given stats_reporter. |
| 452 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 453 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 454 | int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep) |
| 455 | { |
| 456 | int sock; |
| 457 | int rc; |
| 458 | int buffer_size = STATS_DEFAULT_BUFLEN; |
| 459 | |
| 460 | if (srep->fd != -1 && srep->close) |
| 461 | srep->close(srep); |
| 462 | |
| 463 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 464 | if (sock == -1) |
| 465 | return -errno; |
| 466 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 467 | #if defined(__APPLE__) && !defined(MSG_NOSIGNAL) |
| 468 | { |
| 469 | static int val = 1; |
| 470 | |
| 471 | rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val)); |
| 472 | goto failed; |
| 473 | } |
| 474 | #endif |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 475 | if (srep->bind_addr_len > 0) { |
| 476 | rc = bind(sock, &srep->bind_addr, srep->bind_addr_len); |
| 477 | if (rc == -1) |
| 478 | goto failed; |
| 479 | } |
| 480 | |
| 481 | srep->fd = sock; |
| 482 | |
| 483 | if (srep->mtu > 0) { |
| 484 | buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */; |
| 485 | srep->agg_enabled = 1; |
| 486 | } |
| 487 | |
| 488 | srep->buffer = msgb_alloc(buffer_size, "stats buffer"); |
| 489 | |
| 490 | return 0; |
| 491 | |
| 492 | failed: |
| 493 | rc = -errno; |
| 494 | close(sock); |
| 495 | |
| 496 | return rc; |
| 497 | } |
| 498 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 499 | /*! Closee the UDP socket for given stats_reporter. |
| 500 | * \param[in] srep stats_reporter whose UDP socket is to be closed |
| 501 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 502 | int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep) |
| 503 | { |
| 504 | int rc; |
| 505 | if (srep->fd == -1) |
| 506 | return -EBADF; |
| 507 | |
| 508 | osmo_stats_reporter_send_buffer(srep); |
| 509 | |
| 510 | rc = close(srep->fd); |
| 511 | srep->fd = -1; |
| 512 | msgb_free(srep->buffer); |
| 513 | srep->buffer = NULL; |
| 514 | return rc == -1 ? -errno : 0; |
| 515 | } |
| 516 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 517 | /*! Send given date to given stats_reporter. |
| 518 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 519 | * \param[in] data string data to be sent |
| 520 | * \param[in] data_len Length of \a data in bytes |
| 521 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 522 | 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] | 523 | int data_len) |
| 524 | { |
| 525 | int rc; |
| 526 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 527 | rc = sendto(srep->fd, data, data_len, |
| 528 | #ifdef MSG_NOSIGNAL |
| 529 | MSG_NOSIGNAL | |
| 530 | #endif |
| 531 | MSG_DONTWAIT, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 532 | &srep->dest_addr, srep->dest_addr_len); |
| 533 | |
| 534 | if (rc == -1) |
| 535 | rc = -errno; |
| 536 | |
| 537 | return rc; |
| 538 | } |
| 539 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 540 | /*! Send current accumulated buffer to given stats_reporter. |
| 541 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 542 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 543 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 544 | { |
| 545 | int rc; |
| 546 | |
| 547 | if (!srep->buffer || msgb_length(srep->buffer) == 0) |
| 548 | return 0; |
| 549 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 550 | rc = osmo_stats_reporter_send(srep, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 551 | (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer)); |
| 552 | |
| 553 | msgb_trim(srep->buffer, 0); |
| 554 | |
| 555 | return rc; |
| 556 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 557 | #endif /* HAVE_SYS_SOCKET_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 558 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 559 | /*** log reporter ***/ |
| 560 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 561 | /*! Create a stats_reporter that logs via libosmocore logging. |
| 562 | * A stats_reporter created via this function will simply print the statistics |
| 563 | * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO |
| 564 | * priority. The configuration of the libosmocore log targets define where this |
| 565 | * information will end up (ignored, text file, stderr, syslog, ...). |
| 566 | * \param[in] name Name of the to-be-created stats_reporter |
| 567 | * \returns stats_reporter on success; NULL on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 568 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 569 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 570 | struct osmo_stats_reporter *srep; |
| 571 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 572 | |
| 573 | srep->have_net_config = 0; |
| 574 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 575 | srep->send_counter = osmo_stats_reporter_log_send_counter; |
| 576 | srep->send_item = osmo_stats_reporter_log_send_item; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 577 | |
| 578 | return srep; |
| 579 | } |
| 580 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 581 | static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 582 | const char *type, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 583 | const char *name1, unsigned int index1, const char *name2, int value, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 584 | const char *unit) |
| 585 | { |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 586 | LOGP(DLSTATS, LOGL_INFO, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 587 | "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] | 588 | type, srep->name_prefix ? srep->name_prefix : "", |
| 589 | name1 ? name1 : "", index1, |
| 590 | name2, value, unit ? unit : ""); |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 596 | 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] | 597 | const struct rate_ctr_group *ctrg, |
| 598 | const struct rate_ctr_desc *desc, |
| 599 | int64_t value, int64_t delta) |
| 600 | { |
| 601 | if (ctrg) |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 602 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 603 | ctrg->desc->group_name_prefix, |
| 604 | ctrg->idx, |
| 605 | desc->name, value, NULL); |
| 606 | else |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 607 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 608 | NULL, 0, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 609 | desc->name, value, NULL); |
| 610 | } |
| 611 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 612 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 613 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 614 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 615 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 616 | return osmo_stats_reporter_log_send(srep, "i", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 617 | statg->desc->group_name_prefix, statg->idx, |
| 618 | desc->name, value, desc->unit); |
| 619 | } |
| 620 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 621 | /*** helper for reporting ***/ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 622 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 623 | static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep, |
| 624 | unsigned int index, int class_id) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 625 | { |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 626 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 627 | class_id = index != 0 ? |
| 628 | OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 629 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 630 | return class_id <= srep->max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 633 | /*** generic rate counter support ***/ |
| 634 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 635 | static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 636 | const struct rate_ctr_group *ctrg, |
| 637 | const struct rate_ctr_desc *desc, |
| 638 | int64_t value, int64_t delta) |
| 639 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 640 | if (!srep->send_counter) |
| 641 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 642 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 643 | return srep->send_counter(srep, ctrg, desc, value, delta); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static int rate_ctr_handler( |
| 647 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 648 | const struct rate_ctr_desc *desc, void *sctx_) |
| 649 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 650 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 651 | int64_t delta = rate_ctr_difference(ctr); |
| 652 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 653 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 654 | if (!srep->running) |
| 655 | continue; |
| 656 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 657 | if (delta == 0 && !srep->force_single_flush) |
| 658 | continue; |
| 659 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 660 | if (!osmo_stats_reporter_check_config(srep, |
| 661 | ctrg->idx, ctrg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 662 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 663 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 664 | osmo_stats_reporter_send_counter(srep, ctrg, desc, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 665 | ctr->current, delta); |
| 666 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 667 | /* TODO: handle result (log?, inc counter(!)?) or remove it */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) |
| 674 | { |
| 675 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 680 | /*** stat item support ***/ |
| 681 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 682 | static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep, |
| 683 | const struct osmo_stat_item_group *statg, |
| 684 | const struct osmo_stat_item_desc *desc, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 685 | int32_t value) |
| 686 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 687 | if (!srep->send_item) |
| 688 | return 0; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 689 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 690 | return srep->send_item(srep, statg, desc, value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 693 | static int osmo_stat_item_handler( |
| 694 | 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] | 695 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 696 | struct osmo_stats_reporter *srep; |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 697 | int32_t prev_reported_value = item->reported.max; |
| 698 | int32_t new_value = item->value.max; |
Oliver Smith | 11da4a4 | 2021-08-19 11:58:09 +0200 | [diff] [blame] | 699 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 700 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
| 701 | if (!srep->running) |
| 702 | continue; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 703 | |
Neels Hofmeyr | 6a59407 | 2021-09-14 21:49:00 +0200 | [diff] [blame] | 704 | /* If the previously reported value is the same as the current value, skip resending the value. |
| 705 | * However, if the stats reporter is set to resend all values, do resend the current value regardless of |
| 706 | * repetitions. |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 707 | */ |
Neels Hofmeyr | 6a59407 | 2021-09-14 21:49:00 +0200 | [diff] [blame] | 708 | if (new_value == prev_reported_value && !srep->force_single_flush) |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 709 | continue; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 710 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 711 | if (!osmo_stats_reporter_check_config(srep, |
| 712 | statg->idx, statg->desc->class_id)) |
| 713 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 714 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 715 | osmo_stats_reporter_send_item(srep, statg, item->desc, new_value); |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 716 | } |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 717 | |
Neels Hofmeyr | e90c717 | 2021-09-14 14:37:38 +0200 | [diff] [blame] | 718 | osmo_stat_item_flush(item); |
| 719 | |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 723 | 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] | 724 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 725 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 726 | |
| 727 | return 0; |
| 728 | } |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 729 | |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 730 | /*** osmo counter support ***/ |
| 731 | |
| 732 | static int handle_counter(struct osmo_counter *counter, void *sctx_) |
| 733 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 734 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 735 | struct rate_ctr_desc desc = {0}; |
| 736 | /* Fake a rate counter description */ |
| 737 | desc.name = counter->name; |
| 738 | desc.description = counter->description; |
| 739 | |
| 740 | int delta = osmo_counter_difference(counter); |
| 741 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 742 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 743 | if (!srep->running) |
| 744 | continue; |
| 745 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 746 | if (delta == 0 && !srep->force_single_flush) |
| 747 | continue; |
| 748 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 749 | osmo_stats_reporter_send_counter(srep, NULL, &desc, |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 750 | counter->value, delta); |
| 751 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 752 | /* TODO: handle result (log?, inc counter(!)?) */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 759 | /*** main reporting function ***/ |
| 760 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 761 | static void flush_all_reporters() |
| 762 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 763 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 764 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 765 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 766 | if (!srep->running) |
| 767 | continue; |
| 768 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 769 | osmo_stats_reporter_send_buffer(srep); |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 770 | |
| 771 | /* reset force_single_flush first */ |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 772 | srep->force_single_flush = 0; |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 773 | /* and schedule a new flush if it's time for it */ |
| 774 | if (srep->flush_period > 0) { |
| 775 | srep->flush_period_counter++; |
| 776 | if (srep->flush_period_counter >= srep->flush_period) { |
| 777 | srep->force_single_flush = 1; |
| 778 | srep->flush_period_counter = 0; |
| 779 | } |
| 780 | } |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 784 | int osmo_stats_report() |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 785 | { |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 786 | /* per group actions */ |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 787 | TRACE(LIBOSMOCORE_STATS_START()); |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 788 | osmo_counters_for_each(handle_counter, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 789 | rate_ctr_for_each_group(rate_ctr_group_handler, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 790 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL); |
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 | /* global actions */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 793 | flush_all_reporters(); |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 794 | TRACE(LIBOSMOCORE_STATS_DONE()); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 795 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 796 | return 0; |
| 797 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 798 | |
| 799 | #endif /* !EMBEDDED */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 800 | |
| 801 | /*! @} */ |