blob: c91a97800bb46ac6ba699ca43ece16d0fa21fa09 [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);
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300184
Daniel Willmann1a1de332020-07-14 18:11:14 +0200185 if (interval == 0) {
186 rc = osmo_timerfd_disable(&osmo_stats_timer);
187 if (rc < 0)
188 LOGP(DLSTATS, LOGL_ERROR, "Failed to disable the timer with error code %d (fd=%d)\n",
189 rc, osmo_stats_timer.fd);
190 } else {
191
192 rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval);
193 if (rc < 0)
194 LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n",
195 rc, osmo_stats_timer.fd, interval);
196
197 LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval);
198 }
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100199
200 return 0;
201}
202
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100203struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200204 const char *name)
205{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100206 struct osmo_stats_reporter *srep;
207 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200208 OSMO_ASSERT(srep);
209 srep->type = type;
210 if (name)
211 srep->name = talloc_strdup(srep, name);
212 srep->fd = -1;
213
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100214 llist_add(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200215
216 return srep;
217}
218
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200219/*! Destroy a given stats_reporter. Takes care of first disabling it.
220 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100221void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200222{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100223 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200224 llist_del(&srep->list);
225 talloc_free(srep);
226}
227
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200228/*! Initilize the stats reporting module; call this once in your program
229 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100230void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200231{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100232 osmo_stats_ctx = ctx;
233 osmo_stat_item_discard_all(&current_stat_item_index);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100234
235 is_initialised = 1;
236 start_timer();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200237}
238
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200239/*! Find a stats_reporter of given \a type and \a name.
240 * \param[in] type Type of stats_reporter to find
241 * \param[in] name Name of stats_reporter to find
242 * \returns stats_reporter matching \a type and \a name; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100243struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200244 const char *name)
245{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100246 struct osmo_stats_reporter *srep;
247 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200248 if (srep->type != type)
249 continue;
250 if (srep->name != name) {
251 if (name == NULL || srep->name == NULL ||
252 strcmp(name, srep->name) != 0)
253 continue;
254 }
255 return srep;
256 }
257 return NULL;
258}
259
Harald Welte67bdd802017-01-15 17:56:11 +0100260#ifdef HAVE_SYS_SOCKET_H
261
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200262/*! Set the remote (IP) address of a given stats_reporter.
263 * \param[in] srep stats_reporter whose remote address is to be set
264 * \param[in] addr String representation of remote IPv4 address
265 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100266int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200267{
268 int rc;
269 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
270 struct in_addr inaddr;
271
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100272 if (!srep->have_net_config)
273 return -ENOTSUP;
274
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200275 OSMO_ASSERT(addr != NULL);
276
277 rc = inet_pton(AF_INET, addr, &inaddr);
278 if (rc <= 0)
279 return -EINVAL;
280
281 sock_addr->sin_addr = inaddr;
282 sock_addr->sin_family = AF_INET;
283 srep->dest_addr_len = sizeof(*sock_addr);
284
285 talloc_free(srep->dest_addr_str);
286 srep->dest_addr_str = talloc_strdup(srep, addr);
287
288 return update_srep_config(srep);
289}
290
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200291/*! Set the remote (UDP) port of a given stats_reporter
292 * \param[in] srep stats_reporter whose remote address is to be set
293 * \param[in] port UDP port of remote statsd to which we report
294 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100295int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200296{
297 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
298
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100299 if (!srep->have_net_config)
300 return -ENOTSUP;
301
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200302 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200303 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200304
305 return update_srep_config(srep);
306}
307
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200308/*! Set the local (IP) address of a given stats_reporter.
309 * \param[in] srep stats_reporter whose remote address is to be set
310 * \param[in] addr String representation of local IP address
311 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100312int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200313{
314 int rc;
315 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
316 struct in_addr inaddr;
317
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100318 if (!srep->have_net_config)
319 return -ENOTSUP;
320
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200321 if (addr) {
322 rc = inet_pton(AF_INET, addr, &inaddr);
323 if (rc <= 0)
324 return -EINVAL;
325 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100326 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200327 }
328
329 sock_addr->sin_addr = inaddr;
330 sock_addr->sin_family = AF_INET;
331 srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0;
332
333 talloc_free(srep->bind_addr_str);
334 srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL;
335
336 return update_srep_config(srep);
337}
338
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200339/*! Set the maximum transmission unit of a given stats_reporter.
340 * \param[in] srep stats_reporter whose remote address is to be set
341 * \param[in] mtu Maximum Transmission Unit of \a srep
342 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100343int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100344{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100345 if (!srep->have_net_config)
346 return -ENOTSUP;
347
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100348 if (mtu < 0)
349 return -EINVAL;
350
351 srep->mtu = mtu;
352
353 return update_srep_config(srep);
354}
Harald Welte67bdd802017-01-15 17:56:11 +0100355#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100356
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100357int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
358 enum osmo_stats_class class_id)
359{
360 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
361 return -EINVAL;
362
363 srep->max_class = class_id;
364
365 return 0;
366}
367
Alexander Chemerisf203ed32020-05-08 19:09:22 +0300368/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200369 * \param[in] interval Reporting interval in seconds
370 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100371int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200372{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200373 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100374 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200375
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100376 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100377 if (is_initialised)
378 start_timer();
379
380 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200381}
382
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300383/*! Set the regular flush period for a given stats_reporter
384 *
385 * Send all stats even if they have not changed (i.e. force the flush)
386 * every N-th reporting interval. Set to 0 to disable regular flush,
387 * set to 1 to flush every time, set to 2 to flush every 2nd time, etc.
388 * \param[in] srep stats_reporter to set flush period for
389 * \param[in] period Reporting interval in seconds
390 * \returns 0 on success; negative on error */
391int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period)
392{
393 srep->flush_period = period;
394 srep->flush_period_counter = 0;
395 /* force the flush now if it's not disabled by period=0 */
396 if (period > 0)
397 srep->force_single_flush = 1;
398
399 return 0;
400}
401
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200402/*! Set the name prefix of a given stats_reporter.
403 * \param[in] srep stats_reporter whose name prefix is to be set
404 * \param[in] prefix NAme perfix to pre-pend for any reported value
405 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100406int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200407{
408 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100409 srep->name_prefix = prefix && strlen(prefix) > 0 ?
410 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200411
412 return update_srep_config(srep);
413}
414
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200415
416/*! Enable the given stats_reporter.
417 * \param[in] srep stats_reporter who is to be enabled
418 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100419int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200420{
421 srep->enabled = 1;
422
423 return update_srep_config(srep);
424}
425
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200426/*! Disable the given stats_reporter.
427 * \param[in] srep stats_reporter who is to be disabled
428 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100429int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200430{
431 srep->enabled = 0;
432
433 return update_srep_config(srep);
434}
435
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100436/*** i/o helper functions ***/
437
Harald Welte67bdd802017-01-15 17:56:11 +0100438#ifdef HAVE_SYS_SOCKET_H
439
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200440/*! Open the UDP socket for given stats_reporter.
441 * \param[in] srep stats_reporter whose UDP socket is to be opened
442 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100443int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep)
444{
445 int sock;
446 int rc;
447 int buffer_size = STATS_DEFAULT_BUFLEN;
448
449 if (srep->fd != -1 && srep->close)
450 srep->close(srep);
451
452 sock = socket(AF_INET, SOCK_DGRAM, 0);
453 if (sock == -1)
454 return -errno;
455
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400456#if defined(__APPLE__) && !defined(MSG_NOSIGNAL)
457 {
458 static int val = 1;
459
460 rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val));
461 goto failed;
462 }
463#endif
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100464 if (srep->bind_addr_len > 0) {
465 rc = bind(sock, &srep->bind_addr, srep->bind_addr_len);
466 if (rc == -1)
467 goto failed;
468 }
469
470 srep->fd = sock;
471
472 if (srep->mtu > 0) {
473 buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */;
474 srep->agg_enabled = 1;
475 }
476
477 srep->buffer = msgb_alloc(buffer_size, "stats buffer");
478
479 return 0;
480
481failed:
482 rc = -errno;
483 close(sock);
484
485 return rc;
486}
487
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200488/*! Closee the UDP socket for given stats_reporter.
489 * \param[in] srep stats_reporter whose UDP socket is to be closed
490 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100491int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep)
492{
493 int rc;
494 if (srep->fd == -1)
495 return -EBADF;
496
497 osmo_stats_reporter_send_buffer(srep);
498
499 rc = close(srep->fd);
500 srep->fd = -1;
501 msgb_free(srep->buffer);
502 srep->buffer = NULL;
503 return rc == -1 ? -errno : 0;
504}
505
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200506/*! Send given date to given stats_reporter.
507 * \param[in] srep stats_reporter whose UDP socket is to be opened
508 * \param[in] data string data to be sent
509 * \param[in] data_len Length of \a data in bytes
510 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100511int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200512 int data_len)
513{
514 int rc;
515
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400516 rc = sendto(srep->fd, data, data_len,
517#ifdef MSG_NOSIGNAL
518 MSG_NOSIGNAL |
519#endif
520 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200521 &srep->dest_addr, srep->dest_addr_len);
522
523 if (rc == -1)
524 rc = -errno;
525
526 return rc;
527}
528
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200529/*! Send current accumulated buffer to given stats_reporter.
530 * \param[in] srep stats_reporter whose UDP socket is to be opened
531 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100532int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100533{
534 int rc;
535
536 if (!srep->buffer || msgb_length(srep->buffer) == 0)
537 return 0;
538
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100539 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100540 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
541
542 msgb_trim(srep->buffer, 0);
543
544 return rc;
545}
Harald Welte67bdd802017-01-15 17:56:11 +0100546#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100547
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100548/*** log reporter ***/
549
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200550/*! Create a stats_reporter that logs via libosmocore logging.
551 * A stats_reporter created via this function will simply print the statistics
552 * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO
553 * priority. The configuration of the libosmocore log targets define where this
554 * information will end up (ignored, text file, stderr, syslog, ...).
555 * \param[in] name Name of the to-be-created stats_reporter
556 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100557struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100558{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100559 struct osmo_stats_reporter *srep;
560 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100561
562 srep->have_net_config = 0;
563
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100564 srep->send_counter = osmo_stats_reporter_log_send_counter;
565 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100566
567 return srep;
568}
569
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100570static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100571 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100572 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100573 const char *unit)
574{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100575 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100576 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100577 type, srep->name_prefix ? srep->name_prefix : "",
578 name1 ? name1 : "", index1,
579 name2, value, unit ? unit : "");
580
581 return 0;
582}
583
584
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100585static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100586 const struct rate_ctr_group *ctrg,
587 const struct rate_ctr_desc *desc,
588 int64_t value, int64_t delta)
589{
590 if (ctrg)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100591 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100592 ctrg->desc->group_name_prefix,
593 ctrg->idx,
594 desc->name, value, NULL);
595 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100596 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100597 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100598 desc->name, value, NULL);
599}
600
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100601static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
602 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100603 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100604{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100605 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100606 statg->desc->group_name_prefix, statg->idx,
607 desc->name, value, desc->unit);
608}
609
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100610/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200611
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100612static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
613 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200614{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100615 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
616 class_id = index != 0 ?
617 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200618
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100619 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200620}
621
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200622/*** generic rate counter support ***/
623
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100624static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200625 const struct rate_ctr_group *ctrg,
626 const struct rate_ctr_desc *desc,
627 int64_t value, int64_t delta)
628{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100629 if (!srep->send_counter)
630 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200631
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100632 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200633}
634
635static int rate_ctr_handler(
636 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
637 const struct rate_ctr_desc *desc, void *sctx_)
638{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100639 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200640 int64_t delta = rate_ctr_difference(ctr);
641
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100642 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200643 if (!srep->running)
644 continue;
645
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100646 if (delta == 0 && !srep->force_single_flush)
647 continue;
648
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100649 if (!osmo_stats_reporter_check_config(srep,
650 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100651 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100652
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100653 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200654 ctr->current, delta);
655
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100656 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200657 }
658
659 return 0;
660}
661
662static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_)
663{
664 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_);
665
666 return 0;
667}
668
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100669/*** stat item support ***/
670
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100671static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep,
672 const struct osmo_stat_item_group *statg,
673 const struct osmo_stat_item_desc *desc,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100674 int32_t value)
675{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100676 if (!srep->send_item)
677 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100678
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100679 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100680}
681
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100682static int osmo_stat_item_handler(
683 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100684{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100685 struct osmo_stats_reporter *srep;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100686 int32_t idx = current_stat_item_index;
687 int32_t value;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100688 int have_value;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100689
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100690 have_value = osmo_stat_item_get_next(item, &idx, &value) > 0;
691 if (!have_value)
692 /* Send the last value in case a flush is requested */
693 value = osmo_stat_item_get_last(item);
694
695 do {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100696 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100697 if (!srep->running)
698 continue;
699
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100700 if (!have_value && !srep->force_single_flush)
701 continue;
702
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100703 if (!osmo_stats_reporter_check_config(srep,
704 statg->idx, statg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100705 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100706
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100707 osmo_stats_reporter_send_item(srep, statg,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100708 item->desc, value);
709 }
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100710
711 if (!have_value)
712 break;
713
714 have_value = osmo_stat_item_get_next(item, &idx, &value) > 0;
715 } while (have_value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100716
717 return 0;
718}
719
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100720static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100721{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100722 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100723
724 return 0;
725}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200726
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100727/*** osmo counter support ***/
728
729static int handle_counter(struct osmo_counter *counter, void *sctx_)
730{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100731 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100732 struct rate_ctr_desc desc = {0};
733 /* Fake a rate counter description */
734 desc.name = counter->name;
735 desc.description = counter->description;
736
737 int delta = osmo_counter_difference(counter);
738
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100739 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100740 if (!srep->running)
741 continue;
742
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100743 if (delta == 0 && !srep->force_single_flush)
744 continue;
745
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100746 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100747 counter->value, delta);
748
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100749 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100750 }
751
752 return 0;
753}
754
755
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200756/*** main reporting function ***/
757
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100758static void flush_all_reporters()
759{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100760 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100761
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100762 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100763 if (!srep->running)
764 continue;
765
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100766 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300767
768 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100769 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300770 /* and schedule a new flush if it's time for it */
771 if (srep->flush_period > 0) {
772 srep->flush_period_counter++;
773 if (srep->flush_period_counter >= srep->flush_period) {
774 srep->force_single_flush = 1;
775 srep->flush_period_counter = 0;
776 }
777 }
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100778 }
779}
780
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100781int osmo_stats_report()
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200782{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100783 /* per group actions */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100784 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200785 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100786 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200787
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100788 /* global actions */
789 osmo_stat_item_discard_all(&current_stat_item_index);
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100790 flush_all_reporters();
791
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200792 return 0;
793}
Harald Welte67bdd802017-01-15 17:56:11 +0100794
795#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200796
797/*! @} */