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); |
| 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 Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 194 | 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] | 195 | const char *name) |
| 196 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 197 | struct osmo_stats_reporter *srep; |
| 198 | srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 199 | OSMO_ASSERT(srep); |
| 200 | srep->type = type; |
| 201 | if (name) |
| 202 | srep->name = talloc_strdup(srep, name); |
| 203 | srep->fd = -1; |
| 204 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 205 | llist_add(&srep->list, &osmo_stats_reporter_list); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 206 | |
| 207 | return srep; |
| 208 | } |
| 209 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 210 | /*! Destroy a given stats_reporter. Takes care of first disabling it. |
| 211 | * \param[in] srep stats_reporter that shall be disabled + destroyed */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 212 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 213 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 214 | osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 215 | llist_del(&srep->list); |
| 216 | talloc_free(srep); |
| 217 | } |
| 218 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 219 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 221 | void osmo_stats_init(void *ctx) |
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_ctx = ctx; |
| 224 | osmo_stat_item_discard_all(¤t_stat_item_index); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 225 | |
| 226 | is_initialised = 1; |
| 227 | start_timer(); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 230 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 234 | 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] | 235 | const char *name) |
| 236 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 237 | struct osmo_stats_reporter *srep; |
| 238 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 239 | 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 Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 251 | #ifdef HAVE_SYS_SOCKET_H |
| 252 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 253 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 257 | 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] | 258 | { |
| 259 | int rc; |
| 260 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 261 | struct in_addr inaddr; |
| 262 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 263 | if (!srep->have_net_config) |
| 264 | return -ENOTSUP; |
| 265 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 266 | 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 282 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 286 | 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] | 287 | { |
| 288 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 289 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 290 | if (!srep->have_net_config) |
| 291 | return -ENOTSUP; |
| 292 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 293 | srep->dest_port = port; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 294 | sock_addr->sin_port = osmo_htons(port); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 295 | |
| 296 | return update_srep_config(srep); |
| 297 | } |
| 298 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 299 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 303 | 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] | 304 | { |
| 305 | int rc; |
| 306 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr; |
| 307 | struct in_addr inaddr; |
| 308 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 309 | if (!srep->have_net_config) |
| 310 | return -ENOTSUP; |
| 311 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 312 | if (addr) { |
| 313 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 314 | if (rc <= 0) |
| 315 | return -EINVAL; |
| 316 | } else { |
Holger Hans Peter Freyther | 7921975 | 2015-11-02 15:50:32 +0100 | [diff] [blame] | 317 | inaddr.s_addr = INADDR_ANY; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 318 | } |
| 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 330 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 334 | 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] | 335 | { |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 336 | if (!srep->have_net_config) |
| 337 | return -ENOTSUP; |
| 338 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 339 | if (mtu < 0) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | srep->mtu = mtu; |
| 343 | |
| 344 | return update_srep_config(srep); |
| 345 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 346 | #endif /* HAVE_SYS_SOCKETS_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 347 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 348 | int 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 Chemeris | f203ed3 | 2020-05-08 19:09:22 +0300 | [diff] [blame] | 359 | /*! Set the reporting interval (common for all reporters) |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 360 | * \param[in] interval Reporting interval in seconds |
| 361 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 362 | int osmo_stats_set_interval(int interval) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 363 | { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 364 | if (interval <= 0) |
| 365 | return -EINVAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 366 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 367 | osmo_stats_config->interval = interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 368 | if (is_initialised) |
| 369 | start_timer(); |
| 370 | |
| 371 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 374 | /*! 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 */ |
| 382 | int 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 393 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 397 | 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] | 398 | { |
| 399 | talloc_free(srep->name_prefix); |
Jacob Erlbeck | 916423e | 2015-11-09 10:52:19 +0100 | [diff] [blame] | 400 | srep->name_prefix = prefix && strlen(prefix) > 0 ? |
| 401 | talloc_strdup(srep, prefix) : NULL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 402 | |
| 403 | return update_srep_config(srep); |
| 404 | } |
| 405 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 406 | |
| 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 410 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 411 | { |
| 412 | srep->enabled = 1; |
| 413 | |
| 414 | return update_srep_config(srep); |
| 415 | } |
| 416 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 417 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 420 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 421 | { |
| 422 | srep->enabled = 0; |
| 423 | |
| 424 | return update_srep_config(srep); |
| 425 | } |
| 426 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 427 | /*** i/o helper functions ***/ |
| 428 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 429 | #ifdef HAVE_SYS_SOCKET_H |
| 430 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 431 | /*! 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 Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 434 | int 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-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 447 | #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 Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 455 | 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 | |
| 472 | failed: |
| 473 | rc = -errno; |
| 474 | close(sock); |
| 475 | |
| 476 | return rc; |
| 477 | } |
| 478 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 479 | /*! 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 Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 482 | int 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 Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 497 | /*! 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 Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 502 | 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] | 503 | int data_len) |
| 504 | { |
| 505 | int rc; |
| 506 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 507 | rc = sendto(srep->fd, data, data_len, |
| 508 | #ifdef MSG_NOSIGNAL |
| 509 | MSG_NOSIGNAL | |
| 510 | #endif |
| 511 | MSG_DONTWAIT, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 512 | &srep->dest_addr, srep->dest_addr_len); |
| 513 | |
| 514 | if (rc == -1) |
| 515 | rc = -errno; |
| 516 | |
| 517 | return rc; |
| 518 | } |
| 519 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 520 | /*! 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 Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 523 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 524 | { |
| 525 | int rc; |
| 526 | |
| 527 | if (!srep->buffer || msgb_length(srep->buffer) == 0) |
| 528 | return 0; |
| 529 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 530 | rc = osmo_stats_reporter_send(srep, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 531 | (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer)); |
| 532 | |
| 533 | msgb_trim(srep->buffer, 0); |
| 534 | |
| 535 | return rc; |
| 536 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 537 | #endif /* HAVE_SYS_SOCKET_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 538 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 539 | /*** log reporter ***/ |
| 540 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 541 | /*! 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 548 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 549 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 550 | struct osmo_stats_reporter *srep; |
| 551 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 552 | |
| 553 | srep->have_net_config = 0; |
| 554 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 555 | srep->send_counter = osmo_stats_reporter_log_send_counter; |
| 556 | srep->send_item = osmo_stats_reporter_log_send_item; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 557 | |
| 558 | return srep; |
| 559 | } |
| 560 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 561 | static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 562 | const char *type, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 563 | const char *name1, unsigned int index1, const char *name2, int value, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 564 | const char *unit) |
| 565 | { |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 566 | LOGP(DLSTATS, LOGL_INFO, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 567 | "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] | 568 | type, srep->name_prefix ? srep->name_prefix : "", |
| 569 | name1 ? name1 : "", index1, |
| 570 | name2, value, unit ? unit : ""); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 576 | 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] | 577 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 582 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 583 | ctrg->desc->group_name_prefix, |
| 584 | ctrg->idx, |
| 585 | desc->name, value, NULL); |
| 586 | else |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 587 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 588 | NULL, 0, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 589 | desc->name, value, NULL); |
| 590 | } |
| 591 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 592 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 593 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 594 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 595 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 596 | return osmo_stats_reporter_log_send(srep, "i", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 597 | statg->desc->group_name_prefix, statg->idx, |
| 598 | desc->name, value, desc->unit); |
| 599 | } |
| 600 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 601 | /*** helper for reporting ***/ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 602 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 603 | static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep, |
| 604 | unsigned int index, int class_id) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 605 | { |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 606 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 607 | class_id = index != 0 ? |
| 608 | OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 609 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 610 | return class_id <= srep->max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 613 | /*** generic rate counter support ***/ |
| 614 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 615 | static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 616 | const struct rate_ctr_group *ctrg, |
| 617 | const struct rate_ctr_desc *desc, |
| 618 | int64_t value, int64_t delta) |
| 619 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 620 | if (!srep->send_counter) |
| 621 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 622 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 623 | return srep->send_counter(srep, ctrg, desc, value, delta); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 630 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 631 | int64_t delta = rate_ctr_difference(ctr); |
| 632 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 633 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 634 | if (!srep->running) |
| 635 | continue; |
| 636 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 637 | if (delta == 0 && !srep->force_single_flush) |
| 638 | continue; |
| 639 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 640 | if (!osmo_stats_reporter_check_config(srep, |
| 641 | ctrg->idx, ctrg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 642 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 643 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 644 | osmo_stats_reporter_send_counter(srep, ctrg, desc, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 645 | ctr->current, delta); |
| 646 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 647 | /* TODO: handle result (log?, inc counter(!)?) or remove it */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static 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 Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 660 | /*** stat item support ***/ |
| 661 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 662 | static 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 Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 665 | int32_t value) |
| 666 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 667 | if (!srep->send_item) |
| 668 | return 0; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 669 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 670 | return srep->send_item(srep, statg, desc, value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 673 | static int osmo_stat_item_handler( |
| 674 | 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] | 675 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 676 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 677 | int32_t idx = current_stat_item_index; |
| 678 | int32_t value; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 679 | int have_value; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 680 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 681 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 687 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 688 | if (!srep->running) |
| 689 | continue; |
| 690 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 691 | if (!have_value && !srep->force_single_flush) |
| 692 | continue; |
| 693 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 694 | if (!osmo_stats_reporter_check_config(srep, |
| 695 | statg->idx, statg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 696 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 697 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 698 | osmo_stats_reporter_send_item(srep, statg, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 699 | item->desc, value); |
| 700 | } |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 701 | |
| 702 | if (!have_value) |
| 703 | break; |
| 704 | |
| 705 | have_value = osmo_stat_item_get_next(item, &idx, &value) > 0; |
| 706 | } while (have_value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 711 | 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] | 712 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 713 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | } |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 717 | |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 718 | /*** osmo counter support ***/ |
| 719 | |
| 720 | static int handle_counter(struct osmo_counter *counter, void *sctx_) |
| 721 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 722 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 723 | 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 Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 730 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 731 | if (!srep->running) |
| 732 | continue; |
| 733 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 734 | if (delta == 0 && !srep->force_single_flush) |
| 735 | continue; |
| 736 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 737 | osmo_stats_reporter_send_counter(srep, NULL, &desc, |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 738 | counter->value, delta); |
| 739 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 740 | /* TODO: handle result (log?, inc counter(!)?) */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 747 | /*** main reporting function ***/ |
| 748 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 749 | static void flush_all_reporters() |
| 750 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 751 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 752 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 753 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 754 | if (!srep->running) |
| 755 | continue; |
| 756 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 757 | osmo_stats_reporter_send_buffer(srep); |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 758 | |
| 759 | /* reset force_single_flush first */ |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 760 | srep->force_single_flush = 0; |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 761 | /* 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 Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 772 | int osmo_stats_report() |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 773 | { |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 774 | /* per group actions */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 775 | osmo_counters_for_each(handle_counter, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 776 | rate_ctr_for_each_group(rate_ctr_group_handler, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 777 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 778 | |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 779 | /* global actions */ |
| 780 | osmo_stat_item_discard_all(¤t_stat_item_index); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 781 | flush_all_reporters(); |
| 782 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 783 | return 0; |
| 784 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 785 | |
| 786 | #endif /* !EMBEDDED */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 787 | |
| 788 | /*! @} */ |