summaryrefslogtreecommitdiffstats
path: root/include/osmocom/core/stats.h
blob: e4d46baf359bb3ace63c2ae7d839dcc0d9eaab20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * (C) 2015 by sysmocom - s.f.m.c. GmbH
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#pragma once

/*! \defgroup stats Statistics reporting
 *  @{
 *  \file stats.h */

/* a bit of a crude way to disable building/using this on (bare iron)
 * embedded systems.  We cannot use the autoconf-defined HAVE_... macros
 * here, as that only works at library compile time, not at application
 * compile time */
#ifdef unix

#include <sys/socket.h>
#include <arpa/inet.h>

#include <osmocom/core/linuxlist.h>

#include <stdint.h>

struct msgb;
struct osmo_stat_item_group;
struct osmo_stat_item_desc;
struct rate_ctr_group;
struct rate_ctr_desc;

/*! Statistics Class definitions */
enum osmo_stats_class {
	OSMO_STATS_CLASS_UNKNOWN,	/*!< unknown class */
	OSMO_STATS_CLASS_GLOBAL,	/*!< global counter/stat_item */
	OSMO_STATS_CLASS_PEER,		/*!< peer in a communications link */
	OSMO_STATS_CLASS_SUBSCRIBER,	/*!< subscriber */
};

/*! Statistics Reporter Type */
enum osmo_stats_reporter_type {
	OSMO_STATS_REPORTER_LOG,	/*!< libosmocore logging */
	OSMO_STATS_REPORTER_STATSD,	/*!< statsd backend */
};

/*! One statistics reporter instance. */
struct osmo_stats_reporter {
	/*! Type of the reporter (log, statsd) */
	enum osmo_stats_reporter_type type;
	/*! Human-readable name of this reporter */
	char *name;

	unsigned int have_net_config : 1;

	/* config */
	int enabled;		/*!< is this reporter enabled */
	char *name_prefix;	/*!< prefix for counter names */
	char *dest_addr_str;	/*!< destination IP address */
	char *bind_addr_str;	/*!< local bind IP address */
	int dest_port;		/*!< destination (UDP) port */
	int mtu;		/*!< Maximum Transmission Unit */

	/*! Maximum class/index to report. FIXME: More details! */
	enum osmo_stats_class max_class;

	/* state */

	int running;			/*!< is this reporter running */
	struct sockaddr dest_addr;	/*!< destination address of socket */
	int dest_addr_len;		/*!< length of \a dest_addr in bytes */
	struct sockaddr bind_addr;	/*!< local bind address of socket */
	int bind_addr_len;		/*!< length of \a bind_addr in bytes */
	int fd;				/*!< file descriptor of socket */
	struct msgb *buffer;		/*!< message buffer for log output */
	int agg_enabled;		/*!< is aggregation enabled? */
	int force_single_flush;

	struct llist_head list;
	int (*open)(struct osmo_stats_reporter *srep);
	int (*close)(struct osmo_stats_reporter *srep);
	int (*send_counter)(struct osmo_stats_reporter *srep,
		const struct rate_ctr_group *ctrg,
		const struct rate_ctr_desc *desc,
		int64_t value, int64_t delta);
	int (*send_item)(struct osmo_stats_reporter *srep,
		const struct osmo_stat_item_group *statg,
		const struct osmo_stat_item_desc *desc,
		int64_t value);
};

struct osmo_stats_config {
	int interval;
};

extern struct osmo_stats_config *osmo_stats_config;

void osmo_stats_init(void *ctx);
int osmo_stats_report();

int osmo_stats_set_interval(int interval);

struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
	const char *name);
void osmo_stats_reporter_free(struct osmo_stats_reporter *srep);

struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
	const char *name);

int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr);
int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port);
int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr);
int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu);
int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
	enum osmo_stats_class class_id);
int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix);
int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep);
int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep);

/* reporter creation */
struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name);
struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name);

/* helper functions for reporter implementations */
int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
	int data_len);
int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep);
int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep);
int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep);

#endif /* unix */
/*! @} */