blob: a5a4d4bcad049d0d929f5412885175f3e1fa6281 [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
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +010093#define STATS_DEFAULT_INTERVAL 5 /* secs */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010094#define STATS_DEFAULT_BUFLEN 256
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020095
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010096static LLIST_HEAD(osmo_stats_reporter_list);
97static void *osmo_stats_ctx = NULL;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +010098static int is_initialised = 0;
99static int32_t current_stat_item_index = 0;
100
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101static struct osmo_stats_config s_stats_config = {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100102 .interval = STATS_DEFAULT_INTERVAL,
103};
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100104struct osmo_stats_config *osmo_stats_config = &s_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100105
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300106static struct osmo_fd osmo_stats_timer = { .fd = -1 };
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200107
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100109 const struct rate_ctr_group *ctrg,
110 const struct rate_ctr_desc *desc,
111 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
113 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100114 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100115
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116static int update_srep_config(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200117{
118 int rc = 0;
119
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200120 if (srep->running) {
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100121 if (srep->close)
122 rc = srep->close(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200123 srep->running = 0;
124 }
125
126 if (!srep->enabled)
127 return rc;
128
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100129 if (srep->open)
130 rc = srep->open(srep);
131 else
132 rc = 0;
133
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200134 if (rc < 0)
135 srep->enabled = 0;
136 else
137 srep->running = 1;
138
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100139 srep->force_single_flush = 1;
140
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200141 return rc;
142}
143
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300144static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100145{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300146 uint64_t expire_count;
147 int rc;
148
149 /* check that the timer has actually expired */
150 if (!(what & OSMO_FD_READ))
151 return 0;
152
153 /* read from timerfd: number of expirations of periodic timer */
154 rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count));
155 if (rc < 0 && errno == EAGAIN)
156 return 0;
157 OSMO_ASSERT(rc == sizeof(expire_count));
158
159 if (expire_count > 1)
160 LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n",
161 expire_count, expire_count-1);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100162
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100163 if (!llist_empty(&osmo_stats_reporter_list))
164 osmo_stats_report();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100165
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300166 return 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100167}
168
169static int start_timer()
170{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300171 int rc;
172 int interval = osmo_stats_config->interval;
173
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100174 if (!is_initialised)
175 return -ESRCH;
176
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300177 struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000};
178 struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0};
179
180 rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);
181 if (rc < 0)
182 LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n",
183 rc, osmo_stats_timer.fd);
184 rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval);
185 if (rc < 0)
186 LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n",
187 rc, osmo_stats_timer.fd, interval);
188
189 LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100190
191 return 0;
192}
193
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100194struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200195 const char *name)
196{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100197 struct osmo_stats_reporter *srep;
198 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200199 OSMO_ASSERT(srep);
200 srep->type = type;
201 if (name)
202 srep->name = talloc_strdup(srep, name);
203 srep->fd = -1;
204
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100205 llist_add(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200206
207 return srep;
208}
209
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200210/*! Destroy a given stats_reporter. Takes care of first disabling it.
211 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100212void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200213{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100214 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200215 llist_del(&srep->list);
216 talloc_free(srep);
217}
218
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200219/*! Initilize the stats reporting module; call this once in your program
220 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100221void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200222{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100223 osmo_stats_ctx = ctx;
224 osmo_stat_item_discard_all(&current_stat_item_index);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100225
226 is_initialised = 1;
227 start_timer();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200228}
229
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200230/*! Find a stats_reporter of given \a type and \a name.
231 * \param[in] type Type of stats_reporter to find
232 * \param[in] name Name of stats_reporter to find
233 * \returns stats_reporter matching \a type and \a name; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100234struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200235 const char *name)
236{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100237 struct osmo_stats_reporter *srep;
238 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200239 if (srep->type != type)
240 continue;
241 if (srep->name != name) {
242 if (name == NULL || srep->name == NULL ||
243 strcmp(name, srep->name) != 0)
244 continue;
245 }
246 return srep;
247 }
248 return NULL;
249}
250
Harald Welte67bdd802017-01-15 17:56:11 +0100251#ifdef HAVE_SYS_SOCKET_H
252
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200253/*! Set the remote (IP) address of a given stats_reporter.
254 * \param[in] srep stats_reporter whose remote address is to be set
255 * \param[in] addr String representation of remote IPv4 address
256 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100257int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200258{
259 int rc;
260 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
261 struct in_addr inaddr;
262
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100263 if (!srep->have_net_config)
264 return -ENOTSUP;
265
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200266 OSMO_ASSERT(addr != NULL);
267
268 rc = inet_pton(AF_INET, addr, &inaddr);
269 if (rc <= 0)
270 return -EINVAL;
271
272 sock_addr->sin_addr = inaddr;
273 sock_addr->sin_family = AF_INET;
274 srep->dest_addr_len = sizeof(*sock_addr);
275
276 talloc_free(srep->dest_addr_str);
277 srep->dest_addr_str = talloc_strdup(srep, addr);
278
279 return update_srep_config(srep);
280}
281
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200282/*! Set the remote (UDP) port of a given stats_reporter
283 * \param[in] srep stats_reporter whose remote address is to be set
284 * \param[in] port UDP port of remote statsd to which we report
285 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100286int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200287{
288 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
289
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100290 if (!srep->have_net_config)
291 return -ENOTSUP;
292
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200293 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200294 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200295
296 return update_srep_config(srep);
297}
298
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200299/*! Set the local (IP) address of a given stats_reporter.
300 * \param[in] srep stats_reporter whose remote address is to be set
301 * \param[in] addr String representation of local IP address
302 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100303int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200304{
305 int rc;
306 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
307 struct in_addr inaddr;
308
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100309 if (!srep->have_net_config)
310 return -ENOTSUP;
311
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200312 if (addr) {
313 rc = inet_pton(AF_INET, addr, &inaddr);
314 if (rc <= 0)
315 return -EINVAL;
316 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100317 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200318 }
319
320 sock_addr->sin_addr = inaddr;
321 sock_addr->sin_family = AF_INET;
322 srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0;
323
324 talloc_free(srep->bind_addr_str);
325 srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL;
326
327 return update_srep_config(srep);
328}
329
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200330/*! Set the maximum transmission unit of a given stats_reporter.
331 * \param[in] srep stats_reporter whose remote address is to be set
332 * \param[in] mtu Maximum Transmission Unit of \a srep
333 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100334int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100335{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100336 if (!srep->have_net_config)
337 return -ENOTSUP;
338
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100339 if (mtu < 0)
340 return -EINVAL;
341
342 srep->mtu = mtu;
343
344 return update_srep_config(srep);
345}
Harald Welte67bdd802017-01-15 17:56:11 +0100346#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100347
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100348int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
349 enum osmo_stats_class class_id)
350{
351 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
352 return -EINVAL;
353
354 srep->max_class = class_id;
355
356 return 0;
357}
358
Alexander Chemerisf203ed32020-05-08 19:09:22 +0300359/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200360 * \param[in] interval Reporting interval in seconds
361 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100362int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200363{
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100364 if (interval <= 0)
365 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200366
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100367 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100368 if (is_initialised)
369 start_timer();
370
371 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200372}
373
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300374/*! Set the regular flush period for a given stats_reporter
375 *
376 * Send all stats even if they have not changed (i.e. force the flush)
377 * every N-th reporting interval. Set to 0 to disable regular flush,
378 * set to 1 to flush every time, set to 2 to flush every 2nd time, etc.
379 * \param[in] srep stats_reporter to set flush period for
380 * \param[in] period Reporting interval in seconds
381 * \returns 0 on success; negative on error */
382int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period)
383{
384 srep->flush_period = period;
385 srep->flush_period_counter = 0;
386 /* force the flush now if it's not disabled by period=0 */
387 if (period > 0)
388 srep->force_single_flush = 1;
389
390 return 0;
391}
392
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200393/*! Set the name prefix of a given stats_reporter.
394 * \param[in] srep stats_reporter whose name prefix is to be set
395 * \param[in] prefix NAme perfix to pre-pend for any reported value
396 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100397int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200398{
399 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100400 srep->name_prefix = prefix && strlen(prefix) > 0 ?
401 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200402
403 return update_srep_config(srep);
404}
405
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200406
407/*! Enable the given stats_reporter.
408 * \param[in] srep stats_reporter who is to be enabled
409 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100410int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200411{
412 srep->enabled = 1;
413
414 return update_srep_config(srep);
415}
416
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200417/*! Disable the given stats_reporter.
418 * \param[in] srep stats_reporter who is to be disabled
419 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100420int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200421{
422 srep->enabled = 0;
423
424 return update_srep_config(srep);
425}
426
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100427/*** i/o helper functions ***/
428
Harald Welte67bdd802017-01-15 17:56:11 +0100429#ifdef HAVE_SYS_SOCKET_H
430
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200431/*! Open the UDP socket for given stats_reporter.
432 * \param[in] srep stats_reporter whose UDP socket is to be opened
433 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100434int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep)
435{
436 int sock;
437 int rc;
438 int buffer_size = STATS_DEFAULT_BUFLEN;
439
440 if (srep->fd != -1 && srep->close)
441 srep->close(srep);
442
443 sock = socket(AF_INET, SOCK_DGRAM, 0);
444 if (sock == -1)
445 return -errno;
446
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400447#if defined(__APPLE__) && !defined(MSG_NOSIGNAL)
448 {
449 static int val = 1;
450
451 rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val));
452 goto failed;
453 }
454#endif
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100455 if (srep->bind_addr_len > 0) {
456 rc = bind(sock, &srep->bind_addr, srep->bind_addr_len);
457 if (rc == -1)
458 goto failed;
459 }
460
461 srep->fd = sock;
462
463 if (srep->mtu > 0) {
464 buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */;
465 srep->agg_enabled = 1;
466 }
467
468 srep->buffer = msgb_alloc(buffer_size, "stats buffer");
469
470 return 0;
471
472failed:
473 rc = -errno;
474 close(sock);
475
476 return rc;
477}
478
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200479/*! Closee the UDP socket for given stats_reporter.
480 * \param[in] srep stats_reporter whose UDP socket is to be closed
481 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100482int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep)
483{
484 int rc;
485 if (srep->fd == -1)
486 return -EBADF;
487
488 osmo_stats_reporter_send_buffer(srep);
489
490 rc = close(srep->fd);
491 srep->fd = -1;
492 msgb_free(srep->buffer);
493 srep->buffer = NULL;
494 return rc == -1 ? -errno : 0;
495}
496
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200497/*! Send given date to given stats_reporter.
498 * \param[in] srep stats_reporter whose UDP socket is to be opened
499 * \param[in] data string data to be sent
500 * \param[in] data_len Length of \a data in bytes
501 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100502int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200503 int data_len)
504{
505 int rc;
506
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400507 rc = sendto(srep->fd, data, data_len,
508#ifdef MSG_NOSIGNAL
509 MSG_NOSIGNAL |
510#endif
511 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200512 &srep->dest_addr, srep->dest_addr_len);
513
514 if (rc == -1)
515 rc = -errno;
516
517 return rc;
518}
519
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200520/*! Send current accumulated buffer to given stats_reporter.
521 * \param[in] srep stats_reporter whose UDP socket is to be opened
522 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100523int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100524{
525 int rc;
526
527 if (!srep->buffer || msgb_length(srep->buffer) == 0)
528 return 0;
529
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100530 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100531 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
532
533 msgb_trim(srep->buffer, 0);
534
535 return rc;
536}
Harald Welte67bdd802017-01-15 17:56:11 +0100537#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100538
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100539/*** log reporter ***/
540
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200541/*! Create a stats_reporter that logs via libosmocore logging.
542 * A stats_reporter created via this function will simply print the statistics
543 * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO
544 * priority. The configuration of the libosmocore log targets define where this
545 * information will end up (ignored, text file, stderr, syslog, ...).
546 * \param[in] name Name of the to-be-created stats_reporter
547 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100548struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100549{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100550 struct osmo_stats_reporter *srep;
551 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100552
553 srep->have_net_config = 0;
554
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100555 srep->send_counter = osmo_stats_reporter_log_send_counter;
556 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100557
558 return srep;
559}
560
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100561static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100562 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100563 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100564 const char *unit)
565{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100566 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100567 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100568 type, srep->name_prefix ? srep->name_prefix : "",
569 name1 ? name1 : "", index1,
570 name2, value, unit ? unit : "");
571
572 return 0;
573}
574
575
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100576static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100577 const struct rate_ctr_group *ctrg,
578 const struct rate_ctr_desc *desc,
579 int64_t value, int64_t delta)
580{
581 if (ctrg)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100582 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100583 ctrg->desc->group_name_prefix,
584 ctrg->idx,
585 desc->name, value, NULL);
586 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100587 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100588 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100589 desc->name, value, NULL);
590}
591
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100592static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
593 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100594 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100595{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100596 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100597 statg->desc->group_name_prefix, statg->idx,
598 desc->name, value, desc->unit);
599}
600
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100601/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200602
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100603static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
604 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200605{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100606 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
607 class_id = index != 0 ?
608 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200609
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100610 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200611}
612
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200613/*** generic rate counter support ***/
614
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100615static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200616 const struct rate_ctr_group *ctrg,
617 const struct rate_ctr_desc *desc,
618 int64_t value, int64_t delta)
619{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100620 if (!srep->send_counter)
621 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200622
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100623 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200624}
625
626static int rate_ctr_handler(
627 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
628 const struct rate_ctr_desc *desc, void *sctx_)
629{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100630 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200631 int64_t delta = rate_ctr_difference(ctr);
632
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100633 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200634 if (!srep->running)
635 continue;
636
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100637 if (delta == 0 && !srep->force_single_flush)
638 continue;
639
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100640 if (!osmo_stats_reporter_check_config(srep,
641 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100642 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100643
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100644 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200645 ctr->current, delta);
646
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100647 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200648 }
649
650 return 0;
651}
652
653static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_)
654{
655 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_);
656
657 return 0;
658}
659
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100660/*** stat item support ***/
661
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100662static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep,
663 const struct osmo_stat_item_group *statg,
664 const struct osmo_stat_item_desc *desc,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100665 int32_t value)
666{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100667 if (!srep->send_item)
668 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100669
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100670 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100671}
672
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100673static int osmo_stat_item_handler(
674 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100675{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100676 struct osmo_stats_reporter *srep;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100677 int32_t idx = current_stat_item_index;
678 int32_t value;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100679 int have_value;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100680
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100681 have_value = osmo_stat_item_get_next(item, &idx, &value) > 0;
682 if (!have_value)
683 /* Send the last value in case a flush is requested */
684 value = osmo_stat_item_get_last(item);
685
686 do {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100687 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100688 if (!srep->running)
689 continue;
690
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100691 if (!have_value && !srep->force_single_flush)
692 continue;
693
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100694 if (!osmo_stats_reporter_check_config(srep,
695 statg->idx, statg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100696 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100697
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100698 osmo_stats_reporter_send_item(srep, statg,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100699 item->desc, value);
700 }
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100701
702 if (!have_value)
703 break;
704
705 have_value = osmo_stat_item_get_next(item, &idx, &value) > 0;
706 } while (have_value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100707
708 return 0;
709}
710
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100711static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100712{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100713 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100714
715 return 0;
716}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200717
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100718/*** osmo counter support ***/
719
720static int handle_counter(struct osmo_counter *counter, void *sctx_)
721{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100722 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100723 struct rate_ctr_desc desc = {0};
724 /* Fake a rate counter description */
725 desc.name = counter->name;
726 desc.description = counter->description;
727
728 int delta = osmo_counter_difference(counter);
729
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100730 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100731 if (!srep->running)
732 continue;
733
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100734 if (delta == 0 && !srep->force_single_flush)
735 continue;
736
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100737 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100738 counter->value, delta);
739
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100740 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100741 }
742
743 return 0;
744}
745
746
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200747/*** main reporting function ***/
748
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100749static void flush_all_reporters()
750{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100751 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100752
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100753 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100754 if (!srep->running)
755 continue;
756
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100757 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300758
759 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100760 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300761 /* and schedule a new flush if it's time for it */
762 if (srep->flush_period > 0) {
763 srep->flush_period_counter++;
764 if (srep->flush_period_counter >= srep->flush_period) {
765 srep->force_single_flush = 1;
766 srep->flush_period_counter = 0;
767 }
768 }
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100769 }
770}
771
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100772int osmo_stats_report()
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200773{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100774 /* per group actions */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100775 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200776 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100777 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200778
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100779 /* global actions */
780 osmo_stat_item_discard_all(&current_stat_item_index);
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100781 flush_all_reporters();
782
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200783 return 0;
784}
Harald Welte67bdd802017-01-15 17:56:11 +0100785
786#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200787
788/*! @} */