blob: f8c91fd7e241cd90c8125a4e66cfbbe6807a332f [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>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020088
Harald Welte3217d512021-02-18 14:56:46 +010089#ifdef HAVE_SYSTEMTAP
90/* include the generated probes header and put markers in code */
91#include "probes.h"
92#define TRACE(probe) probe
93#define TRACE_ENABLED(probe) probe ## _ENABLED()
94#else
95/* Wrap the probe to allow it to be removed when no systemtap available */
96#define TRACE(probe)
97#define TRACE_ENABLED(probe) (0)
98#endif /* HAVE_SYSTEMTAP */
99
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200100#include <stat_item_internal.h>
101
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100102#define STATS_DEFAULT_INTERVAL 5 /* secs */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100103#define STATS_DEFAULT_BUFLEN 256
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200104
Vadim Yanitskiybfc83772021-11-08 23:14:29 +0300105LLIST_HEAD(osmo_stats_reporter_list);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100106static void *osmo_stats_ctx = NULL;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100107static int is_initialised = 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100108
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109static struct osmo_stats_config s_stats_config = {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100110 .interval = STATS_DEFAULT_INTERVAL,
111};
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112struct osmo_stats_config *osmo_stats_config = &s_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100113
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300114static struct osmo_fd osmo_stats_timer = { .fd = -1 };
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200115
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100117 const struct rate_ctr_group *ctrg,
118 const struct rate_ctr_desc *desc,
119 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
121 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100122 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100123
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100124static int update_srep_config(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200125{
126 int rc = 0;
127
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200128 if (srep->running) {
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100129 if (srep->close)
130 rc = srep->close(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200131 srep->running = 0;
132 }
133
134 if (!srep->enabled)
135 return rc;
136
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100137 if (srep->open)
138 rc = srep->open(srep);
139 else
140 rc = 0;
141
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200142 if (rc < 0)
143 srep->enabled = 0;
144 else
145 srep->running = 1;
146
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100147 srep->force_single_flush = 1;
148
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200149 return rc;
150}
151
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300152static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100153{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300154 uint64_t expire_count;
155 int rc;
156
157 /* check that the timer has actually expired */
158 if (!(what & OSMO_FD_READ))
159 return 0;
160
161 /* read from timerfd: number of expirations of periodic timer */
162 rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count));
163 if (rc < 0 && errno == EAGAIN)
164 return 0;
165 OSMO_ASSERT(rc == sizeof(expire_count));
166
167 if (expire_count > 1)
168 LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n",
169 expire_count, expire_count-1);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100170
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100171 if (!llist_empty(&osmo_stats_reporter_list))
172 osmo_stats_report();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100173
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300174 return 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100175}
176
177static int start_timer()
178{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300179 int rc;
180 int interval = osmo_stats_config->interval;
181
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100182 if (!is_initialised)
183 return -ESRCH;
184
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300185 struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000};
186 struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0};
187
188 rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);
189 if (rc < 0)
190 LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n",
191 rc, osmo_stats_timer.fd);
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300192
Daniel Willmann1a1de332020-07-14 18:11:14 +0200193 if (interval == 0) {
194 rc = osmo_timerfd_disable(&osmo_stats_timer);
195 if (rc < 0)
196 LOGP(DLSTATS, LOGL_ERROR, "Failed to disable the timer with error code %d (fd=%d)\n",
197 rc, osmo_stats_timer.fd);
198 } else {
199
200 rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval);
201 if (rc < 0)
202 LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n",
203 rc, osmo_stats_timer.fd, interval);
204
205 LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval);
206 }
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100207
208 return 0;
209}
210
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100211struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200212 const char *name)
213{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100214 struct osmo_stats_reporter *srep;
215 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200216 OSMO_ASSERT(srep);
217 srep->type = type;
218 if (name)
219 srep->name = talloc_strdup(srep, name);
220 srep->fd = -1;
221
Vadim Yanitskiy4e924722021-11-09 03:58:35 +0300222 llist_add_tail(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200223
224 return srep;
225}
226
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200227/*! Destroy a given stats_reporter. Takes care of first disabling it.
228 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100229void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200230{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100231 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200232 llist_del(&srep->list);
233 talloc_free(srep);
234}
235
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200236/*! Initialize the stats reporting module; call this once in your program.
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200237 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100238void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200239{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100240 osmo_stats_ctx = ctx;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100241 is_initialised = 1;
242 start_timer();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200243}
244
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200245/*! Find a stats_reporter of given \a type and \a name.
246 * \param[in] type Type of stats_reporter to find
247 * \param[in] name Name of stats_reporter to find
248 * \returns stats_reporter matching \a type and \a name; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100249struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200250 const char *name)
251{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100252 struct osmo_stats_reporter *srep;
253 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200254 if (srep->type != type)
255 continue;
256 if (srep->name != name) {
257 if (name == NULL || srep->name == NULL ||
258 strcmp(name, srep->name) != 0)
259 continue;
260 }
261 return srep;
262 }
263 return NULL;
264}
265
Harald Welte67bdd802017-01-15 17:56:11 +0100266#ifdef HAVE_SYS_SOCKET_H
267
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200268/*! Set the remote (IP) address of a given stats_reporter.
269 * \param[in] srep stats_reporter whose remote address is to be set
270 * \param[in] addr String representation of remote IPv4 address
271 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100272int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200273{
274 int rc;
275 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
276 struct in_addr inaddr;
277
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100278 if (!srep->have_net_config)
279 return -ENOTSUP;
280
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200281 OSMO_ASSERT(addr != NULL);
282
283 rc = inet_pton(AF_INET, addr, &inaddr);
284 if (rc <= 0)
285 return -EINVAL;
286
287 sock_addr->sin_addr = inaddr;
288 sock_addr->sin_family = AF_INET;
289 srep->dest_addr_len = sizeof(*sock_addr);
290
291 talloc_free(srep->dest_addr_str);
292 srep->dest_addr_str = talloc_strdup(srep, addr);
293
294 return update_srep_config(srep);
295}
296
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200297/*! Set the remote (UDP) port of a given stats_reporter
298 * \param[in] srep stats_reporter whose remote address is to be set
299 * \param[in] port UDP port of remote statsd to which we report
300 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100301int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200302{
303 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
304
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100305 if (!srep->have_net_config)
306 return -ENOTSUP;
307
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200308 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200309 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200310
311 return update_srep_config(srep);
312}
313
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200314/*! Set the local (IP) address of a given stats_reporter.
315 * \param[in] srep stats_reporter whose remote address is to be set
316 * \param[in] addr String representation of local IP address
317 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100318int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200319{
320 int rc;
321 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
322 struct in_addr inaddr;
323
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100324 if (!srep->have_net_config)
325 return -ENOTSUP;
326
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200327 if (addr) {
328 rc = inet_pton(AF_INET, addr, &inaddr);
329 if (rc <= 0)
330 return -EINVAL;
331 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100332 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200333 }
334
335 sock_addr->sin_addr = inaddr;
336 sock_addr->sin_family = AF_INET;
337 srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0;
338
339 talloc_free(srep->bind_addr_str);
340 srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL;
341
342 return update_srep_config(srep);
343}
344
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200345/*! Set the maximum transmission unit of a given stats_reporter.
346 * \param[in] srep stats_reporter whose remote address is to be set
347 * \param[in] mtu Maximum Transmission Unit of \a srep
348 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100349int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100350{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100351 if (!srep->have_net_config)
352 return -ENOTSUP;
353
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100354 if (mtu < 0)
355 return -EINVAL;
356
357 srep->mtu = mtu;
358
359 return update_srep_config(srep);
360}
Harald Welte67bdd802017-01-15 17:56:11 +0100361#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100362
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100363int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
364 enum osmo_stats_class class_id)
365{
366 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
367 return -EINVAL;
368
369 srep->max_class = class_id;
370
371 return 0;
372}
373
Alexander Chemerisf203ed32020-05-08 19:09:22 +0300374/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200375 * \param[in] interval Reporting interval in seconds
376 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100377int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200378{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200379 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100380 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200381
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100382 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100383 if (is_initialised)
384 start_timer();
385
386 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200387}
388
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300389/*! Set the regular flush period for a given stats_reporter
390 *
391 * Send all stats even if they have not changed (i.e. force the flush)
392 * every N-th reporting interval. Set to 0 to disable regular flush,
393 * set to 1 to flush every time, set to 2 to flush every 2nd time, etc.
394 * \param[in] srep stats_reporter to set flush period for
395 * \param[in] period Reporting interval in seconds
396 * \returns 0 on success; negative on error */
397int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period)
398{
399 srep->flush_period = period;
400 srep->flush_period_counter = 0;
401 /* force the flush now if it's not disabled by period=0 */
402 if (period > 0)
403 srep->force_single_flush = 1;
404
405 return 0;
406}
407
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200408/*! Set the name prefix of a given stats_reporter.
409 * \param[in] srep stats_reporter whose name prefix is to be set
Philipp Maier26728952021-12-14 18:49:05 +0100410 * \param[in] prefix Name prefix to pre-pend for any reported value
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200411 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100412int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200413{
414 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100415 srep->name_prefix = prefix && strlen(prefix) > 0 ?
416 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200417
418 return update_srep_config(srep);
419}
420
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200421
422/*! Enable the given stats_reporter.
423 * \param[in] srep stats_reporter who is to be enabled
424 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100425int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200426{
427 srep->enabled = 1;
428
429 return update_srep_config(srep);
430}
431
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200432/*! Disable the given stats_reporter.
433 * \param[in] srep stats_reporter who is to be disabled
434 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100435int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200436{
437 srep->enabled = 0;
438
439 return update_srep_config(srep);
440}
441
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100442/*** i/o helper functions ***/
443
Harald Welte67bdd802017-01-15 17:56:11 +0100444#ifdef HAVE_SYS_SOCKET_H
445
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200446/*! Open the UDP socket for given stats_reporter.
447 * \param[in] srep stats_reporter whose UDP socket is to be opened
448 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100449int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep)
450{
451 int sock;
452 int rc;
453 int buffer_size = STATS_DEFAULT_BUFLEN;
454
455 if (srep->fd != -1 && srep->close)
456 srep->close(srep);
457
458 sock = socket(AF_INET, SOCK_DGRAM, 0);
459 if (sock == -1)
460 return -errno;
461
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400462#if defined(__APPLE__) && !defined(MSG_NOSIGNAL)
463 {
464 static int val = 1;
465
466 rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val));
467 goto failed;
468 }
469#endif
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100470 if (srep->bind_addr_len > 0) {
471 rc = bind(sock, &srep->bind_addr, srep->bind_addr_len);
472 if (rc == -1)
473 goto failed;
474 }
475
476 srep->fd = sock;
477
478 if (srep->mtu > 0) {
479 buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */;
480 srep->agg_enabled = 1;
481 }
482
483 srep->buffer = msgb_alloc(buffer_size, "stats buffer");
484
485 return 0;
486
487failed:
488 rc = -errno;
489 close(sock);
490
491 return rc;
492}
493
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200494/*! Closee the UDP socket for given stats_reporter.
495 * \param[in] srep stats_reporter whose UDP socket is to be closed
496 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100497int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep)
498{
499 int rc;
500 if (srep->fd == -1)
501 return -EBADF;
502
503 osmo_stats_reporter_send_buffer(srep);
504
505 rc = close(srep->fd);
506 srep->fd = -1;
507 msgb_free(srep->buffer);
508 srep->buffer = NULL;
509 return rc == -1 ? -errno : 0;
510}
511
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200512/*! Send given date to given stats_reporter.
513 * \param[in] srep stats_reporter whose UDP socket is to be opened
514 * \param[in] data string data to be sent
515 * \param[in] data_len Length of \a data in bytes
516 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100517int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200518 int data_len)
519{
520 int rc;
521
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400522 rc = sendto(srep->fd, data, data_len,
523#ifdef MSG_NOSIGNAL
524 MSG_NOSIGNAL |
525#endif
526 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200527 &srep->dest_addr, srep->dest_addr_len);
528
529 if (rc == -1)
530 rc = -errno;
531
532 return rc;
533}
534
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200535/*! Send current accumulated buffer to given stats_reporter.
536 * \param[in] srep stats_reporter whose UDP socket is to be opened
537 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100538int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100539{
540 int rc;
541
542 if (!srep->buffer || msgb_length(srep->buffer) == 0)
543 return 0;
544
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100545 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100546 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
547
548 msgb_trim(srep->buffer, 0);
549
550 return rc;
551}
Harald Welte67bdd802017-01-15 17:56:11 +0100552#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100553
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100554/*** log reporter ***/
555
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200556/*! Create a stats_reporter that logs via libosmocore logging.
557 * A stats_reporter created via this function will simply print the statistics
558 * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO
559 * priority. The configuration of the libosmocore log targets define where this
560 * information will end up (ignored, text file, stderr, syslog, ...).
561 * \param[in] name Name of the to-be-created stats_reporter
562 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100563struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100564{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100565 struct osmo_stats_reporter *srep;
566 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100567
568 srep->have_net_config = 0;
569
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100570 srep->send_counter = osmo_stats_reporter_log_send_counter;
571 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100572
573 return srep;
574}
575
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100576static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100577 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100578 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100579 const char *unit)
580{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100581 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100582 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100583 type, srep->name_prefix ? srep->name_prefix : "",
584 name1 ? name1 : "", index1,
585 name2, value, unit ? unit : "");
586
587 return 0;
588}
589
590
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100591static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100592 const struct rate_ctr_group *ctrg,
593 const struct rate_ctr_desc *desc,
594 int64_t value, int64_t delta)
595{
596 if (ctrg)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100597 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100598 ctrg->desc->group_name_prefix,
599 ctrg->idx,
600 desc->name, value, NULL);
601 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100602 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100603 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100604 desc->name, value, NULL);
605}
606
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100607static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
608 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100609 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100610{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100611 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100612 statg->desc->group_name_prefix, statg->idx,
613 desc->name, value, desc->unit);
614}
615
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100616/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200617
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100618static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
619 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200620{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100621 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
622 class_id = index != 0 ?
623 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200624
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100625 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200626}
627
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200628/*** generic rate counter support ***/
629
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100630static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200631 const struct rate_ctr_group *ctrg,
632 const struct rate_ctr_desc *desc,
633 int64_t value, int64_t delta)
634{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100635 if (!srep->send_counter)
636 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200637
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100638 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200639}
640
641static int rate_ctr_handler(
642 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
643 const struct rate_ctr_desc *desc, void *sctx_)
644{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100645 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200646 int64_t delta = rate_ctr_difference(ctr);
647
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100648 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200649 if (!srep->running)
650 continue;
651
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100652 if (delta == 0 && !srep->force_single_flush)
653 continue;
654
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100655 if (!osmo_stats_reporter_check_config(srep,
656 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100657 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100658
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100659 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200660 ctr->current, delta);
661
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100662 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200663 }
664
665 return 0;
666}
667
668static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_)
669{
670 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_);
671
672 return 0;
673}
674
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100675/*** stat item support ***/
676
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100677static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep,
678 const struct osmo_stat_item_group *statg,
679 const struct osmo_stat_item_desc *desc,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100680 int32_t value)
681{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100682 if (!srep->send_item)
683 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100684
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100685 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100686}
687
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100688static int osmo_stat_item_handler(
689 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100690{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100691 struct osmo_stats_reporter *srep;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200692 int32_t prev_reported_value = item->reported.max;
693 int32_t new_value = item->value.max;
Oliver Smith11da4a42021-08-19 11:58:09 +0200694
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100695 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
696 if (!srep->running)
697 continue;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100698
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200699 /* If the previously reported value is the same as the current value, skip resending the value.
700 * However, if the stats reporter is set to resend all values, do resend the current value regardless of
701 * repetitions.
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200702 */
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200703 if (new_value == prev_reported_value && !srep->force_single_flush)
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100704 continue;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100705
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100706 if (!osmo_stats_reporter_check_config(srep,
707 statg->idx, statg->desc->class_id))
708 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100709
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200710 osmo_stats_reporter_send_item(srep, statg, item->desc, new_value);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100711 }
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100712
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200713 osmo_stat_item_flush(item);
714
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100715 return 0;
716}
717
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100718static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100719{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100720 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100721
722 return 0;
723}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200724
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100725/*** osmo counter support ***/
726
727static int handle_counter(struct osmo_counter *counter, void *sctx_)
728{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100729 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100730 struct rate_ctr_desc desc = {0};
731 /* Fake a rate counter description */
732 desc.name = counter->name;
733 desc.description = counter->description;
734
735 int delta = osmo_counter_difference(counter);
736
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100737 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100738 if (!srep->running)
739 continue;
740
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100741 if (delta == 0 && !srep->force_single_flush)
742 continue;
743
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100744 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100745 counter->value, delta);
746
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100747 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100748 }
749
750 return 0;
751}
752
753
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200754/*** main reporting function ***/
755
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100756static void flush_all_reporters()
757{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100758 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100759
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100760 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100761 if (!srep->running)
762 continue;
763
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100764 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300765
766 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100767 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300768 /* and schedule a new flush if it's time for it */
769 if (srep->flush_period > 0) {
770 srep->flush_period_counter++;
771 if (srep->flush_period_counter >= srep->flush_period) {
772 srep->force_single_flush = 1;
773 srep->flush_period_counter = 0;
774 }
775 }
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100776 }
777}
778
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100779int osmo_stats_report()
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200780{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100781 /* per group actions */
Harald Welte3217d512021-02-18 14:56:46 +0100782 TRACE(LIBOSMOCORE_STATS_START());
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100783 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200784 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100785 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200786
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100787 /* global actions */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100788 flush_all_reporters();
Harald Welte3217d512021-02-18 14:56:46 +0100789 TRACE(LIBOSMOCORE_STATS_DONE());
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100790
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200791 return 0;
792}
Harald Welte67bdd802017-01-15 17:56:11 +0100793
794#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200795
796/*! @} */