blob: e4d46baf359bb3ace63c2ae7d839dcc0d9eaab20 [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 */
31#ifdef unix
32
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 */
76
77 /*! Maximum class/index to report. FIXME: More details! */
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010078 enum osmo_stats_class max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020079
80 /* state */
Harald Welteeb5b6ce2017-10-15 20:03:24 +020081
82 int running; /*!< is this reporter running */
83 struct sockaddr dest_addr; /*!< destination address of socket */
84 int dest_addr_len; /*!< length of \a dest_addr in bytes */
85 struct sockaddr bind_addr; /*!< local bind address of socket */
86 int bind_addr_len; /*!< length of \a bind_addr in bytes */
87 int fd; /*!< file descriptor of socket */
88 struct msgb *buffer; /*!< message buffer for log output */
89 int agg_enabled; /*!< is aggregation enabled? */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +010090 int force_single_flush;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020091
92 struct llist_head list;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010093 int (*open)(struct osmo_stats_reporter *srep);
94 int (*close)(struct osmo_stats_reporter *srep);
95 int (*send_counter)(struct osmo_stats_reporter *srep,
Jacob Erlbeck490b38f2015-10-27 15:10:28 +010096 const struct rate_ctr_group *ctrg,
97 const struct rate_ctr_desc *desc,
98 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010099 int (*send_item)(struct osmo_stats_reporter *srep,
100 const struct osmo_stat_item_group *statg,
101 const struct osmo_stat_item_desc *desc,
Harald Welte1554f802016-11-11 15:06:06 +0100102 int64_t value);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200103};
104
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100105struct osmo_stats_config {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100106 int interval;
107};
108
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109extern struct osmo_stats_config *osmo_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100110
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100111void osmo_stats_init(void *ctx);
112int osmo_stats_report();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200113
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100114int osmo_stats_set_interval(int interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100115
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200117 const char *name);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118void osmo_stats_reporter_free(struct osmo_stats_reporter *srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100119
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200121 const char *name);
122
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100123int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr);
124int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port);
125int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr);
126int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu);
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100127int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
128 enum osmo_stats_class class_id);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100129int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix);
130int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep);
131int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep);
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
144#endif /* unix */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200145/*! @} */