Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file stats.c */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 2 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | * (C) 2015 by sysmocom - s.f.m.c. GmbH |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 4 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 9 | * 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 25 | /*! \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 Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 65 | #include "config.h" |
| 66 | #if !defined(EMBEDDED) |
| 67 | |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 68 | #include <osmocom/core/byteswap.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 69 | #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 Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 76 | #include <sys/types.h> |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 77 | #include <inttypes.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 78 | |
| 79 | #ifdef HAVE_SYS_SOCKET_H |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 80 | #include <sys/socket.h> |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 81 | #include <netinet/in.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 82 | #include <arpa/inet.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 83 | #endif |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 84 | |
| 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 Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 89 | #include <osmocom/core/select.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 90 | #include <osmocom/core/counter.h> |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 91 | #include <osmocom/core/msgb.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 92 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 93 | #define STATS_DEFAULT_INTERVAL 5 /* secs */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 94 | #define STATS_DEFAULT_BUFLEN 256 |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 95 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 96 | static LLIST_HEAD(osmo_stats_reporter_list); |
| 97 | static void *osmo_stats_ctx = NULL; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 98 | static int is_initialised = 0; |
| 99 | static int32_t current_stat_item_index = 0; |
| 100 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 101 | static struct osmo_stats_config s_stats_config = { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 102 | .interval = STATS_DEFAULT_INTERVAL, |
| 103 | }; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 104 | struct osmo_stats_config *osmo_stats_config = &s_stats_config; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 105 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 106 | static struct osmo_fd osmo_stats_timer = { .fd = -1 }; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 107 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 108 | static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 109 | const struct rate_ctr_group *ctrg, |
| 110 | const struct rate_ctr_desc *desc, |
| 111 | int64_t value, int64_t delta); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 112 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 113 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 114 | const struct osmo_stat_item_desc *desc, int64_t value); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 115 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 116 | static int update_srep_config(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 117 | { |
| 118 | int rc = 0; |
| 119 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 120 | if (srep->running) { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 121 | if (srep->close) |
| 122 | rc = srep->close(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 123 | srep->running = 0; |
| 124 | } |
| 125 | |
| 126 | if (!srep->enabled) |
| 127 | return rc; |
| 128 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 129 | if (srep->open) |
| 130 | rc = srep->open(srep); |
| 131 | else |
| 132 | rc = 0; |
| 133 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 134 | if (rc < 0) |
| 135 | srep->enabled = 0; |
| 136 | else |
| 137 | srep->running = 1; |
| 138 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 139 | srep->force_single_flush = 1; |
| 140 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 141 | return rc; |
| 142 | } |
| 143 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 144 | static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 145 | { |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 146 | 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 Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 162 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 163 | if (!llist_empty(&osmo_stats_reporter_list)) |
| 164 | osmo_stats_report(); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 165 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 166 | return 0; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static int start_timer() |
| 170 | { |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 171 | int rc; |
| 172 | int interval = osmo_stats_config->interval; |
| 173 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 174 | if (!is_initialised) |
| 175 | return -ESRCH; |
| 176 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 177 | 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 Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 184 | |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 185 | 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 Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 203 | struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 204 | const char *name) |
| 205 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 206 | struct osmo_stats_reporter *srep; |
| 207 | srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 208 | OSMO_ASSERT(srep); |
| 209 | srep->type = type; |
| 210 | if (name) |
| 211 | srep->name = talloc_strdup(srep, name); |
| 212 | srep->fd = -1; |
| 213 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 214 | llist_add(&srep->list, &osmo_stats_reporter_list); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 215 | |
| 216 | return srep; |
| 217 | } |
| 218 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 219 | /*! Destroy a given stats_reporter. Takes care of first disabling it. |
| 220 | * \param[in] srep stats_reporter that shall be disabled + destroyed */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 221 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 222 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 223 | osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 224 | llist_del(&srep->list); |
| 225 | talloc_free(srep); |
| 226 | } |
| 227 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 228 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 230 | void osmo_stats_init(void *ctx) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 231 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 232 | osmo_stats_ctx = ctx; |
| 233 | osmo_stat_item_discard_all(¤t_stat_item_index); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 234 | |
| 235 | is_initialised = 1; |
| 236 | start_timer(); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 239 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 243 | struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 244 | const char *name) |
| 245 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 246 | struct osmo_stats_reporter *srep; |
| 247 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 248 | 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 Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 260 | #ifdef HAVE_SYS_SOCKET_H |
| 261 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 262 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 266 | int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 267 | { |
| 268 | int rc; |
| 269 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 270 | struct in_addr inaddr; |
| 271 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 272 | if (!srep->have_net_config) |
| 273 | return -ENOTSUP; |
| 274 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 275 | 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 291 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 295 | int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 296 | { |
| 297 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 298 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 299 | if (!srep->have_net_config) |
| 300 | return -ENOTSUP; |
| 301 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 302 | srep->dest_port = port; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 303 | sock_addr->sin_port = osmo_htons(port); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 304 | |
| 305 | return update_srep_config(srep); |
| 306 | } |
| 307 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 308 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 312 | int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 313 | { |
| 314 | int rc; |
| 315 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr; |
| 316 | struct in_addr inaddr; |
| 317 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 318 | if (!srep->have_net_config) |
| 319 | return -ENOTSUP; |
| 320 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 321 | if (addr) { |
| 322 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 323 | if (rc <= 0) |
| 324 | return -EINVAL; |
| 325 | } else { |
Holger Hans Peter Freyther | 7921975 | 2015-11-02 15:50:32 +0100 | [diff] [blame] | 326 | inaddr.s_addr = INADDR_ANY; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 327 | } |
| 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 339 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 343 | int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 344 | { |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 345 | if (!srep->have_net_config) |
| 346 | return -ENOTSUP; |
| 347 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 348 | if (mtu < 0) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | srep->mtu = mtu; |
| 352 | |
| 353 | return update_srep_config(srep); |
| 354 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 355 | #endif /* HAVE_SYS_SOCKETS_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 356 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 357 | int 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 Chemeris | f203ed3 | 2020-05-08 19:09:22 +0300 | [diff] [blame] | 368 | /*! Set the reporting interval (common for all reporters) |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 369 | * \param[in] interval Reporting interval in seconds |
| 370 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 371 | int osmo_stats_set_interval(int interval) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 372 | { |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 373 | if (interval < 0) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 374 | return -EINVAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 375 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 376 | osmo_stats_config->interval = interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 377 | if (is_initialised) |
| 378 | start_timer(); |
| 379 | |
| 380 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 383 | /*! 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 */ |
| 391 | int 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 402 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 406 | int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 407 | { |
| 408 | talloc_free(srep->name_prefix); |
Jacob Erlbeck | 916423e | 2015-11-09 10:52:19 +0100 | [diff] [blame] | 409 | srep->name_prefix = prefix && strlen(prefix) > 0 ? |
| 410 | talloc_strdup(srep, prefix) : NULL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 411 | |
| 412 | return update_srep_config(srep); |
| 413 | } |
| 414 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 415 | |
| 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 419 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 420 | { |
| 421 | srep->enabled = 1; |
| 422 | |
| 423 | return update_srep_config(srep); |
| 424 | } |
| 425 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 426 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 429 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 430 | { |
| 431 | srep->enabled = 0; |
| 432 | |
| 433 | return update_srep_config(srep); |
| 434 | } |
| 435 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 436 | /*** i/o helper functions ***/ |
| 437 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 438 | #ifdef HAVE_SYS_SOCKET_H |
| 439 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 440 | /*! 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 Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 443 | int 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-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 456 | #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 Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 464 | 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 | |
| 481 | failed: |
| 482 | rc = -errno; |
| 483 | close(sock); |
| 484 | |
| 485 | return rc; |
| 486 | } |
| 487 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 488 | /*! 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 Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 491 | int 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 506 | /*! 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 Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 511 | int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 512 | int data_len) |
| 513 | { |
| 514 | int rc; |
| 515 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 516 | rc = sendto(srep->fd, data, data_len, |
| 517 | #ifdef MSG_NOSIGNAL |
| 518 | MSG_NOSIGNAL | |
| 519 | #endif |
| 520 | MSG_DONTWAIT, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 521 | &srep->dest_addr, srep->dest_addr_len); |
| 522 | |
| 523 | if (rc == -1) |
| 524 | rc = -errno; |
| 525 | |
| 526 | return rc; |
| 527 | } |
| 528 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 529 | /*! 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 Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 532 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 533 | { |
| 534 | int rc; |
| 535 | |
| 536 | if (!srep->buffer || msgb_length(srep->buffer) == 0) |
| 537 | return 0; |
| 538 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 539 | rc = osmo_stats_reporter_send(srep, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 540 | (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer)); |
| 541 | |
| 542 | msgb_trim(srep->buffer, 0); |
| 543 | |
| 544 | return rc; |
| 545 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 546 | #endif /* HAVE_SYS_SOCKET_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 547 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 548 | /*** log reporter ***/ |
| 549 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 550 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 557 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 558 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 559 | struct osmo_stats_reporter *srep; |
| 560 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 561 | |
| 562 | srep->have_net_config = 0; |
| 563 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 564 | srep->send_counter = osmo_stats_reporter_log_send_counter; |
| 565 | srep->send_item = osmo_stats_reporter_log_send_item; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 566 | |
| 567 | return srep; |
| 568 | } |
| 569 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 570 | static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 571 | const char *type, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 572 | const char *name1, unsigned int index1, const char *name2, int value, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 573 | const char *unit) |
| 574 | { |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 575 | LOGP(DLSTATS, LOGL_INFO, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 576 | "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 577 | type, srep->name_prefix ? srep->name_prefix : "", |
| 578 | name1 ? name1 : "", index1, |
| 579 | name2, value, unit ? unit : ""); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 585 | static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 586 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 591 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 592 | ctrg->desc->group_name_prefix, |
| 593 | ctrg->idx, |
| 594 | desc->name, value, NULL); |
| 595 | else |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 596 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 597 | NULL, 0, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 598 | desc->name, value, NULL); |
| 599 | } |
| 600 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 601 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 602 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 603 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 604 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 605 | return osmo_stats_reporter_log_send(srep, "i", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 606 | statg->desc->group_name_prefix, statg->idx, |
| 607 | desc->name, value, desc->unit); |
| 608 | } |
| 609 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 610 | /*** helper for reporting ***/ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 611 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 612 | static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep, |
| 613 | unsigned int index, int class_id) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 614 | { |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 615 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 616 | class_id = index != 0 ? |
| 617 | OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 618 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 619 | return class_id <= srep->max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 622 | /*** generic rate counter support ***/ |
| 623 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 624 | static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 625 | const struct rate_ctr_group *ctrg, |
| 626 | const struct rate_ctr_desc *desc, |
| 627 | int64_t value, int64_t delta) |
| 628 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 629 | if (!srep->send_counter) |
| 630 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 631 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 632 | return srep->send_counter(srep, ctrg, desc, value, delta); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 639 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 640 | int64_t delta = rate_ctr_difference(ctr); |
| 641 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 642 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 643 | if (!srep->running) |
| 644 | continue; |
| 645 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 646 | if (delta == 0 && !srep->force_single_flush) |
| 647 | continue; |
| 648 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 649 | if (!osmo_stats_reporter_check_config(srep, |
| 650 | ctrg->idx, ctrg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 651 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 652 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 653 | osmo_stats_reporter_send_counter(srep, ctrg, desc, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 654 | ctr->current, delta); |
| 655 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 656 | /* TODO: handle result (log?, inc counter(!)?) or remove it */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static 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 Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 669 | /*** stat item support ***/ |
| 670 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 671 | static 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 Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 674 | int32_t value) |
| 675 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 676 | if (!srep->send_item) |
| 677 | return 0; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 678 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 679 | return srep->send_item(srep, statg, desc, value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 682 | static int osmo_stat_item_handler( |
| 683 | struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_) |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 684 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 685 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 686 | int32_t idx = current_stat_item_index; |
| 687 | int32_t value; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 688 | int have_value; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 689 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 690 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 696 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 697 | if (!srep->running) |
| 698 | continue; |
| 699 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 700 | if (!have_value && !srep->force_single_flush) |
| 701 | continue; |
| 702 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 703 | if (!osmo_stats_reporter_check_config(srep, |
| 704 | statg->idx, statg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 705 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 706 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 707 | osmo_stats_reporter_send_item(srep, statg, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 708 | item->desc, value); |
| 709 | } |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 710 | |
| 711 | if (!have_value) |
| 712 | break; |
| 713 | |
| 714 | have_value = osmo_stat_item_get_next(item, &idx, &value) > 0; |
| 715 | } while (have_value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 720 | static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_) |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 721 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 722 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 723 | |
| 724 | return 0; |
| 725 | } |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 726 | |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 727 | /*** osmo counter support ***/ |
| 728 | |
| 729 | static int handle_counter(struct osmo_counter *counter, void *sctx_) |
| 730 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 731 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 732 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 739 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 740 | if (!srep->running) |
| 741 | continue; |
| 742 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 743 | if (delta == 0 && !srep->force_single_flush) |
| 744 | continue; |
| 745 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 746 | osmo_stats_reporter_send_counter(srep, NULL, &desc, |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 747 | counter->value, delta); |
| 748 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 749 | /* TODO: handle result (log?, inc counter(!)?) */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 756 | /*** main reporting function ***/ |
| 757 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 758 | static void flush_all_reporters() |
| 759 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 760 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 761 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 762 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 763 | if (!srep->running) |
| 764 | continue; |
| 765 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 766 | osmo_stats_reporter_send_buffer(srep); |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 767 | |
| 768 | /* reset force_single_flush first */ |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 769 | srep->force_single_flush = 0; |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 770 | /* 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 Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 781 | int osmo_stats_report() |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 782 | { |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 783 | /* per group actions */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 784 | osmo_counters_for_each(handle_counter, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 785 | rate_ctr_for_each_group(rate_ctr_group_handler, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 786 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 787 | |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 788 | /* global actions */ |
| 789 | osmo_stat_item_discard_all(¤t_stat_item_index); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 790 | flush_all_reporters(); |
| 791 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 792 | return 0; |
| 793 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 794 | |
| 795 | #endif /* !EMBEDDED */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 796 | |
| 797 | /*! @} */ |