blob: 16e6f62003abd40379d8113a6fc17f2638b44d5e [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file stats.c */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02002/*
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2015 by sysmocom - s.f.m.c. GmbH
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02004 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02005 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02009 * 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 Erlbeck95bf82802015-10-20 19:05:52 +020019 */
20
Harald Welteeb5b6ce2017-10-15 20:03:24 +020021/*! \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 Welte67bdd802017-01-15 17:56:11 +010061#include "config.h"
62#if !defined(EMBEDDED)
63
Harald Welte95871da2017-05-15 12:11:36 +020064#include <osmocom/core/byteswap.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020065#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 Freytherc3376932015-08-21 19:56:54 +000072#include <sys/types.h>
Alexander Chemerisf5bdef42020-05-09 01:57:51 +030073#include <inttypes.h>
Harald Welte67bdd802017-01-15 17:56:11 +010074
75#ifdef HAVE_SYS_SOCKET_H
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020076#include <sys/socket.h>
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000077#include <netinet/in.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020078#include <arpa/inet.h>
Harald Welte67bdd802017-01-15 17:56:11 +010079#endif
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020080
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 Chemerisf5bdef42020-05-09 01:57:51 +030085#include <osmocom/core/select.h>
Harald Welte216338c2017-10-15 19:46:19 +020086#include <osmocom/core/counter.h>
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010087#include <osmocom/core/msgb.h>
Philipp Maierb1ef8f52021-12-06 16:31:02 +010088#include <osmocom/core/stats_tcp.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020089
Harald Welte3217d512021-02-18 14:56:46 +010090#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 Hofmeyre90c7172021-09-14 14:37:38 +0200101#include <stat_item_internal.h>
102
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100103#define STATS_DEFAULT_INTERVAL 5 /* secs */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100104#define STATS_DEFAULT_BUFLEN 256
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200105
Vadim Yanitskiybfc83772021-11-08 23:14:29 +0300106LLIST_HEAD(osmo_stats_reporter_list);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107static void *osmo_stats_ctx = NULL;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100108static int is_initialised = 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100109
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100110static struct osmo_stats_config s_stats_config = {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100111 .interval = STATS_DEFAULT_INTERVAL,
112};
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100113struct osmo_stats_config *osmo_stats_config = &s_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100114
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300115static struct osmo_fd osmo_stats_timer = { .fd = -1 };
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200116
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100118 const struct rate_ctr_group *ctrg,
119 const struct rate_ctr_desc *desc,
120 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100121static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
122 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100123 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100124
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100125static int update_srep_config(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200126{
127 int rc = 0;
128
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200129 if (srep->running) {
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100130 if (srep->close)
131 rc = srep->close(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200132 srep->running = 0;
133 }
134
135 if (!srep->enabled)
136 return rc;
137
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100138 if (srep->open)
139 rc = srep->open(srep);
140 else
141 rc = 0;
142
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200143 if (rc < 0)
144 srep->enabled = 0;
145 else
146 srep->running = 1;
147
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100148 srep->force_single_flush = 1;
149
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200150 return rc;
151}
152
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300153static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100154{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300155 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 Erlbeckb1dbfb42015-10-26 11:58:38 +0100171
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100172 if (!llist_empty(&osmo_stats_reporter_list))
173 osmo_stats_report();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100174
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300175 return 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100176}
177
Harald Welte1e1436c2022-05-08 09:57:04 +0200178static int start_timer(void)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100179{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300180 int rc;
181 int interval = osmo_stats_config->interval;
182
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100183 if (!is_initialised)
184 return -ESRCH;
185
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300186 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 Chemerisf5bdef42020-05-09 01:57:51 +0300193
Daniel Willmann1a1de332020-07-14 18:11:14 +0200194 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 Erlbeckb1dbfb42015-10-26 11:58:38 +0100208
209 return 0;
210}
211
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100212struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200213 const char *name)
214{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100215 struct osmo_stats_reporter *srep;
216 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Harald Weltee8e24c72022-05-08 10:01:52 +0200217 if (!srep)
218 return NULL;
219
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200220 srep->type = type;
221 if (name)
222 srep->name = talloc_strdup(srep, name);
223 srep->fd = -1;
224
Vadim Yanitskiy4e924722021-11-09 03:58:35 +0300225 llist_add_tail(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200226
227 return srep;
228}
229
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200230/*! Destroy a given stats_reporter. Takes care of first disabling it.
231 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100232void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200233{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100234 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200235 llist_del(&srep->list);
236 talloc_free(srep);
237}
238
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200239/*! Initialize the stats reporting module; call this once in your program.
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200240 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100241void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200242{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100243 osmo_stats_ctx = ctx;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100244 is_initialised = 1;
245 start_timer();
Philipp Maierb1ef8f52021-12-06 16:31:02 +0100246
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 Erlbeck95bf82802015-10-20 19:05:52 +0200250}
251
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200252/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100256struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200257 const char *name)
258{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100259 struct osmo_stats_reporter *srep;
260 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200261 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 Welte67bdd802017-01-15 17:56:11 +0100273#ifdef HAVE_SYS_SOCKET_H
274
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200275/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100279int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200280{
281 int rc;
282 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
283 struct in_addr inaddr;
284
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100285 if (!srep->have_net_config)
286 return -ENOTSUP;
287
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200288 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 Welteeb5b6ce2017-10-15 20:03:24 +0200304/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100308int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200309{
310 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
311
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100312 if (!srep->have_net_config)
313 return -ENOTSUP;
314
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200315 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200316 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200317
318 return update_srep_config(srep);
319}
320
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200321/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100325int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200326{
327 int rc;
328 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
329 struct in_addr inaddr;
330
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100331 if (!srep->have_net_config)
332 return -ENOTSUP;
333
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200334 if (addr) {
335 rc = inet_pton(AF_INET, addr, &inaddr);
336 if (rc <= 0)
337 return -EINVAL;
338 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100339 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200340 }
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 Welteeb5b6ce2017-10-15 20:03:24 +0200352/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100356int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100357{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100358 if (!srep->have_net_config)
359 return -ENOTSUP;
360
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100361 if (mtu < 0)
362 return -EINVAL;
363
364 srep->mtu = mtu;
365
366 return update_srep_config(srep);
367}
Harald Welte67bdd802017-01-15 17:56:11 +0100368#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100369
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100370int 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 Chemerisf203ed32020-05-08 19:09:22 +0300381/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200382 * \param[in] interval Reporting interval in seconds
383 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100384int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200385{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200386 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100387 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200388
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100389 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100390 if (is_initialised)
391 start_timer();
392
393 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200394}
395
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300396/*! 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 */
404int 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 Welteeb5b6ce2017-10-15 20:03:24 +0200415/*! Set the name prefix of a given stats_reporter.
416 * \param[in] srep stats_reporter whose name prefix is to be set
Philipp Maier26728952021-12-14 18:49:05 +0100417 * \param[in] prefix Name prefix to pre-pend for any reported value
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200418 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100419int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200420{
421 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100422 srep->name_prefix = prefix && strlen(prefix) > 0 ?
423 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200424
425 return update_srep_config(srep);
426}
427
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200428
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 Erlbeckfc9533d2015-10-29 00:55:58 +0100432int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200433{
434 srep->enabled = 1;
435
436 return update_srep_config(srep);
437}
438
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200439/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100442int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200443{
444 srep->enabled = 0;
445
446 return update_srep_config(srep);
447}
448
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100449/*** i/o helper functions ***/
450
Harald Welte67bdd802017-01-15 17:56:11 +0100451#ifdef HAVE_SYS_SOCKET_H
452
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200453/*! 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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100456int 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-Bellcc3694b2016-05-18 16:02:19 -0400469#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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100477 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 Weltee8e24c72022-05-08 10:01:52 +0200491 if (!srep->buffer)
492 goto failed;
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100493
494 return 0;
495
496failed:
497 rc = -errno;
498 close(sock);
499
500 return rc;
501}
502
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200503/*! 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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100506int 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 Welteeb5b6ce2017-10-15 20:03:24 +0200521/*! 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 Erlbeckb6e6bea2015-11-09 15:33:44 +0100526int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200527 int data_len)
528{
529 int rc;
530
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400531 rc = sendto(srep->fd, data, data_len,
532#ifdef MSG_NOSIGNAL
533 MSG_NOSIGNAL |
534#endif
535 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200536 &srep->dest_addr, srep->dest_addr_len);
537
538 if (rc == -1)
539 rc = -errno;
540
541 return rc;
542}
543
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200544/*! 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 Erlbeckb6e6bea2015-11-09 15:33:44 +0100547int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100548{
549 int rc;
550
551 if (!srep->buffer || msgb_length(srep->buffer) == 0)
552 return 0;
553
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100554 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100555 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
556
557 msgb_trim(srep->buffer, 0);
558
559 return rc;
560}
Harald Welte67bdd802017-01-15 17:56:11 +0100561#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100562
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100563/*** log reporter ***/
564
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200565/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100572struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100573{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100574 struct osmo_stats_reporter *srep;
575 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Harald Weltee8e24c72022-05-08 10:01:52 +0200576 if (!srep)
577 return NULL;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100578
579 srep->have_net_config = 0;
580
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100581 srep->send_counter = osmo_stats_reporter_log_send_counter;
582 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100583
584 return srep;
585}
586
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100587static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100588 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100589 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100590 const char *unit)
591{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100592 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100593 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100594 type, srep->name_prefix ? srep->name_prefix : "",
595 name1 ? name1 : "", index1,
596 name2, value, unit ? unit : "");
597
598 return 0;
599}
600
601
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100602static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100603 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100608 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100609 ctrg->desc->group_name_prefix,
610 ctrg->idx,
611 desc->name, value, NULL);
612 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100613 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100614 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100615 desc->name, value, NULL);
616}
617
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100618static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
619 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100620 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100621{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100622 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100623 statg->desc->group_name_prefix, statg->idx,
624 desc->name, value, desc->unit);
625}
626
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100627/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200628
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100629static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
630 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200631{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100632 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
633 class_id = index != 0 ?
634 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200635
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100636 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200637}
638
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200639/*** generic rate counter support ***/
640
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100641static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200642 const struct rate_ctr_group *ctrg,
643 const struct rate_ctr_desc *desc,
644 int64_t value, int64_t delta)
645{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100646 if (!srep->send_counter)
647 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200648
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100649 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200650}
651
652static 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100656 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200657 int64_t delta = rate_ctr_difference(ctr);
658
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100659 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200660 if (!srep->running)
661 continue;
662
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100663 if (delta == 0 && !srep->force_single_flush)
664 continue;
665
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100666 if (!osmo_stats_reporter_check_config(srep,
667 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100668 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100669
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100670 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200671 ctr->current, delta);
672
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100673 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200674 }
675
676 return 0;
677}
678
679static 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 Erlbeckc27671c2015-10-26 12:32:07 +0100686/*** stat item support ***/
687
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100688static 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 Erlbeckc27671c2015-10-26 12:32:07 +0100691 int32_t value)
692{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100693 if (!srep->send_item)
694 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100695
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100696 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100697}
698
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100699static int osmo_stat_item_handler(
700 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100701{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100702 struct osmo_stats_reporter *srep;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200703 int32_t prev_reported_value = item->reported.max;
704 int32_t new_value = item->value.max;
Oliver Smith11da4a42021-08-19 11:58:09 +0200705
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100706 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
707 if (!srep->running)
708 continue;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100709
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200710 /* 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 Hofmeyre90c7172021-09-14 14:37:38 +0200713 */
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200714 if (new_value == prev_reported_value && !srep->force_single_flush)
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100715 continue;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100716
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100717 if (!osmo_stats_reporter_check_config(srep,
718 statg->idx, statg->desc->class_id))
719 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100720
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200721 osmo_stats_reporter_send_item(srep, statg, item->desc, new_value);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100722 }
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100723
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200724 osmo_stat_item_flush(item);
725
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100726 return 0;
727}
728
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100729static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100730{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100731 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100732
733 return 0;
734}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200735
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100736/*** osmo counter support ***/
737
738static int handle_counter(struct osmo_counter *counter, void *sctx_)
739{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100740 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100741 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100748 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100749 if (!srep->running)
750 continue;
751
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100752 if (delta == 0 && !srep->force_single_flush)
753 continue;
754
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100755 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100756 counter->value, delta);
757
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100758 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100759 }
760
761 return 0;
762}
763
764
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200765/*** main reporting function ***/
766
Harald Welte1e1436c2022-05-08 09:57:04 +0200767static void flush_all_reporters(void)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100768{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100769 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100770
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100771 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100772 if (!srep->running)
773 continue;
774
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100775 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300776
777 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100778 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300779 /* 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 Erlbeckd01acfc2015-10-26 16:22:45 +0100787 }
788}
789
Harald Welte1e1436c2022-05-08 09:57:04 +0200790int osmo_stats_report(void)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200791{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100792 /* per group actions */
Harald Welte3217d512021-02-18 14:56:46 +0100793 TRACE(LIBOSMOCORE_STATS_START());
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100794 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200795 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100796 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200797
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100798 /* global actions */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100799 flush_all_reporters();
Harald Welte3217d512021-02-18 14:56:46 +0100800 TRACE(LIBOSMOCORE_STATS_DONE());
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100801
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200802 return 0;
803}
Harald Welte67bdd802017-01-15 17:56:11 +0100804
805#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200806
807/*! @} */