blob: a034a61640627b20a06c376d5f488d433af9e89a [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 *
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020016 */
17#pragma once
18
Harald Welteeb5b6ce2017-10-15 20:03:24 +020019/*! \defgroup stats Statistics reporting
20 * @{
21 * \file stats.h */
22
Harald Welte67bdd802017-01-15 17:56:11 +010023/* a bit of a crude way to disable building/using this on (bare iron)
24 * embedded systems. We cannot use the autoconf-defined HAVE_... macros
25 * here, as that only works at library compile time, not at application
26 * compile time */
Pau Espin Pedrol2bf01d42018-12-10 10:59:13 +010027#if defined(unix) || defined(__APPLE__)
Harald Welte67bdd802017-01-15 17:56:11 +010028
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020029#include <sys/socket.h>
Harald Welte67bdd802017-01-15 17:56:11 +010030#include <arpa/inet.h>
31
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020032#include <osmocom/core/linuxlist.h>
33
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000034#include <stdint.h>
35
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010036struct msgb;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010037struct osmo_stat_item_group;
38struct osmo_stat_item_desc;
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010039struct rate_ctr_group;
40struct rate_ctr_desc;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010041
Harald Welteeb5b6ce2017-10-15 20:03:24 +020042/*! Statistics Class definitions */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010043enum osmo_stats_class {
Harald Welteeb5b6ce2017-10-15 20:03:24 +020044 OSMO_STATS_CLASS_UNKNOWN, /*!< unknown class */
45 OSMO_STATS_CLASS_GLOBAL, /*!< global counter/stat_item */
46 OSMO_STATS_CLASS_PEER, /*!< peer in a communications link */
47 OSMO_STATS_CLASS_SUBSCRIBER, /*!< subscriber */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010048};
49
Harald Welteeb5b6ce2017-10-15 20:03:24 +020050/*! Statistics Reporter Type */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010051enum osmo_stats_reporter_type {
Harald Welteeb5b6ce2017-10-15 20:03:24 +020052 OSMO_STATS_REPORTER_LOG, /*!< libosmocore logging */
53 OSMO_STATS_REPORTER_STATSD, /*!< statsd backend */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020054};
55
Harald Welteeb5b6ce2017-10-15 20:03:24 +020056/*! One statistics reporter instance. */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010057struct osmo_stats_reporter {
Harald Welteeb5b6ce2017-10-15 20:03:24 +020058 /*! Type of the reporter (log, statsd) */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010059 enum osmo_stats_reporter_type type;
Harald Welteeb5b6ce2017-10-15 20:03:24 +020060 /*! Human-readable name of this reporter */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020061 char *name;
62
Jacob Erlbecked197fd2015-10-27 14:43:24 +010063 unsigned int have_net_config : 1;
64
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020065 /* config */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020066 int enabled; /*!< is this reporter enabled */
67 char *name_prefix; /*!< prefix for counter names */
68 char *dest_addr_str; /*!< destination IP address */
69 char *bind_addr_str; /*!< local bind IP address */
70 int dest_port; /*!< destination (UDP) port */
71 int mtu; /*!< Maximum Transmission Unit */
Alexander Chemerisdfebf402020-05-08 19:10:40 +030072 unsigned int flush_period; /*!< period between regular flushes */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020073
74 /*! Maximum class/index to report. FIXME: More details! */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010075 enum osmo_stats_class max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020076
77 /* state */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020078
79 int running; /*!< is this reporter running */
80 struct sockaddr dest_addr; /*!< destination address of socket */
81 int dest_addr_len; /*!< length of \a dest_addr in bytes */
82 struct sockaddr bind_addr; /*!< local bind address of socket */
83 int bind_addr_len; /*!< length of \a bind_addr in bytes */
84 int fd; /*!< file descriptor of socket */
85 struct msgb *buffer; /*!< message buffer for log output */
86 int agg_enabled; /*!< is aggregation enabled? */
Alexander Chemerisdfebf402020-05-08 19:10:40 +030087 int force_single_flush; /*!< set to 1 to force a flush (send even unchanged stats values) */
88 unsigned int flush_period_counter; /*!< count sends between forced flushes */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020089
90 struct llist_head list;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010091 int (*open)(struct osmo_stats_reporter *srep);
92 int (*close)(struct osmo_stats_reporter *srep);
93 int (*send_counter)(struct osmo_stats_reporter *srep,
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010094 const struct rate_ctr_group *ctrg,
95 const struct rate_ctr_desc *desc,
96 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010097 int (*send_item)(struct osmo_stats_reporter *srep,
98 const struct osmo_stat_item_group *statg,
99 const struct osmo_stat_item_desc *desc,
Harald Welte1554f802016-11-11 15:06:06 +0100100 int64_t value);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200101};
102
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100103struct osmo_stats_config {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100104 int interval;
105};
106
Vadim Yanitskiybfc83772021-11-08 23:14:29 +0300107extern struct llist_head osmo_stats_reporter_list;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100108extern struct osmo_stats_config *osmo_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100109
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100110void osmo_stats_init(void *ctx);
Harald Welte1e1436c2022-05-08 09:57:04 +0200111int osmo_stats_report(void);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200112
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100113int osmo_stats_set_interval(int interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100114
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100115struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200116 const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100117void osmo_stats_reporter_free(struct osmo_stats_reporter *srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100118
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100119struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200120 const char *name);
121
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100122int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr);
123int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port);
124int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr);
125int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu);
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100126int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
127 enum osmo_stats_class class_id);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100128int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix);
129int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep);
130int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300131int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period);
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100132
133/* reporter creation */
134struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name);
135struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name);
136
137/* helper functions for reporter implementations */
138int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
139 int data_len);
140int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep);
141int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep);
142int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep);
Harald Welte67bdd802017-01-15 17:56:11 +0100143
Pau Espin Pedrol2bf01d42018-12-10 10:59:13 +0100144#endif /* unix || __APPLE__ */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200145/*! @} */