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