blob: 09b836aba8d79b9aa067047378f68385cceb84cd [file] [log] [blame]
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02001/* (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 Welte67bdd802017-01-15 17:56:11 +010022/* 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 Erlbeck95bf82802015-10-20 19:05:52 +020028#include <sys/socket.h>
Harald Welte67bdd802017-01-15 17:56:11 +010029#include <arpa/inet.h>
30
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020031#include <osmocom/core/linuxlist.h>
32
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000033#include <stdint.h>
34
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010035struct msgb;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010036struct osmo_stat_item_group;
37struct osmo_stat_item_desc;
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010038struct rate_ctr_group;
39struct rate_ctr_desc;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010040
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010041enum 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 Erlbeckfc9533d2015-10-29 00:55:58 +010048enum osmo_stats_reporter_type {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010049 OSMO_STATS_REPORTER_LOG,
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010050 OSMO_STATS_REPORTER_STATSD,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020051};
52
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010053struct osmo_stats_reporter {
54 enum osmo_stats_reporter_type type;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020055 char *name;
56
Jacob Erlbecked197fd2015-10-27 14:43:24 +010057 unsigned int have_net_config : 1;
58
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020059 /* config */
60 int enabled;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020061 char *name_prefix;
62 char *dest_addr_str;
63 char *bind_addr_str;
64 int dest_port;
65 int mtu;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010066 enum osmo_stats_class max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020067
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 Erlbeckd01acfc2015-10-26 16:22:45 +010075 struct msgb *buffer;
76 int agg_enabled;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +010077 int force_single_flush;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020078
79 struct llist_head list;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010080 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 Erlbeck490b38f2015-10-27 15:10:28 +010083 const struct rate_ctr_group *ctrg,
84 const struct rate_ctr_desc *desc,
85 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010086 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 Welte1554f802016-11-11 15:06:06 +010089 int64_t value);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020090};
91
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010092struct osmo_stats_config {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +010093 int interval;
94};
95
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010096extern struct osmo_stats_config *osmo_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +010097
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010098void osmo_stats_init(void *ctx);
99int osmo_stats_report();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200100
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101int osmo_stats_set_interval(int interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100102
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200104 const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105void osmo_stats_reporter_free(struct osmo_stats_reporter *srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100106
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200108 const char *name);
109
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100110int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr);
111int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port);
112int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr);
113int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu);
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100114int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
115 enum osmo_stats_class class_id);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix);
117int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep);
118int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100119
120/* reporter creation */
121struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name);
122struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name);
123
124/* helper functions for reporter implementations */
125int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
126 int data_len);
127int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep);
128int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep);
129int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep);
Harald Welte67bdd802017-01-15 17:56:11 +0100130
131#endif /* unix */