blob: f06515dd2ebaa45883cfa0f6b01d64cf3c24d3f6 [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 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
Harald Welteeb5b6ce2017-10-15 20:03:24 +020025/*! \addtogroup stats
26 * @{
27 *
28 * This module implements periodic reporting of statistics / counters.
29 * It supports the notion of multiple \ref osmo_stats_reporter objects
30 * which independently of each other can report statistics at different
31 * configurable intervals to different destinations.
32 *
33 * In order to use this facility, you have to call \ref
34 * osmo_stats_init() once at application start-up and then create one or
35 * more \ref osmo_stats_reporter, either using the direct API functions
36 * or by using the optional VTY bindings:
37 *
38 * - reporting to any of the libosmocore log targets
39 * \ref osmo_stats_reporter_create_log() creates a new stats_reporter
40 * which reports to the libosmcoore \ref logging subsystem.
41 *
42 * - reporting to statsd (a front-end proxy for the Graphite/Carbon
43 * metrics server
44 * \ref osmo_stats_reporter_create_statsd() creates a new stats_reporter
45 * which reports via UDP to statsd.
46 *
47 * You can either use the above API functions directly to create \ref
48 * osmo_stats_reporter instances, or you can use the VTY support
49 * contained in libosmovty. See the "stats" configuration node
50 * installed by osmo_stats_vty_Add_cmds().
51 *
52 * An \ref osmo_stats_reporter reports statistics on all of the following
53 * libosmocore internal counter/statistics objects:
54 * - \ref osmo_counter
55 * - \ref rate_ctr
56 * - \ref osmo_stat_item
57 *
58 * You do not need to do anything in particular to expose a given
59 * counter or stat_item, they are all exported automatically via any
60 * \ref osmo_stats_reporter. If you have multiple \ref
61 * osmo_stats_reporter, they will each report all counters/stat_items.
62 *
63 * \file stats.c */
64
Harald Welte67bdd802017-01-15 17:56:11 +010065#include "config.h"
66#if !defined(EMBEDDED)
67
Harald Welte95871da2017-05-15 12:11:36 +020068#include <osmocom/core/byteswap.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020069#include <osmocom/core/stats.h>
70
71#include <unistd.h>
72#include <string.h>
73#include <stdint.h>
74#include <errno.h>
75#include <stdio.h>
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000076#include <sys/types.h>
Alexander Chemerisf5bdef42020-05-09 01:57:51 +030077#include <inttypes.h>
Harald Welte67bdd802017-01-15 17:56:11 +010078
79#ifdef HAVE_SYS_SOCKET_H
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020080#include <sys/socket.h>
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000081#include <netinet/in.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020082#include <arpa/inet.h>
Harald Welte67bdd802017-01-15 17:56:11 +010083#endif
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020084
85#include <osmocom/core/utils.h>
86#include <osmocom/core/logging.h>
87#include <osmocom/core/rate_ctr.h>
88#include <osmocom/core/stat_item.h>
Alexander Chemerisf5bdef42020-05-09 01:57:51 +030089#include <osmocom/core/select.h>
Harald Welte216338c2017-10-15 19:46:19 +020090#include <osmocom/core/counter.h>
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010091#include <osmocom/core/msgb.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020092
Harald Welte3217d512021-02-18 14:56:46 +010093#ifdef HAVE_SYSTEMTAP
94/* include the generated probes header and put markers in code */
95#include "probes.h"
96#define TRACE(probe) probe
97#define TRACE_ENABLED(probe) probe ## _ENABLED()
98#else
99/* Wrap the probe to allow it to be removed when no systemtap available */
100#define TRACE(probe)
101#define TRACE_ENABLED(probe) (0)
102#endif /* HAVE_SYSTEMTAP */
103
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100104#define STATS_DEFAULT_INTERVAL 5 /* secs */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100105#define STATS_DEFAULT_BUFLEN 256
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200106
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107static LLIST_HEAD(osmo_stats_reporter_list);
108static void *osmo_stats_ctx = NULL;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100109static int is_initialised = 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111static struct osmo_stats_config s_stats_config = {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100112 .interval = STATS_DEFAULT_INTERVAL,
113};
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114struct osmo_stats_config *osmo_stats_config = &s_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100115
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300116static struct osmo_fd osmo_stats_timer = { .fd = -1 };
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200117
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100119 const struct rate_ctr_group *ctrg,
120 const struct rate_ctr_desc *desc,
121 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
123 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100124 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100125
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100126static int update_srep_config(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200127{
128 int rc = 0;
129
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200130 if (srep->running) {
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100131 if (srep->close)
132 rc = srep->close(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200133 srep->running = 0;
134 }
135
136 if (!srep->enabled)
137 return rc;
138
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100139 if (srep->open)
140 rc = srep->open(srep);
141 else
142 rc = 0;
143
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200144 if (rc < 0)
145 srep->enabled = 0;
146 else
147 srep->running = 1;
148
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100149 srep->force_single_flush = 1;
150
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200151 return rc;
152}
153
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300154static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100155{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300156 uint64_t expire_count;
157 int rc;
158
159 /* check that the timer has actually expired */
160 if (!(what & OSMO_FD_READ))
161 return 0;
162
163 /* read from timerfd: number of expirations of periodic timer */
164 rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count));
165 if (rc < 0 && errno == EAGAIN)
166 return 0;
167 OSMO_ASSERT(rc == sizeof(expire_count));
168
169 if (expire_count > 1)
170 LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n",
171 expire_count, expire_count-1);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100172
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100173 if (!llist_empty(&osmo_stats_reporter_list))
174 osmo_stats_report();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100175
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300176 return 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100177}
178
179static int start_timer()
180{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300181 int rc;
182 int interval = osmo_stats_config->interval;
183
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100184 if (!is_initialised)
185 return -ESRCH;
186
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300187 struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000};
188 struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0};
189
190 rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);
191 if (rc < 0)
192 LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n",
193 rc, osmo_stats_timer.fd);
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300194
Daniel Willmann1a1de332020-07-14 18:11:14 +0200195 if (interval == 0) {
196 rc = osmo_timerfd_disable(&osmo_stats_timer);
197 if (rc < 0)
198 LOGP(DLSTATS, LOGL_ERROR, "Failed to disable the timer with error code %d (fd=%d)\n",
199 rc, osmo_stats_timer.fd);
200 } else {
201
202 rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval);
203 if (rc < 0)
204 LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n",
205 rc, osmo_stats_timer.fd, interval);
206
207 LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval);
208 }
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100209
210 return 0;
211}
212
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100213struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200214 const char *name)
215{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100216 struct osmo_stats_reporter *srep;
217 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200218 OSMO_ASSERT(srep);
219 srep->type = type;
220 if (name)
221 srep->name = talloc_strdup(srep, name);
222 srep->fd = -1;
223
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100224 llist_add(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200225
226 return srep;
227}
228
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200229/*! Destroy a given stats_reporter. Takes care of first disabling it.
230 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100231void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200232{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100233 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200234 llist_del(&srep->list);
235 talloc_free(srep);
236}
237
Oliver Smith61401942021-03-26 10:18:37 +0100238static int osmo_stats_discard_item(struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
239{
240 return osmo_stat_item_discard(item, &item->stats_next_id);
241}
242
243static int osmo_stats_discard_group(struct osmo_stat_item_group *statg, void *sctx_)
244{
245 return osmo_stat_item_for_each_item(statg, &osmo_stats_discard_item, NULL);
246}
247
248static int osmo_stats_discard_all()
249{
250 return osmo_stat_item_for_each_group(&osmo_stats_discard_group, NULL);
251}
252
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200253/*! Initilize the stats reporting module; call this once in your program
254 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100255void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200256{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100257 osmo_stats_ctx = ctx;
Oliver Smith61401942021-03-26 10:18:37 +0100258 osmo_stats_discard_all();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100259
260 is_initialised = 1;
261 start_timer();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200262}
263
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200264/*! Find a stats_reporter of given \a type and \a name.
265 * \param[in] type Type of stats_reporter to find
266 * \param[in] name Name of stats_reporter to find
267 * \returns stats_reporter matching \a type and \a name; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100268struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200269 const char *name)
270{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100271 struct osmo_stats_reporter *srep;
272 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200273 if (srep->type != type)
274 continue;
275 if (srep->name != name) {
276 if (name == NULL || srep->name == NULL ||
277 strcmp(name, srep->name) != 0)
278 continue;
279 }
280 return srep;
281 }
282 return NULL;
283}
284
Harald Welte67bdd802017-01-15 17:56:11 +0100285#ifdef HAVE_SYS_SOCKET_H
286
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200287/*! Set the remote (IP) address of a given stats_reporter.
288 * \param[in] srep stats_reporter whose remote address is to be set
289 * \param[in] addr String representation of remote IPv4 address
290 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100291int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200292{
293 int rc;
294 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
295 struct in_addr inaddr;
296
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100297 if (!srep->have_net_config)
298 return -ENOTSUP;
299
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200300 OSMO_ASSERT(addr != NULL);
301
302 rc = inet_pton(AF_INET, addr, &inaddr);
303 if (rc <= 0)
304 return -EINVAL;
305
306 sock_addr->sin_addr = inaddr;
307 sock_addr->sin_family = AF_INET;
308 srep->dest_addr_len = sizeof(*sock_addr);
309
310 talloc_free(srep->dest_addr_str);
311 srep->dest_addr_str = talloc_strdup(srep, addr);
312
313 return update_srep_config(srep);
314}
315
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200316/*! Set the remote (UDP) port of a given stats_reporter
317 * \param[in] srep stats_reporter whose remote address is to be set
318 * \param[in] port UDP port of remote statsd to which we report
319 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100320int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200321{
322 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
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 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200328 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200329
330 return update_srep_config(srep);
331}
332
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200333/*! Set the local (IP) address of a given stats_reporter.
334 * \param[in] srep stats_reporter whose remote address is to be set
335 * \param[in] addr String representation of local IP address
336 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100337int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200338{
339 int rc;
340 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
341 struct in_addr inaddr;
342
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100343 if (!srep->have_net_config)
344 return -ENOTSUP;
345
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200346 if (addr) {
347 rc = inet_pton(AF_INET, addr, &inaddr);
348 if (rc <= 0)
349 return -EINVAL;
350 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100351 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200352 }
353
354 sock_addr->sin_addr = inaddr;
355 sock_addr->sin_family = AF_INET;
356 srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0;
357
358 talloc_free(srep->bind_addr_str);
359 srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL;
360
361 return update_srep_config(srep);
362}
363
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200364/*! Set the maximum transmission unit of a given stats_reporter.
365 * \param[in] srep stats_reporter whose remote address is to be set
366 * \param[in] mtu Maximum Transmission Unit of \a srep
367 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100368int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100369{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100370 if (!srep->have_net_config)
371 return -ENOTSUP;
372
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100373 if (mtu < 0)
374 return -EINVAL;
375
376 srep->mtu = mtu;
377
378 return update_srep_config(srep);
379}
Harald Welte67bdd802017-01-15 17:56:11 +0100380#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100381
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100382int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
383 enum osmo_stats_class class_id)
384{
385 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
386 return -EINVAL;
387
388 srep->max_class = class_id;
389
390 return 0;
391}
392
Alexander Chemerisf203ed32020-05-08 19:09:22 +0300393/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200394 * \param[in] interval Reporting interval in seconds
395 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100396int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200397{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200398 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100399 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200400
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100401 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100402 if (is_initialised)
403 start_timer();
404
405 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200406}
407
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300408/*! Set the regular flush period for a given stats_reporter
409 *
410 * Send all stats even if they have not changed (i.e. force the flush)
411 * every N-th reporting interval. Set to 0 to disable regular flush,
412 * set to 1 to flush every time, set to 2 to flush every 2nd time, etc.
413 * \param[in] srep stats_reporter to set flush period for
414 * \param[in] period Reporting interval in seconds
415 * \returns 0 on success; negative on error */
416int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period)
417{
418 srep->flush_period = period;
419 srep->flush_period_counter = 0;
420 /* force the flush now if it's not disabled by period=0 */
421 if (period > 0)
422 srep->force_single_flush = 1;
423
424 return 0;
425}
426
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200427/*! Set the name prefix of a given stats_reporter.
428 * \param[in] srep stats_reporter whose name prefix is to be set
429 * \param[in] prefix NAme perfix to pre-pend for any reported value
430 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100431int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200432{
433 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100434 srep->name_prefix = prefix && strlen(prefix) > 0 ?
435 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200436
437 return update_srep_config(srep);
438}
439
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200440
441/*! Enable the given stats_reporter.
442 * \param[in] srep stats_reporter who is to be enabled
443 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100444int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200445{
446 srep->enabled = 1;
447
448 return update_srep_config(srep);
449}
450
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200451/*! Disable the given stats_reporter.
452 * \param[in] srep stats_reporter who is to be disabled
453 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100454int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200455{
456 srep->enabled = 0;
457
458 return update_srep_config(srep);
459}
460
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100461/*** i/o helper functions ***/
462
Harald Welte67bdd802017-01-15 17:56:11 +0100463#ifdef HAVE_SYS_SOCKET_H
464
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200465/*! Open the UDP socket for given stats_reporter.
466 * \param[in] srep stats_reporter whose UDP socket is to be opened
467 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100468int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep)
469{
470 int sock;
471 int rc;
472 int buffer_size = STATS_DEFAULT_BUFLEN;
473
474 if (srep->fd != -1 && srep->close)
475 srep->close(srep);
476
477 sock = socket(AF_INET, SOCK_DGRAM, 0);
478 if (sock == -1)
479 return -errno;
480
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400481#if defined(__APPLE__) && !defined(MSG_NOSIGNAL)
482 {
483 static int val = 1;
484
485 rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val));
486 goto failed;
487 }
488#endif
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100489 if (srep->bind_addr_len > 0) {
490 rc = bind(sock, &srep->bind_addr, srep->bind_addr_len);
491 if (rc == -1)
492 goto failed;
493 }
494
495 srep->fd = sock;
496
497 if (srep->mtu > 0) {
498 buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */;
499 srep->agg_enabled = 1;
500 }
501
502 srep->buffer = msgb_alloc(buffer_size, "stats buffer");
503
504 return 0;
505
506failed:
507 rc = -errno;
508 close(sock);
509
510 return rc;
511}
512
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200513/*! Closee the UDP socket for given stats_reporter.
514 * \param[in] srep stats_reporter whose UDP socket is to be closed
515 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100516int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep)
517{
518 int rc;
519 if (srep->fd == -1)
520 return -EBADF;
521
522 osmo_stats_reporter_send_buffer(srep);
523
524 rc = close(srep->fd);
525 srep->fd = -1;
526 msgb_free(srep->buffer);
527 srep->buffer = NULL;
528 return rc == -1 ? -errno : 0;
529}
530
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200531/*! Send given date to given stats_reporter.
532 * \param[in] srep stats_reporter whose UDP socket is to be opened
533 * \param[in] data string data to be sent
534 * \param[in] data_len Length of \a data in bytes
535 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100536int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200537 int data_len)
538{
539 int rc;
540
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400541 rc = sendto(srep->fd, data, data_len,
542#ifdef MSG_NOSIGNAL
543 MSG_NOSIGNAL |
544#endif
545 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200546 &srep->dest_addr, srep->dest_addr_len);
547
548 if (rc == -1)
549 rc = -errno;
550
551 return rc;
552}
553
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200554/*! Send current accumulated buffer to given stats_reporter.
555 * \param[in] srep stats_reporter whose UDP socket is to be opened
556 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100557int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100558{
559 int rc;
560
561 if (!srep->buffer || msgb_length(srep->buffer) == 0)
562 return 0;
563
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100564 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100565 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
566
567 msgb_trim(srep->buffer, 0);
568
569 return rc;
570}
Harald Welte67bdd802017-01-15 17:56:11 +0100571#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100572
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100573/*** log reporter ***/
574
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200575/*! Create a stats_reporter that logs via libosmocore logging.
576 * A stats_reporter created via this function will simply print the statistics
577 * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO
578 * priority. The configuration of the libosmocore log targets define where this
579 * information will end up (ignored, text file, stderr, syslog, ...).
580 * \param[in] name Name of the to-be-created stats_reporter
581 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100582struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100583{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100584 struct osmo_stats_reporter *srep;
585 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100586
587 srep->have_net_config = 0;
588
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100589 srep->send_counter = osmo_stats_reporter_log_send_counter;
590 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100591
592 return srep;
593}
594
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100595static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100596 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100597 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100598 const char *unit)
599{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100600 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100601 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100602 type, srep->name_prefix ? srep->name_prefix : "",
603 name1 ? name1 : "", index1,
604 name2, value, unit ? unit : "");
605
606 return 0;
607}
608
609
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100610static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100611 const struct rate_ctr_group *ctrg,
612 const struct rate_ctr_desc *desc,
613 int64_t value, int64_t delta)
614{
615 if (ctrg)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100616 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100617 ctrg->desc->group_name_prefix,
618 ctrg->idx,
619 desc->name, value, NULL);
620 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100621 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100622 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100623 desc->name, value, NULL);
624}
625
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100626static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
627 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100628 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100629{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100630 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100631 statg->desc->group_name_prefix, statg->idx,
632 desc->name, value, desc->unit);
633}
634
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100635/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200636
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100637static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
638 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200639{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100640 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
641 class_id = index != 0 ?
642 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200643
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100644 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200645}
646
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200647/*** generic rate counter support ***/
648
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100649static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200650 const struct rate_ctr_group *ctrg,
651 const struct rate_ctr_desc *desc,
652 int64_t value, int64_t delta)
653{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100654 if (!srep->send_counter)
655 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200656
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100657 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200658}
659
660static int rate_ctr_handler(
661 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
662 const struct rate_ctr_desc *desc, void *sctx_)
663{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100664 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200665 int64_t delta = rate_ctr_difference(ctr);
666
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100667 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200668 if (!srep->running)
669 continue;
670
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100671 if (delta == 0 && !srep->force_single_flush)
672 continue;
673
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100674 if (!osmo_stats_reporter_check_config(srep,
675 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100676 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100677
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100678 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200679 ctr->current, delta);
680
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100681 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200682 }
683
684 return 0;
685}
686
687static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_)
688{
689 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_);
690
691 return 0;
692}
693
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100694/*** stat item support ***/
695
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100696static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep,
697 const struct osmo_stat_item_group *statg,
698 const struct osmo_stat_item_desc *desc,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100699 int32_t value)
700{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100701 if (!srep->send_item)
702 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100703
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100704 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100705}
706
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100707static int osmo_stat_item_handler(
708 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100709{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100710 struct osmo_stats_reporter *srep;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100711 int32_t value;
Oliver Smithd4393602021-04-06 11:53:42 +0200712 bool have_value;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100713
Oliver Smith61401942021-03-26 10:18:37 +0100714 have_value = osmo_stat_item_get_next(item, &item->stats_next_id, &value) > 0;
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100715 if (!have_value) {
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100716 /* Send the last value in case a flush is requested */
717 value = osmo_stat_item_get_last(item);
Oliver Smith11da4a42021-08-19 11:58:09 +0200718
719 /* Also send it in case a different max value was sent
720 * previously (OS#5215) */
721 if (!item->stats_last_sent_was_max)
722 have_value = 1;
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100723 } else {
724 int32_t next_val;
725 /* If we have multiple values only send the max */
Oliver Smith61401942021-03-26 10:18:37 +0100726 while (osmo_stat_item_get_next(item, &item->stats_next_id, &next_val) > 0)
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100727 value = OSMO_MAX(value, next_val);
728 }
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100729
Oliver Smith11da4a42021-08-19 11:58:09 +0200730 item->stats_last_sent_was_max = (osmo_stat_item_get_last(item) == value);
731
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100732 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
733 if (!srep->running)
734 continue;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100735
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100736 if (!have_value && !srep->force_single_flush)
737 continue;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100738
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100739 if (!osmo_stats_reporter_check_config(srep,
740 statg->idx, statg->desc->class_id))
741 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100742
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100743 osmo_stats_reporter_send_item(srep, statg,
744 item->desc, value);
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100745
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100746 }
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100747
748 return 0;
749}
750
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100751static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100752{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100753 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100754
755 return 0;
756}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200757
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100758/*** osmo counter support ***/
759
760static int handle_counter(struct osmo_counter *counter, void *sctx_)
761{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100762 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100763 struct rate_ctr_desc desc = {0};
764 /* Fake a rate counter description */
765 desc.name = counter->name;
766 desc.description = counter->description;
767
768 int delta = osmo_counter_difference(counter);
769
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100770 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100771 if (!srep->running)
772 continue;
773
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100774 if (delta == 0 && !srep->force_single_flush)
775 continue;
776
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100777 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100778 counter->value, delta);
779
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100780 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100781 }
782
783 return 0;
784}
785
786
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200787/*** main reporting function ***/
788
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100789static void flush_all_reporters()
790{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100791 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100792
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100793 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100794 if (!srep->running)
795 continue;
796
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100797 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300798
799 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100800 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300801 /* and schedule a new flush if it's time for it */
802 if (srep->flush_period > 0) {
803 srep->flush_period_counter++;
804 if (srep->flush_period_counter >= srep->flush_period) {
805 srep->force_single_flush = 1;
806 srep->flush_period_counter = 0;
807 }
808 }
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100809 }
810}
811
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100812int osmo_stats_report()
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200813{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100814 /* per group actions */
Harald Welte3217d512021-02-18 14:56:46 +0100815 TRACE(LIBOSMOCORE_STATS_START());
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100816 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200817 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100818 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200819
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100820 /* global actions */
Oliver Smith61401942021-03-26 10:18:37 +0100821 osmo_stats_discard_all();
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100822 flush_all_reporters();
Harald Welte3217d512021-02-18 14:56:46 +0100823 TRACE(LIBOSMOCORE_STATS_DONE());
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100824
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200825 return 0;
826}
Harald Welte67bdd802017-01-15 17:56:11 +0100827
828#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200829
830/*! @} */