Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 1 | /* (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | #pragma once |
| 21 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 22 | /* a bit of a crude way to disable building/using this on (bare iron) |
| 23 | * embedded systems. We cannot use the autoconf-defined HAVE_... macros |
| 24 | * here, as that only works at library compile time, not at application |
| 25 | * compile time */ |
| 26 | #ifdef unix |
| 27 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 28 | #include <sys/socket.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 29 | #include <arpa/inet.h> |
| 30 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 31 | #include <osmocom/core/linuxlist.h> |
| 32 | |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 35 | struct msgb; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 36 | struct osmo_stat_item_group; |
| 37 | struct osmo_stat_item_desc; |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 38 | struct rate_ctr_group; |
| 39 | struct rate_ctr_desc; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 40 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 41 | enum osmo_stats_class { |
| 42 | OSMO_STATS_CLASS_UNKNOWN, |
| 43 | OSMO_STATS_CLASS_GLOBAL, |
| 44 | OSMO_STATS_CLASS_PEER, |
| 45 | OSMO_STATS_CLASS_SUBSCRIBER, |
| 46 | }; |
| 47 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 48 | enum osmo_stats_reporter_type { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 49 | OSMO_STATS_REPORTER_LOG, |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 50 | OSMO_STATS_REPORTER_STATSD, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 53 | struct osmo_stats_reporter { |
| 54 | enum osmo_stats_reporter_type type; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 55 | char *name; |
| 56 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 57 | unsigned int have_net_config : 1; |
| 58 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 59 | /* config */ |
| 60 | int enabled; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 61 | char *name_prefix; |
| 62 | char *dest_addr_str; |
| 63 | char *bind_addr_str; |
| 64 | int dest_port; |
| 65 | int mtu; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 66 | enum osmo_stats_class max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 67 | |
| 68 | /* state */ |
| 69 | int running; |
| 70 | struct sockaddr dest_addr; |
| 71 | int dest_addr_len; |
| 72 | struct sockaddr bind_addr; |
| 73 | int bind_addr_len; |
| 74 | int fd; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 75 | struct msgb *buffer; |
| 76 | int agg_enabled; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 77 | int force_single_flush; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 78 | |
| 79 | struct llist_head list; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 80 | int (*open)(struct osmo_stats_reporter *srep); |
| 81 | int (*close)(struct osmo_stats_reporter *srep); |
| 82 | int (*send_counter)(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 83 | const struct rate_ctr_group *ctrg, |
| 84 | const struct rate_ctr_desc *desc, |
| 85 | int64_t value, int64_t delta); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 86 | int (*send_item)(struct osmo_stats_reporter *srep, |
| 87 | const struct osmo_stat_item_group *statg, |
| 88 | const struct osmo_stat_item_desc *desc, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 89 | int64_t value); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 92 | struct osmo_stats_config { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 93 | int interval; |
| 94 | }; |
| 95 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 96 | extern struct osmo_stats_config *osmo_stats_config; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 97 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 98 | void osmo_stats_init(void *ctx); |
| 99 | int osmo_stats_report(); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 100 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 101 | int osmo_stats_set_interval(int interval); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 102 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 103 | 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] | 104 | const char *name); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 105 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 106 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | 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] | 108 | const char *name); |
| 109 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 110 | int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr); |
| 111 | int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port); |
| 112 | int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr); |
| 113 | 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] | 114 | int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, |
| 115 | enum osmo_stats_class class_id); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 116 | int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix); |
| 117 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep); |
| 118 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep); |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 119 | |
| 120 | /* reporter creation */ |
| 121 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name); |
| 122 | struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name); |
| 123 | |
| 124 | /* helper functions for reporter implementations */ |
| 125 | int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data, |
| 126 | int data_len); |
| 127 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep); |
| 128 | int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep); |
| 129 | int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep); |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 130 | |
| 131 | #endif /* unix */ |