Newer
Older
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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2019 Sangoma, Inc.
*
* Matt Jordan <mjordan@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief Prometheus Channel Metrics
*
* \author Matt Jordan <mjordan@digium.com>
*
*/
#include "asterisk.h"
#include "asterisk/res_prometheus.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/pbx.h"
#include "prometheus_internal.h"
#define CHANNELS_STATE_HELP "Individual channel states. 0=down; 1=reserved; 2=offhook; 3=dialing; 4=ring; 5=ringing; 6=up; 7=busy; 8=dialing_offhook; 9=prering."
#define CHANNELS_DURATION_HELP "Individual channel durations (in seconds)."
/*!
* \internal
* \brief Callback function to get a channel's current state
*
* \param metric The metric to populate
* \snapshot Channel snapshot
*/
static void get_channel_state(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
{
snprintf(metric->value, sizeof(metric->value), "%d", snapshot->state);
}
/*!
* \internal
* \brief Callback function to get a channel's current duration
*
* \param metric The metric to populate
* \param snapshot Channel snapshot
*/
static void get_channel_duration(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
{
struct timeval now = ast_tvnow();
int64_t duration = ast_tvdiff_sec(now, snapshot->base->creationtime);
snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
}
/*!
* \internal
* \brief Helper struct for generating individual channel stats
*/
struct channel_metric_defs {
/*!
* \brief Help text to display
*/
const char *help;
/*!
* \brief Name of the metric
*/
const char *name;
/*!
* \brief Callback function to generate a metric value for a given channel
*/
void (* const get_value)(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot);
} channel_metric_defs[] = {
{
.help = CHANNELS_STATE_HELP,
.name = "asterisk_channels_state",
.get_value = get_channel_state,
},
{
.help = CHANNELS_DURATION_HELP,
.name = "asterisk_channels_duration_seconds",
.get_value = get_channel_duration,
},
};
static void get_total_call_count(struct prometheus_metric *metric)
{
snprintf(metric->value, sizeof(metric->value), "%d", ast_processed_calls());
}
static void get_current_call_count(struct prometheus_metric *metric)
{
snprintf(metric->value, sizeof(metric->value), "%d", ast_active_calls());
}
/*!
* \internal
* \brief Channel based metrics that are always available
*/
static struct prometheus_metric global_channel_metrics[] = {
PROMETHEUS_METRIC_STATIC_INITIALIZATION(
PROMETHEUS_METRIC_COUNTER,
"asterisk_calls_sum",
"Total call count.",
&get_total_call_count
),
PROMETHEUS_METRIC_STATIC_INITIALIZATION(
PROMETHEUS_METRIC_GAUGE,
"asterisk_calls_count",
"Current call count.",
&get_current_call_count
),
};
/*!
* \internal
* \brief Callback invoked when Prometheus scrapes the server
*
* \param response The response to populate with formatted metrics
*/
static void channels_scrape_cb(struct ast_str **response)
{
struct ao2_container *channel_cache;
struct ao2_container *channels;
struct ao2_iterator it_chans;
struct ast_channel_snapshot *snapshot;
struct prometheus_metric *channel_metrics;
char eid_str[32];
int num_channels;
int i, j;
struct prometheus_metric channel_count = PROMETHEUS_METRIC_STATIC_INITIALIZATION(
PROMETHEUS_METRIC_GAUGE,
"asterisk_channels_count",
"Current channel count.",
NULL
);
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
channel_cache = ast_channel_cache_all();
if (!channel_cache) {
return;
}
channels = ao2_container_clone(channel_cache, 0);
ao2_ref(channel_cache, -1);
if (!channels) {
return;
}
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
num_channels = ao2_container_count(channels);
/* Channel count */
PROMETHEUS_METRIC_SET_LABEL(&channel_count, 0, "eid", eid_str);
snprintf(channel_count.value, sizeof(channel_count.value), "%d", num_channels);
prometheus_metric_to_string(&channel_count, response);
/* Global call values */
for (i = 0; i < ARRAY_LEN(global_channel_metrics); i++) {
PROMETHEUS_METRIC_SET_LABEL(&global_channel_metrics[i], 0, "eid", eid_str);
global_channel_metrics[i].get_metric_value(&global_channel_metrics[i]);
prometheus_metric_to_string(&global_channel_metrics[i], response);
}
if (num_channels == 0) {
ao2_ref(channels, -1);
return;
}
/* Channel dependent values */
channel_metrics = ast_calloc(ARRAY_LEN(channel_metric_defs) * num_channels, sizeof(*channel_metrics));
if (!channel_metrics) {
ao2_ref(channels, -1);
return;
}
it_chans = ao2_iterator_init(channels, 0);
for (i = 0; (snapshot = ao2_iterator_next(&it_chans)); ao2_ref(snapshot, -1), i++) {
for (j = 0; j < ARRAY_LEN(channel_metric_defs); j++) {
int index = i * ARRAY_LEN(channel_metric_defs) + j;
channel_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
ast_copy_string(channel_metrics[index].name, channel_metric_defs[j].name, sizeof(channel_metrics[index].name));
channel_metrics[index].help = channel_metric_defs[j].help;
PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 0, "eid", eid_str);
PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 1, "name", (snapshot->base->name));
PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 2, "id", (snapshot->base->uniqueid));
PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 3, "type", (snapshot->base->type));
if (snapshot->peer) {
PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 4, "linkedid", (snapshot->peer->linkedid));
}
channel_metric_defs[j].get_value(&channel_metrics[index], snapshot);
if (i > 0) {
AST_LIST_INSERT_TAIL(&channel_metrics[j].children, &channel_metrics[index], entry);
}
}
}
ao2_iterator_destroy(&it_chans);
for (j = 0; j < ARRAY_LEN(channel_metric_defs); j++) {
prometheus_metric_to_string(&channel_metrics[j], response);
}
ast_free(channel_metrics);
ao2_ref(channels, -1);
}
struct prometheus_callback channels_callback = {
.name = "Channels callback",
.callback_fn = channels_scrape_cb,
};
/*!
* \internal
* \brief Callback invoked when the core module is unloaded
*/
static void channel_metrics_unload_cb(void)
{
prometheus_callback_unregister(&channels_callback);
}
/*!
* \internal
* \brief Metrics provider definition
*/
static struct prometheus_metrics_provider provider = {
.name = "channels",
.unload_cb = channel_metrics_unload_cb,
};
int channel_metrics_init(void)
{
prometheus_metrics_provider_register(&provider);
prometheus_callback_register(&channels_callback);
return 0;