Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 2 | * (C) 2015 by sysmocom - s.f.m.c. GmbH |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 3 | * |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | #pragma once |
| 22 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 23 | /*! \defgroup stats Statistics reporting |
| 24 | * @{ |
| 25 | * \file stats.h */ |
| 26 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 27 | /* a bit of a crude way to disable building/using this on (bare iron) |
| 28 | * embedded systems. We cannot use the autoconf-defined HAVE_... macros |
| 29 | * here, as that only works at library compile time, not at application |
| 30 | * compile time */ |
Pau Espin Pedrol | 2bf01d4 | 2018-12-10 10:59:13 +0100 | [diff] [blame] | 31 | #if defined(unix) || defined(__APPLE__) |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 32 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 33 | #include <sys/socket.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 34 | #include <arpa/inet.h> |
| 35 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 36 | #include <osmocom/core/linuxlist.h> |
| 37 | |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 38 | #include <stdint.h> |
| 39 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 40 | struct msgb; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 41 | struct osmo_stat_item_group; |
| 42 | struct osmo_stat_item_desc; |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 43 | struct rate_ctr_group; |
| 44 | struct rate_ctr_desc; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 45 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 46 | /*! Statistics Class definitions */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 47 | enum osmo_stats_class { |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 48 | OSMO_STATS_CLASS_UNKNOWN, /*!< unknown class */ |
| 49 | OSMO_STATS_CLASS_GLOBAL, /*!< global counter/stat_item */ |
| 50 | OSMO_STATS_CLASS_PEER, /*!< peer in a communications link */ |
| 51 | OSMO_STATS_CLASS_SUBSCRIBER, /*!< subscriber */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 54 | /*! Statistics Reporter Type */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 55 | enum osmo_stats_reporter_type { |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 56 | OSMO_STATS_REPORTER_LOG, /*!< libosmocore logging */ |
| 57 | OSMO_STATS_REPORTER_STATSD, /*!< statsd backend */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 60 | /*! One statistics reporter instance. */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 61 | struct osmo_stats_reporter { |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 62 | /*! Type of the reporter (log, statsd) */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 63 | enum osmo_stats_reporter_type type; |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 64 | /*! Human-readable name of this reporter */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 65 | char *name; |
| 66 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 67 | unsigned int have_net_config : 1; |
| 68 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 69 | /* config */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 70 | int enabled; /*!< is this reporter enabled */ |
| 71 | char *name_prefix; /*!< prefix for counter names */ |
| 72 | char *dest_addr_str; /*!< destination IP address */ |
| 73 | char *bind_addr_str; /*!< local bind IP address */ |
| 74 | int dest_port; /*!< destination (UDP) port */ |
| 75 | int mtu; /*!< Maximum Transmission Unit */ |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 76 | unsigned int flush_period; /*!< period between regular flushes */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 77 | |
| 78 | /*! Maximum class/index to report. FIXME: More details! */ |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 79 | enum osmo_stats_class max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 80 | |
| 81 | /* state */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 82 | |
| 83 | int running; /*!< is this reporter running */ |
| 84 | struct sockaddr dest_addr; /*!< destination address of socket */ |
| 85 | int dest_addr_len; /*!< length of \a dest_addr in bytes */ |
| 86 | struct sockaddr bind_addr; /*!< local bind address of socket */ |
| 87 | int bind_addr_len; /*!< length of \a bind_addr in bytes */ |
| 88 | int fd; /*!< file descriptor of socket */ |
| 89 | struct msgb *buffer; /*!< message buffer for log output */ |
| 90 | int agg_enabled; /*!< is aggregation enabled? */ |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 91 | int force_single_flush; /*!< set to 1 to force a flush (send even unchanged stats values) */ |
| 92 | unsigned int flush_period_counter; /*!< count sends between forced flushes */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 93 | |
| 94 | struct llist_head list; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 95 | int (*open)(struct osmo_stats_reporter *srep); |
| 96 | int (*close)(struct osmo_stats_reporter *srep); |
| 97 | int (*send_counter)(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 98 | const struct rate_ctr_group *ctrg, |
| 99 | const struct rate_ctr_desc *desc, |
| 100 | int64_t value, int64_t delta); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 101 | int (*send_item)(struct osmo_stats_reporter *srep, |
| 102 | const struct osmo_stat_item_group *statg, |
| 103 | const struct osmo_stat_item_desc *desc, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 104 | int64_t value); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | struct osmo_stats_config { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 108 | int interval; |
| 109 | }; |
| 110 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 111 | extern struct osmo_stats_config *osmo_stats_config; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 112 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 113 | void osmo_stats_init(void *ctx); |
| 114 | int osmo_stats_report(); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 115 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 116 | int osmo_stats_set_interval(int interval); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 117 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 118 | 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] | 119 | const char *name); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 120 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 121 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 122 | 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] | 123 | const char *name); |
| 124 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 125 | int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr); |
| 126 | int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port); |
| 127 | int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr); |
| 128 | int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu); |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 129 | int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, |
| 130 | enum osmo_stats_class class_id); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 131 | int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix); |
| 132 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep); |
| 133 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep); |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 134 | int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period); |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 135 | |
| 136 | /* reporter creation */ |
| 137 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name); |
| 138 | struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name); |
| 139 | |
| 140 | /* helper functions for reporter implementations */ |
| 141 | int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data, |
| 142 | int data_len); |
| 143 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep); |
| 144 | int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep); |
| 145 | int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep); |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 146 | |
Pau Espin Pedrol | 2bf01d4 | 2018-12-10 10:59:13 +0100 | [diff] [blame] | 147 | #endif /* unix || __APPLE__ */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 148 | /*! @} */ |