blob: 7b25ab1c699f9ec30f456ed405da3dbd5b7d7819 [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);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200217 OSMO_ASSERT(srep);
218 srep->type = type;
219 if (name)
220 srep->name = talloc_strdup(srep, name);
221 srep->fd = -1;
222
Vadim Yanitskiy4e924722021-11-09 03:58:35 +0300223 llist_add_tail(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200224
225 return srep;
226}
227
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200228/*! Destroy a given stats_reporter. Takes care of first disabling it.
229 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100230void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200231{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100232 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200233 llist_del(&srep->list);
234 talloc_free(srep);
235}
236
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200237/*! Initialize the stats reporting module; call this once in your program.
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200238 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100239void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200240{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100241 osmo_stats_ctx = ctx;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100242 is_initialised = 1;
243 start_timer();
Philipp Maierb1ef8f52021-12-06 16:31:02 +0100244
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 Erlbeck95bf82802015-10-20 19:05:52 +0200248}
249
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200250/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100254struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200255 const char *name)
256{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100257 struct osmo_stats_reporter *srep;
258 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200259 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 Welte67bdd802017-01-15 17:56:11 +0100271#ifdef HAVE_SYS_SOCKET_H
272
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200273/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100277int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200278{
279 int rc;
280 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
281 struct in_addr inaddr;
282
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100283 if (!srep->have_net_config)
284 return -ENOTSUP;
285
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200286 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 Welteeb5b6ce2017-10-15 20:03:24 +0200302/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100306int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200307{
308 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
309
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100310 if (!srep->have_net_config)
311 return -ENOTSUP;
312
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200313 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200314 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200315
316 return update_srep_config(srep);
317}
318
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200319/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100323int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200324{
325 int rc;
326 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
327 struct in_addr inaddr;
328
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100329 if (!srep->have_net_config)
330 return -ENOTSUP;
331
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200332 if (addr) {
333 rc = inet_pton(AF_INET, addr, &inaddr);
334 if (rc <= 0)
335 return -EINVAL;
336 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100337 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200338 }
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 Welteeb5b6ce2017-10-15 20:03:24 +0200350/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100354int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100355{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100356 if (!srep->have_net_config)
357 return -ENOTSUP;
358
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100359 if (mtu < 0)
360 return -EINVAL;
361
362 srep->mtu = mtu;
363
364 return update_srep_config(srep);
365}
Harald Welte67bdd802017-01-15 17:56:11 +0100366#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100367
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100368int 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 Chemerisf203ed32020-05-08 19:09:22 +0300379/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200380 * \param[in] interval Reporting interval in seconds
381 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100382int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200383{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200384 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100385 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200386
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100387 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100388 if (is_initialised)
389 start_timer();
390
391 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200392}
393
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300394/*! 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 */
402int 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 Welteeb5b6ce2017-10-15 20:03:24 +0200413/*! Set the name prefix of a given stats_reporter.
414 * \param[in] srep stats_reporter whose name prefix is to be set
Philipp Maier26728952021-12-14 18:49:05 +0100415 * \param[in] prefix Name prefix to pre-pend for any reported value
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200416 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100417int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200418{
419 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100420 srep->name_prefix = prefix && strlen(prefix) > 0 ?
421 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200422
423 return update_srep_config(srep);
424}
425
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200426
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 Erlbeckfc9533d2015-10-29 00:55:58 +0100430int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200431{
432 srep->enabled = 1;
433
434 return update_srep_config(srep);
435}
436
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200437/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100440int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200441{
442 srep->enabled = 0;
443
444 return update_srep_config(srep);
445}
446
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100447/*** i/o helper functions ***/
448
Harald Welte67bdd802017-01-15 17:56:11 +0100449#ifdef HAVE_SYS_SOCKET_H
450
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200451/*! 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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100454int 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-Bellcc3694b2016-05-18 16:02:19 -0400467#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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100475 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
492failed:
493 rc = -errno;
494 close(sock);
495
496 return rc;
497}
498
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200499/*! 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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100502int 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 Welteeb5b6ce2017-10-15 20:03:24 +0200517/*! 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 Erlbeckb6e6bea2015-11-09 15:33:44 +0100522int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200523 int data_len)
524{
525 int rc;
526
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400527 rc = sendto(srep->fd, data, data_len,
528#ifdef MSG_NOSIGNAL
529 MSG_NOSIGNAL |
530#endif
531 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200532 &srep->dest_addr, srep->dest_addr_len);
533
534 if (rc == -1)
535 rc = -errno;
536
537 return rc;
538}
539
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200540/*! 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 Erlbeckb6e6bea2015-11-09 15:33:44 +0100543int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100544{
545 int rc;
546
547 if (!srep->buffer || msgb_length(srep->buffer) == 0)
548 return 0;
549
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100550 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100551 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
552
553 msgb_trim(srep->buffer, 0);
554
555 return rc;
556}
Harald Welte67bdd802017-01-15 17:56:11 +0100557#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100558
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100559/*** log reporter ***/
560
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200561/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100568struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100569{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100570 struct osmo_stats_reporter *srep;
571 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100572
573 srep->have_net_config = 0;
574
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100575 srep->send_counter = osmo_stats_reporter_log_send_counter;
576 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100577
578 return srep;
579}
580
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100581static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100582 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100583 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100584 const char *unit)
585{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100586 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100587 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100588 type, srep->name_prefix ? srep->name_prefix : "",
589 name1 ? name1 : "", index1,
590 name2, value, unit ? unit : "");
591
592 return 0;
593}
594
595
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100596static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100597 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100602 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100603 ctrg->desc->group_name_prefix,
604 ctrg->idx,
605 desc->name, value, NULL);
606 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100607 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100608 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100609 desc->name, value, NULL);
610}
611
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100612static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
613 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100614 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100615{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100616 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100617 statg->desc->group_name_prefix, statg->idx,
618 desc->name, value, desc->unit);
619}
620
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100621/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200622
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100623static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
624 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200625{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100626 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
627 class_id = index != 0 ?
628 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200629
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100630 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200631}
632
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200633/*** generic rate counter support ***/
634
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100635static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200636 const struct rate_ctr_group *ctrg,
637 const struct rate_ctr_desc *desc,
638 int64_t value, int64_t delta)
639{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100640 if (!srep->send_counter)
641 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200642
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100643 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200644}
645
646static 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100650 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200651 int64_t delta = rate_ctr_difference(ctr);
652
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100653 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200654 if (!srep->running)
655 continue;
656
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100657 if (delta == 0 && !srep->force_single_flush)
658 continue;
659
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100660 if (!osmo_stats_reporter_check_config(srep,
661 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100662 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100663
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100664 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200665 ctr->current, delta);
666
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100667 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200668 }
669
670 return 0;
671}
672
673static 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 Erlbeckc27671c2015-10-26 12:32:07 +0100680/*** stat item support ***/
681
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100682static 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 Erlbeckc27671c2015-10-26 12:32:07 +0100685 int32_t value)
686{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100687 if (!srep->send_item)
688 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100689
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100690 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100691}
692
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100693static int osmo_stat_item_handler(
694 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100695{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100696 struct osmo_stats_reporter *srep;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200697 int32_t prev_reported_value = item->reported.max;
698 int32_t new_value = item->value.max;
Oliver Smith11da4a42021-08-19 11:58:09 +0200699
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100700 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
701 if (!srep->running)
702 continue;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100703
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200704 /* 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 Hofmeyre90c7172021-09-14 14:37:38 +0200707 */
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200708 if (new_value == prev_reported_value && !srep->force_single_flush)
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100709 continue;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100710
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100711 if (!osmo_stats_reporter_check_config(srep,
712 statg->idx, statg->desc->class_id))
713 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100714
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200715 osmo_stats_reporter_send_item(srep, statg, item->desc, new_value);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100716 }
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100717
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200718 osmo_stat_item_flush(item);
719
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100720 return 0;
721}
722
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100723static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100724{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100725 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100726
727 return 0;
728}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200729
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100730/*** osmo counter support ***/
731
732static int handle_counter(struct osmo_counter *counter, void *sctx_)
733{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100734 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100735 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100742 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100743 if (!srep->running)
744 continue;
745
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100746 if (delta == 0 && !srep->force_single_flush)
747 continue;
748
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100749 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100750 counter->value, delta);
751
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100752 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100753 }
754
755 return 0;
756}
757
758
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200759/*** main reporting function ***/
760
Harald Welte1e1436c2022-05-08 09:57:04 +0200761static void flush_all_reporters(void)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100762{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100763 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100764
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100765 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100766 if (!srep->running)
767 continue;
768
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100769 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300770
771 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100772 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300773 /* 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 Erlbeckd01acfc2015-10-26 16:22:45 +0100781 }
782}
783
Harald Welte1e1436c2022-05-08 09:57:04 +0200784int osmo_stats_report(void)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200785{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100786 /* per group actions */
Harald Welte3217d512021-02-18 14:56:46 +0100787 TRACE(LIBOSMOCORE_STATS_START());
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100788 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200789 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100790 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200791
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100792 /* global actions */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100793 flush_all_reporters();
Harald Welte3217d512021-02-18 14:56:46 +0100794 TRACE(LIBOSMOCORE_STATS_DONE());
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100795
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200796 return 0;
797}
Harald Welte67bdd802017-01-15 17:56:11 +0100798
799#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200800
801/*! @} */