blob: b9edac2a2db6e68a42e82d574997508a56f7d2e6 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2015 by sysmocom - s.f.m.c. GmbH
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02003 *
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21#pragma once
22
Harald Welteeb5b6ce2017-10-15 20:03:24 +020023/*! \defgroup stats Statistics reporting
24 * @{
25 * \file stats.h */
26
Harald Welte67bdd802017-01-15 17:56:11 +010027/* a bit of a crude way to disable building/using this on (bare iron)
28 * embedded systems. We cannot use the autoconf-defined HAVE_... macros
29 * here, as that only works at library compile time, not at application
30 * compile time */
Pau Espin Pedrol2bf01d42018-12-10 10:59:13 +010031#if defined(unix) || defined(__APPLE__)
Harald Welte67bdd802017-01-15 17:56:11 +010032
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020033#include <sys/socket.h>
Harald Welte67bdd802017-01-15 17:56:11 +010034#include <arpa/inet.h>
35
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020036#include <osmocom/core/linuxlist.h>
37
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000038#include <stdint.h>
39
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010040struct msgb;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010041struct osmo_stat_item_group;
42struct osmo_stat_item_desc;
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010043struct rate_ctr_group;
44struct rate_ctr_desc;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010045
Harald Welteeb5b6ce2017-10-15 20:03:24 +020046/*! Statistics Class definitions */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010047enum osmo_stats_class {
Harald Welteeb5b6ce2017-10-15 20:03:24 +020048 OSMO_STATS_CLASS_UNKNOWN, /*!< unknown class */
49 OSMO_STATS_CLASS_GLOBAL, /*!< global counter/stat_item */
50 OSMO_STATS_CLASS_PEER, /*!< peer in a communications link */
51 OSMO_STATS_CLASS_SUBSCRIBER, /*!< subscriber */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010052};
53
Harald Welteeb5b6ce2017-10-15 20:03:24 +020054/*! Statistics Reporter Type */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010055enum osmo_stats_reporter_type {
Harald Welteeb5b6ce2017-10-15 20:03:24 +020056 OSMO_STATS_REPORTER_LOG, /*!< libosmocore logging */
57 OSMO_STATS_REPORTER_STATSD, /*!< statsd backend */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020058};
59
Harald Welteeb5b6ce2017-10-15 20:03:24 +020060/*! One statistics reporter instance. */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010061struct osmo_stats_reporter {
Harald Welteeb5b6ce2017-10-15 20:03:24 +020062 /*! Type of the reporter (log, statsd) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010063 enum osmo_stats_reporter_type type;
Harald Welteeb5b6ce2017-10-15 20:03:24 +020064 /*! Human-readable name of this reporter */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020065 char *name;
66
Jacob Erlbecked197fd2015-10-27 14:43:24 +010067 unsigned int have_net_config : 1;
68
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020069 /* config */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020070 int enabled; /*!< is this reporter enabled */
71 char *name_prefix; /*!< prefix for counter names */
72 char *dest_addr_str; /*!< destination IP address */
73 char *bind_addr_str; /*!< local bind IP address */
74 int dest_port; /*!< destination (UDP) port */
75 int mtu; /*!< Maximum Transmission Unit */
Alexander Chemerisdfebf402020-05-08 19:10:40 +030076 unsigned int flush_period; /*!< period between regular flushes */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020077
78 /*! Maximum class/index to report. FIXME: More details! */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010079 enum osmo_stats_class max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020080
81 /* state */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020082
83 int running; /*!< is this reporter running */
84 struct sockaddr dest_addr; /*!< destination address of socket */
85 int dest_addr_len; /*!< length of \a dest_addr in bytes */
86 struct sockaddr bind_addr; /*!< local bind address of socket */
87 int bind_addr_len; /*!< length of \a bind_addr in bytes */
88 int fd; /*!< file descriptor of socket */
89 struct msgb *buffer; /*!< message buffer for log output */
90 int agg_enabled; /*!< is aggregation enabled? */
Alexander Chemerisdfebf402020-05-08 19:10:40 +030091 int force_single_flush; /*!< set to 1 to force a flush (send even unchanged stats values) */
92 unsigned int flush_period_counter; /*!< count sends between forced flushes */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020093
94 struct llist_head list;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010095 int (*open)(struct osmo_stats_reporter *srep);
96 int (*close)(struct osmo_stats_reporter *srep);
97 int (*send_counter)(struct osmo_stats_reporter *srep,
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010098 const struct rate_ctr_group *ctrg,
99 const struct rate_ctr_desc *desc,
100 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100101 int (*send_item)(struct osmo_stats_reporter *srep,
102 const struct osmo_stat_item_group *statg,
103 const struct osmo_stat_item_desc *desc,
Harald Welte1554f802016-11-11 15:06:06 +0100104 int64_t value);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200105};
106
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107struct osmo_stats_config {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100108 int interval;
109};
110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111extern struct osmo_stats_config *osmo_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100112
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100113void osmo_stats_init(void *ctx);
114int osmo_stats_report();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200115
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116int osmo_stats_set_interval(int interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100117
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200119 const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120void osmo_stats_reporter_free(struct osmo_stats_reporter *srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100121
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200123 const char *name);
124
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100125int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr);
126int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port);
127int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr);
128int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu);
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100129int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
130 enum osmo_stats_class class_id);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100131int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix);
132int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep);
133int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300134int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100135
136/* reporter creation */
137struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name);
138struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name);
139
140/* helper functions for reporter implementations */
141int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
142 int data_len);
143int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep);
144int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep);
145int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep);
Harald Welte67bdd802017-01-15 17:56:11 +0100146
Pau Espin Pedrol2bf01d42018-12-10 10:59:13 +0100147#endif /* unix || __APPLE__ */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200148/*! @} */