Debian Codegraph @

res/prometheus/channels.c

raw symbols SCIP
1
2 /*
3 * Asterisk -- An open source telephony toolkit.
4 *
5 * Copyright (C) 2019 Sangoma, Inc.
6 *
7 * Matt Jordan <[email protected]>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20 /*!
21 * \file
22 * \brief Prometheus Channel Metrics
23 *
24 * \author Matt Jordan <[email protected]>
25 *
26 */
27
28 #include "asterisk.h"
29 #include "asterisk/res_prometheus.h"
30 #include "asterisk/stasis_channels.h"
31 #include "asterisk/pbx.h"
32 #include "prometheus_internal.h"
33
34 #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."
35
36 #define CHANNELS_DURATION_HELP "Individual channel durations (in seconds)."
37
38 /*!
39 * \internal
40 * \brief Callback function to get a channel's current state
41 *
42 * \param metric The metric to populate
43 * \param snapshot Channel snapshot
44 */
45 static void get_channel_state(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
46 {
47 snprintf(metric->value, sizeof(metric->value), "%d", snapshot->state);
48 }
49
50 /*!
51 * \internal
52 * \brief Callback function to get a channel's current duration
53 *
54 * \param metric The metric to populate
55 * \param snapshot Channel snapshot
56 */
57 static void get_channel_duration(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
58 {
59 struct timeval now = ast_tvnow();
60 int64_t duration = ast_tvdiff_sec(now, snapshot->base->creationtime);
61
62 snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
63 }
64
65 /*!
66 * \internal
67 * \brief Helper struct for generating individual channel stats
68 */
69 struct channel_metric_defs {
70 /*!
71 * \brief Help text to display
72 */
73 const char *help;
74 /*!
75 * \brief Name of the metric
76 */
77 const char *name;
78 /*!
79 * \brief Callback function to generate a metric value for a given channel
80 */
81 void (* const get_value)(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot);
82 } channel_metric_defs[] = {
83 {
84 .help = CHANNELS_STATE_HELP,
85 .name = "asterisk_channels_state",
86 .get_value = get_channel_state,
87 },
88 {
89 .help = CHANNELS_DURATION_HELP,
90 .name = "asterisk_channels_duration_seconds",
91 .get_value = get_channel_duration,
92 },
93 };
94
95 static void get_total_call_count(struct prometheus_metric *metric)
96 {
97 snprintf(metric->value, sizeof(metric->value), "%d", ast_processed_calls());
98 }
99
100 static void get_current_call_count(struct prometheus_metric *metric)
101 {
102 snprintf(metric->value, sizeof(metric->value), "%d", ast_active_calls());
103 }
104
105 /*!
106 * \internal
107 * \brief Channel based metrics that are always available
108 */
109 static struct prometheus_metric global_channel_metrics[] = {
110 PROMETHEUS_METRIC_STATIC_INITIALIZATION(
111 PROMETHEUS_METRIC_COUNTER,
112 "asterisk_calls_sum",
113 "Total call count.",
114 &get_total_call_count
115 ),
116 PROMETHEUS_METRIC_STATIC_INITIALIZATION(
117 PROMETHEUS_METRIC_GAUGE,
118 "asterisk_calls_count",
119 "Current call count.",
120 &get_current_call_count
121 ),
122 };
123
124 /*!
125 * \internal
126 * \brief Callback invoked when Prometheus scrapes the server
127 *
128 * \param response The response to populate with formatted metrics
129 */
130 static void channels_scrape_cb(struct ast_str **response)
131 {
132 struct ao2_container *channel_cache;
133 struct ao2_container *channels;
134 struct ao2_iterator it_chans;
135 struct ast_channel_snapshot *snapshot;
136 struct prometheus_metric *channel_metrics;
137 char eid_str[32];
138 int num_channels;
139 int i, j;
140 struct prometheus_metric channel_count = PROMETHEUS_METRIC_STATIC_INITIALIZATION(
141 PROMETHEUS_METRIC_GAUGE,
142 "asterisk_channels_count",
143 "Current channel count.",
144 NULL
145 );
146
147 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
148
149 channel_cache = ast_channel_cache_all();
150 if (!channel_cache) {
151 return;
152 }
153
154 channels = ao2_container_clone(channel_cache, 0);
155 ao2_ref(channel_cache, -1);
156 if (!channels) {
157 return;
158 }
159
160 num_channels = ao2_container_count(channels);
161
162 /* Channel count */
163 PROMETHEUS_METRIC_SET_LABEL(&channel_count, 0, "eid", eid_str);
164 snprintf(channel_count.value, sizeof(channel_count.value), "%d", num_channels);
165 prometheus_metric_to_string(&channel_count, response);
166
167 /* Global call values */
168 for (i = 0; i < ARRAY_LEN(global_channel_metrics); i++) {
169 PROMETHEUS_METRIC_SET_LABEL(&global_channel_metrics[i], 0, "eid", eid_str);
170 global_channel_metrics[i].get_metric_value(&global_channel_metrics[i]);
171 prometheus_metric_to_string(&global_channel_metrics[i], response);
172 }
173
174 if (num_channels == 0) {
175 ao2_ref(channels, -1);
176 return;
177 }
178
179 /* Channel dependent values */
180 channel_metrics = ast_calloc(ARRAY_LEN(channel_metric_defs) * num_channels, sizeof(*channel_metrics));
181 if (!channel_metrics) {
182 ao2_ref(channels, -1);
183 return;
184 }
185
186 it_chans = ao2_iterator_init(channels, 0);
187 for (i = 0; (snapshot = ao2_iterator_next(&it_chans)); ao2_ref(snapshot, -1), i++) {
188 for (j = 0; j < ARRAY_LEN(channel_metric_defs); j++) {
189 int index = i * ARRAY_LEN(channel_metric_defs) + j;
190
191 channel_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
192 ast_copy_string(channel_metrics[index].name, channel_metric_defs[j].name, sizeof(channel_metrics[index].name));
193 channel_metrics[index].help = channel_metric_defs[j].help;
194 PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 0, "eid", eid_str);
195 PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 1, "name", (snapshot->base->name));
196 PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 2, "id", (snapshot->base->uniqueid));
197 PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 3, "type", (snapshot->base->type));
198 if (snapshot->peer) {
199 PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 4, "linkedid", (snapshot->peer->linkedid));
200 }
201 channel_metric_defs[j].get_value(&channel_metrics[index], snapshot);
202
203 if (i > 0) {
204 AST_LIST_INSERT_TAIL(&channel_metrics[j].children, &channel_metrics[index], entry);
205 }
206 }
207 }
208 ao2_iterator_destroy(&it_chans);
209
210 for (j = 0; j < ARRAY_LEN(channel_metric_defs); j++) {
211 prometheus_metric_to_string(&channel_metrics[j], response);
212 }
213
214 ast_free(channel_metrics);
215 ao2_ref(channels, -1);
216 }
217
218 struct prometheus_callback channels_callback = {
219 .name = "Channels callback",
220 .callback_fn = channels_scrape_cb,
221 };
222
223 /*!
224 * \internal
225 * \brief Callback invoked when the core module is unloaded
226 */
227 static void channel_metrics_unload_cb(void)
228 {
229 prometheus_callback_unregister(&channels_callback);
230 }
231
232 /*!
233 * \internal
234 * \brief Metrics provider definition
235 */
236 static struct prometheus_metrics_provider provider = {
237 .name = "channels",
238 .unload_cb = channel_metrics_unload_cb,
239 };
240
241 int channel_metrics_init(void)
242 {
243 prometheus_metrics_provider_register(&provider);
244 prometheus_callback_register(&channels_callback);
245
246 return 0;
247 }