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 | /* |
| 3 | * (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 4 | * |
| 5 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 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> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 77 | |
| 78 | #ifdef HAVE_SYS_SOCKET_H |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 79 | #include <sys/socket.h> |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 80 | #include <netinet/in.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 81 | #include <arpa/inet.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 82 | #endif |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 83 | |
| 84 | #include <osmocom/core/utils.h> |
| 85 | #include <osmocom/core/logging.h> |
| 86 | #include <osmocom/core/rate_ctr.h> |
| 87 | #include <osmocom/core/stat_item.h> |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 88 | #include <osmocom/core/timer.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 89 | #include <osmocom/core/counter.h> |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 90 | #include <osmocom/core/msgb.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 91 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 92 | #define STATS_DEFAULT_INTERVAL 5 /* secs */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 93 | #define STATS_DEFAULT_BUFLEN 256 |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 94 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 95 | static LLIST_HEAD(osmo_stats_reporter_list); |
| 96 | static void *osmo_stats_ctx = NULL; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 97 | static int is_initialised = 0; |
| 98 | static int32_t current_stat_item_index = 0; |
| 99 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 100 | static struct osmo_stats_config s_stats_config = { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 101 | .interval = STATS_DEFAULT_INTERVAL, |
| 102 | }; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 103 | struct osmo_stats_config *osmo_stats_config = &s_stats_config; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 104 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 105 | static struct osmo_timer_list osmo_stats_timer; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 106 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | 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] | 108 | const struct rate_ctr_group *ctrg, |
| 109 | const struct rate_ctr_desc *desc, |
| 110 | int64_t value, int64_t delta); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 111 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 112 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 113 | const struct osmo_stat_item_desc *desc, int64_t value); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 114 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 115 | static int update_srep_config(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 116 | { |
| 117 | int rc = 0; |
| 118 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 119 | if (srep->running) { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 120 | if (srep->close) |
| 121 | rc = srep->close(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 122 | srep->running = 0; |
| 123 | } |
| 124 | |
| 125 | if (!srep->enabled) |
| 126 | return rc; |
| 127 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 128 | if (srep->open) |
| 129 | rc = srep->open(srep); |
| 130 | else |
| 131 | rc = 0; |
| 132 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 133 | if (rc < 0) |
| 134 | srep->enabled = 0; |
| 135 | else |
| 136 | srep->running = 1; |
| 137 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 138 | srep->force_single_flush = 1; |
| 139 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 140 | return rc; |
| 141 | } |
| 142 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 143 | static void osmo_stats_timer_cb(void *data) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 144 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 145 | int interval = osmo_stats_config->interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 146 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 147 | if (!llist_empty(&osmo_stats_reporter_list)) |
| 148 | osmo_stats_report(); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 149 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 150 | osmo_timer_schedule(&osmo_stats_timer, interval, 0); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static int start_timer() |
| 154 | { |
| 155 | if (!is_initialised) |
| 156 | return -ESRCH; |
| 157 | |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 158 | osmo_timer_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 159 | osmo_timer_schedule(&osmo_stats_timer, 0, 1); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 164 | 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] | 165 | const char *name) |
| 166 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 167 | struct osmo_stats_reporter *srep; |
| 168 | srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 169 | OSMO_ASSERT(srep); |
| 170 | srep->type = type; |
| 171 | if (name) |
| 172 | srep->name = talloc_strdup(srep, name); |
| 173 | srep->fd = -1; |
| 174 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 175 | llist_add(&srep->list, &osmo_stats_reporter_list); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 176 | |
| 177 | return srep; |
| 178 | } |
| 179 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 180 | /*! Destroy a given stats_reporter. Takes care of first disabling it. |
| 181 | * \param[in] srep stats_reporter that shall be disabled + destroyed */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 182 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 183 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 184 | osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 185 | llist_del(&srep->list); |
| 186 | talloc_free(srep); |
| 187 | } |
| 188 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 189 | /*! Initilize the stats reporting module; call this once in your program |
| 190 | * \param[in] ctx Talloc context from which stats related memory is allocated */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 191 | void osmo_stats_init(void *ctx) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 192 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 193 | osmo_stats_ctx = ctx; |
| 194 | osmo_stat_item_discard_all(¤t_stat_item_index); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 195 | |
| 196 | is_initialised = 1; |
| 197 | start_timer(); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 200 | /*! Find a stats_reporter of given \a type and \a name. |
| 201 | * \param[in] type Type of stats_reporter to find |
| 202 | * \param[in] name Name of stats_reporter to find |
| 203 | * \returns stats_reporter matching \a type and \a name; NULL otherwise */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 204 | 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] | 205 | const char *name) |
| 206 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 207 | struct osmo_stats_reporter *srep; |
| 208 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 209 | if (srep->type != type) |
| 210 | continue; |
| 211 | if (srep->name != name) { |
| 212 | if (name == NULL || srep->name == NULL || |
| 213 | strcmp(name, srep->name) != 0) |
| 214 | continue; |
| 215 | } |
| 216 | return srep; |
| 217 | } |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 221 | #ifdef HAVE_SYS_SOCKET_H |
| 222 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 223 | /*! Set the remote (IP) address of a given stats_reporter. |
| 224 | * \param[in] srep stats_reporter whose remote address is to be set |
| 225 | * \param[in] addr String representation of remote IPv4 address |
| 226 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 227 | 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] | 228 | { |
| 229 | int rc; |
| 230 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 231 | struct in_addr inaddr; |
| 232 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 233 | if (!srep->have_net_config) |
| 234 | return -ENOTSUP; |
| 235 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 236 | OSMO_ASSERT(addr != NULL); |
| 237 | |
| 238 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 239 | if (rc <= 0) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | sock_addr->sin_addr = inaddr; |
| 243 | sock_addr->sin_family = AF_INET; |
| 244 | srep->dest_addr_len = sizeof(*sock_addr); |
| 245 | |
| 246 | talloc_free(srep->dest_addr_str); |
| 247 | srep->dest_addr_str = talloc_strdup(srep, addr); |
| 248 | |
| 249 | return update_srep_config(srep); |
| 250 | } |
| 251 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 252 | /*! Set the remote (UDP) port of a given stats_reporter |
| 253 | * \param[in] srep stats_reporter whose remote address is to be set |
| 254 | * \param[in] port UDP port of remote statsd to which we report |
| 255 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 259 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 260 | if (!srep->have_net_config) |
| 261 | return -ENOTSUP; |
| 262 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 263 | srep->dest_port = port; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 264 | sock_addr->sin_port = osmo_htons(port); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 265 | |
| 266 | return update_srep_config(srep); |
| 267 | } |
| 268 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 269 | /*! Set the local (IP) address of a given stats_reporter. |
| 270 | * \param[in] srep stats_reporter whose remote address is to be set |
| 271 | * \param[in] addr String representation of local IP address |
| 272 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 273 | 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] | 274 | { |
| 275 | int rc; |
| 276 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr; |
| 277 | struct in_addr inaddr; |
| 278 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 279 | if (!srep->have_net_config) |
| 280 | return -ENOTSUP; |
| 281 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 282 | if (addr) { |
| 283 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 284 | if (rc <= 0) |
| 285 | return -EINVAL; |
| 286 | } else { |
Holger Hans Peter Freyther | 7921975 | 2015-11-02 15:50:32 +0100 | [diff] [blame] | 287 | inaddr.s_addr = INADDR_ANY; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | sock_addr->sin_addr = inaddr; |
| 291 | sock_addr->sin_family = AF_INET; |
| 292 | srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0; |
| 293 | |
| 294 | talloc_free(srep->bind_addr_str); |
| 295 | srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL; |
| 296 | |
| 297 | return update_srep_config(srep); |
| 298 | } |
| 299 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 300 | /*! Set the maximum transmission unit of a given stats_reporter. |
| 301 | * \param[in] srep stats_reporter whose remote address is to be set |
| 302 | * \param[in] mtu Maximum Transmission Unit of \a srep |
| 303 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 306 | if (!srep->have_net_config) |
| 307 | return -ENOTSUP; |
| 308 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 309 | if (mtu < 0) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | srep->mtu = mtu; |
| 313 | |
| 314 | return update_srep_config(srep); |
| 315 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 316 | #endif /* HAVE_SYS_SOCKETS_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 317 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 318 | int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, |
| 319 | enum osmo_stats_class class_id) |
| 320 | { |
| 321 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 322 | return -EINVAL; |
| 323 | |
| 324 | srep->max_class = class_id; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 329 | /*! Set the reporting interval of a given stats_reporter (in seconds). |
| 330 | * \param[in] srep stats_reporter whose remote address is to be set |
| 331 | * \param[in] interval Reporting interval in seconds |
| 332 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 333 | int osmo_stats_set_interval(int interval) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 334 | { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 335 | if (interval <= 0) |
| 336 | return -EINVAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 337 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 338 | osmo_stats_config->interval = interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 339 | if (is_initialised) |
| 340 | start_timer(); |
| 341 | |
| 342 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 345 | /*! Set the name prefix of a given stats_reporter. |
| 346 | * \param[in] srep stats_reporter whose name prefix is to be set |
| 347 | * \param[in] prefix NAme perfix to pre-pend for any reported value |
| 348 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 349 | 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] | 350 | { |
| 351 | talloc_free(srep->name_prefix); |
Jacob Erlbeck | 916423e | 2015-11-09 10:52:19 +0100 | [diff] [blame] | 352 | srep->name_prefix = prefix && strlen(prefix) > 0 ? |
| 353 | talloc_strdup(srep, prefix) : NULL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 354 | |
| 355 | return update_srep_config(srep); |
| 356 | } |
| 357 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 358 | |
| 359 | /*! Enable the given stats_reporter. |
| 360 | * \param[in] srep stats_reporter who is to be enabled |
| 361 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 362 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 363 | { |
| 364 | srep->enabled = 1; |
| 365 | |
| 366 | return update_srep_config(srep); |
| 367 | } |
| 368 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 369 | /*! Disable the given stats_reporter. |
| 370 | * \param[in] srep stats_reporter who is to be disabled |
| 371 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 372 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 373 | { |
| 374 | srep->enabled = 0; |
| 375 | |
| 376 | return update_srep_config(srep); |
| 377 | } |
| 378 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 379 | /*** i/o helper functions ***/ |
| 380 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 381 | #ifdef HAVE_SYS_SOCKET_H |
| 382 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 383 | /*! Open the UDP socket for given stats_reporter. |
| 384 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 385 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 386 | int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep) |
| 387 | { |
| 388 | int sock; |
| 389 | int rc; |
| 390 | int buffer_size = STATS_DEFAULT_BUFLEN; |
| 391 | |
| 392 | if (srep->fd != -1 && srep->close) |
| 393 | srep->close(srep); |
| 394 | |
| 395 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 396 | if (sock == -1) |
| 397 | return -errno; |
| 398 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 399 | #if defined(__APPLE__) && !defined(MSG_NOSIGNAL) |
| 400 | { |
| 401 | static int val = 1; |
| 402 | |
| 403 | rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val)); |
| 404 | goto failed; |
| 405 | } |
| 406 | #endif |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 407 | if (srep->bind_addr_len > 0) { |
| 408 | rc = bind(sock, &srep->bind_addr, srep->bind_addr_len); |
| 409 | if (rc == -1) |
| 410 | goto failed; |
| 411 | } |
| 412 | |
| 413 | srep->fd = sock; |
| 414 | |
| 415 | if (srep->mtu > 0) { |
| 416 | buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */; |
| 417 | srep->agg_enabled = 1; |
| 418 | } |
| 419 | |
| 420 | srep->buffer = msgb_alloc(buffer_size, "stats buffer"); |
| 421 | |
| 422 | return 0; |
| 423 | |
| 424 | failed: |
| 425 | rc = -errno; |
| 426 | close(sock); |
| 427 | |
| 428 | return rc; |
| 429 | } |
| 430 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 431 | /*! Closee the UDP socket for given stats_reporter. |
| 432 | * \param[in] srep stats_reporter whose UDP socket is to be closed |
| 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_close(struct osmo_stats_reporter *srep) |
| 435 | { |
| 436 | int rc; |
| 437 | if (srep->fd == -1) |
| 438 | return -EBADF; |
| 439 | |
| 440 | osmo_stats_reporter_send_buffer(srep); |
| 441 | |
| 442 | rc = close(srep->fd); |
| 443 | srep->fd = -1; |
| 444 | msgb_free(srep->buffer); |
| 445 | srep->buffer = NULL; |
| 446 | return rc == -1 ? -errno : 0; |
| 447 | } |
| 448 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 449 | /*! Send given date to given stats_reporter. |
| 450 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 451 | * \param[in] data string data to be sent |
| 452 | * \param[in] data_len Length of \a data in bytes |
| 453 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 454 | 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] | 455 | int data_len) |
| 456 | { |
| 457 | int rc; |
| 458 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 459 | rc = sendto(srep->fd, data, data_len, |
| 460 | #ifdef MSG_NOSIGNAL |
| 461 | MSG_NOSIGNAL | |
| 462 | #endif |
| 463 | MSG_DONTWAIT, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 464 | &srep->dest_addr, srep->dest_addr_len); |
| 465 | |
| 466 | if (rc == -1) |
| 467 | rc = -errno; |
| 468 | |
| 469 | return rc; |
| 470 | } |
| 471 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 472 | /*! Send current accumulated buffer to given stats_reporter. |
| 473 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 474 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 475 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 476 | { |
| 477 | int rc; |
| 478 | |
| 479 | if (!srep->buffer || msgb_length(srep->buffer) == 0) |
| 480 | return 0; |
| 481 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 482 | rc = osmo_stats_reporter_send(srep, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 483 | (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer)); |
| 484 | |
| 485 | msgb_trim(srep->buffer, 0); |
| 486 | |
| 487 | return rc; |
| 488 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 489 | #endif /* HAVE_SYS_SOCKET_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 490 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 491 | /*** log reporter ***/ |
| 492 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 493 | /*! Create a stats_reporter that logs via libosmocore logging. |
| 494 | * A stats_reporter created via this function will simply print the statistics |
| 495 | * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO |
| 496 | * priority. The configuration of the libosmocore log targets define where this |
| 497 | * information will end up (ignored, text file, stderr, syslog, ...). |
| 498 | * \param[in] name Name of the to-be-created stats_reporter |
| 499 | * \returns stats_reporter on success; NULL on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 500 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 501 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 502 | struct osmo_stats_reporter *srep; |
| 503 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 504 | |
| 505 | srep->have_net_config = 0; |
| 506 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 507 | srep->send_counter = osmo_stats_reporter_log_send_counter; |
| 508 | srep->send_item = osmo_stats_reporter_log_send_item; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 509 | |
| 510 | return srep; |
| 511 | } |
| 512 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 513 | static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 514 | const char *type, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 515 | const char *name1, unsigned int index1, const char *name2, int value, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 516 | const char *unit) |
| 517 | { |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 518 | LOGP(DLSTATS, LOGL_INFO, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 519 | "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] | 520 | type, srep->name_prefix ? srep->name_prefix : "", |
| 521 | name1 ? name1 : "", index1, |
| 522 | name2, value, unit ? unit : ""); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 528 | 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] | 529 | const struct rate_ctr_group *ctrg, |
| 530 | const struct rate_ctr_desc *desc, |
| 531 | int64_t value, int64_t delta) |
| 532 | { |
| 533 | if (ctrg) |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 534 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 535 | ctrg->desc->group_name_prefix, |
| 536 | ctrg->idx, |
| 537 | desc->name, value, NULL); |
| 538 | else |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 539 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 540 | NULL, 0, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 541 | desc->name, value, NULL); |
| 542 | } |
| 543 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 544 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 545 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 546 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 547 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 548 | return osmo_stats_reporter_log_send(srep, "i", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 549 | statg->desc->group_name_prefix, statg->idx, |
| 550 | desc->name, value, desc->unit); |
| 551 | } |
| 552 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 553 | /*** helper for reporting ***/ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 554 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 555 | static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep, |
| 556 | unsigned int index, int class_id) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 557 | { |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 558 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 559 | class_id = index != 0 ? |
| 560 | OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 561 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 562 | return class_id <= srep->max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 565 | /*** generic rate counter support ***/ |
| 566 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 567 | static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 568 | const struct rate_ctr_group *ctrg, |
| 569 | const struct rate_ctr_desc *desc, |
| 570 | int64_t value, int64_t delta) |
| 571 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 572 | if (!srep->send_counter) |
| 573 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 574 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 575 | return srep->send_counter(srep, ctrg, desc, value, delta); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static int rate_ctr_handler( |
| 579 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 580 | const struct rate_ctr_desc *desc, void *sctx_) |
| 581 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 582 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 583 | int64_t delta = rate_ctr_difference(ctr); |
| 584 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 585 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 586 | if (!srep->running) |
| 587 | continue; |
| 588 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 589 | if (delta == 0 && !srep->force_single_flush) |
| 590 | continue; |
| 591 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 592 | if (!osmo_stats_reporter_check_config(srep, |
| 593 | ctrg->idx, ctrg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 594 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 595 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 596 | osmo_stats_reporter_send_counter(srep, ctrg, desc, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 597 | ctr->current, delta); |
| 598 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 599 | /* TODO: handle result (log?, inc counter(!)?) or remove it */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) |
| 606 | { |
| 607 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_); |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 612 | /*** stat item support ***/ |
| 613 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 614 | static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep, |
| 615 | const struct osmo_stat_item_group *statg, |
| 616 | const struct osmo_stat_item_desc *desc, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 617 | int32_t value) |
| 618 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 619 | if (!srep->send_item) |
| 620 | return 0; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 621 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 622 | return srep->send_item(srep, statg, desc, value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 625 | static int osmo_stat_item_handler( |
| 626 | 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] | 627 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 628 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 629 | int32_t idx = current_stat_item_index; |
| 630 | int32_t value; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 631 | int have_value; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 632 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 633 | have_value = osmo_stat_item_get_next(item, &idx, &value) > 0; |
| 634 | if (!have_value) |
| 635 | /* Send the last value in case a flush is requested */ |
| 636 | value = osmo_stat_item_get_last(item); |
| 637 | |
| 638 | do { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 639 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 640 | if (!srep->running) |
| 641 | continue; |
| 642 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 643 | if (!have_value && !srep->force_single_flush) |
| 644 | continue; |
| 645 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 646 | if (!osmo_stats_reporter_check_config(srep, |
| 647 | statg->idx, statg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 648 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 649 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 650 | osmo_stats_reporter_send_item(srep, statg, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 651 | item->desc, value); |
| 652 | } |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 653 | |
| 654 | if (!have_value) |
| 655 | break; |
| 656 | |
| 657 | have_value = osmo_stat_item_get_next(item, &idx, &value) > 0; |
| 658 | } while (have_value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 663 | 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] | 664 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 665 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 666 | |
| 667 | return 0; |
| 668 | } |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 669 | |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 670 | /*** osmo counter support ***/ |
| 671 | |
| 672 | static int handle_counter(struct osmo_counter *counter, void *sctx_) |
| 673 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 674 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 675 | struct rate_ctr_desc desc = {0}; |
| 676 | /* Fake a rate counter description */ |
| 677 | desc.name = counter->name; |
| 678 | desc.description = counter->description; |
| 679 | |
| 680 | int delta = osmo_counter_difference(counter); |
| 681 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 682 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 683 | if (!srep->running) |
| 684 | continue; |
| 685 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 686 | if (delta == 0 && !srep->force_single_flush) |
| 687 | continue; |
| 688 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 689 | osmo_stats_reporter_send_counter(srep, NULL, &desc, |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 690 | counter->value, delta); |
| 691 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 692 | /* TODO: handle result (log?, inc counter(!)?) */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 699 | /*** main reporting function ***/ |
| 700 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 701 | static void flush_all_reporters() |
| 702 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 703 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 704 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 705 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 706 | if (!srep->running) |
| 707 | continue; |
| 708 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 709 | osmo_stats_reporter_send_buffer(srep); |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 710 | srep->force_single_flush = 0; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 714 | int osmo_stats_report() |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 715 | { |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 716 | /* per group actions */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 717 | osmo_counters_for_each(handle_counter, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 718 | rate_ctr_for_each_group(rate_ctr_group_handler, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 719 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 720 | |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 721 | /* global actions */ |
| 722 | osmo_stat_item_discard_all(¤t_stat_item_index); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 723 | flush_all_reporters(); |
| 724 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 725 | return 0; |
| 726 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 727 | |
| 728 | #endif /* !EMBEDDED */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 729 | |
| 730 | /*! @} */ |