blob: 161b34cec7977525363e7d3d0b0a12a32fde6ac5 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file stats.h */
2/*
3 * (C) 2015 by Sysmocom s.f.m.c. GmbH
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02004 *
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 Welte67bdd802017-01-15 17:56:11 +010024/* 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 Erlbeck95bf82802015-10-20 19:05:52 +020030#include <sys/socket.h>
Harald Welte67bdd802017-01-15 17:56:11 +010031#include <arpa/inet.h>
32
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020033#include <osmocom/core/linuxlist.h>
34
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000035#include <stdint.h>
36
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010037struct msgb;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010038struct osmo_stat_item_group;
39struct osmo_stat_item_desc;
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010040struct rate_ctr_group;
41struct rate_ctr_desc;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010042
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010043enum 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 Erlbeckfc9533d2015-10-29 00:55:58 +010050enum osmo_stats_reporter_type {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010051 OSMO_STATS_REPORTER_LOG,
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +010052 OSMO_STATS_REPORTER_STATSD,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020053};
54
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010055struct osmo_stats_reporter {
56 enum osmo_stats_reporter_type type;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020057 char *name;
58
Jacob Erlbecked197fd2015-10-27 14:43:24 +010059 unsigned int have_net_config : 1;
60
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020061 /* config */
62 int enabled;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020063 char *name_prefix;
64 char *dest_addr_str;
65 char *bind_addr_str;
66 int dest_port;
67 int mtu;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010068 enum osmo_stats_class max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020069
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 Erlbeckd01acfc2015-10-26 16:22:45 +010077 struct msgb *buffer;
78 int agg_enabled;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +010079 int force_single_flush;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020080
81 struct llist_head list;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010082 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 Erlbeck490b38f2015-10-27 15:10:28 +010085 const struct rate_ctr_group *ctrg,
86 const struct rate_ctr_desc *desc,
87 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010088 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 Welte1554f802016-11-11 15:06:06 +010091 int64_t value);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020092};
93
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010094struct osmo_stats_config {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +010095 int interval;
96};
97
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010098extern struct osmo_stats_config *osmo_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +010099
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100100void osmo_stats_init(void *ctx);
101int osmo_stats_report();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200102
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103int osmo_stats_set_interval(int interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100104
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200106 const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107void osmo_stats_reporter_free(struct osmo_stats_reporter *srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100108
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200110 const char *name);
111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr);
113int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port);
114int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr);
115int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu);
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100116int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
117 enum osmo_stats_class class_id);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix);
119int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep);
120int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100121
122/* reporter creation */
123struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name);
124struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name);
125
126/* helper functions for reporter implementations */
127int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
128 int data_len);
129int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep);
130int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep);
131int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep);
Harald Welte67bdd802017-01-15 17:56:11 +0100132
133#endif /* unix */