| 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 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#define AST_MODULE_SELF_SYM __internal_res_prometheus_self |
| 144 |
|
| 145 |
#include "asterisk.h" |
| 146 |
|
| 147 |
#include "asterisk/module.h" |
| 148 |
#include "asterisk/vector.h" |
| 149 |
#include "asterisk/http.h" |
| 150 |
#include "asterisk/config_options.h" |
| 151 |
#include "asterisk/ast_version.h" |
| 152 |
#include "asterisk/buildinfo.h" |
| 153 |
#include "asterisk/res_prometheus.h" |
| 154 |
|
| 155 |
#include "prometheus/prometheus_internal.h" |
| 156 |
|
| 157 |
|
| 158 |
AST_MUTEX_DEFINE_STATIC(scrape_lock); |
| 159 |
|
| 160 |
AST_VECTOR(, struct prometheus_metric *) metrics; |
| 161 |
|
| 162 |
AST_VECTOR(, struct prometheus_callback *) callbacks; |
| 163 |
|
| 164 |
AST_VECTOR(, const struct prometheus_metrics_provider *) providers; |
| 165 |
|
| 166 |
static struct timeval last_scrape; |
| 167 |
|
| 168 |
|
| 169 |
struct module_config { |
| 170 |
|
| 171 |
struct prometheus_general_config *general; |
| 172 |
}; |
| 173 |
|
| 174 |
static struct aco_type global_option = { |
| 175 |
.type = ACO_GLOBAL, |
| 176 |
.name = "general", |
| 177 |
.item_offset = offsetof(struct module_config, general), |
| 178 |
.category_match = ACO_WHITELIST_EXACT, |
| 179 |
.category = "general", |
| 180 |
}; |
| 181 |
|
| 182 |
struct aco_type *global_options[] = ACO_TYPES(&global_option); |
| 183 |
|
| 184 |
struct aco_file prometheus_conf = { |
| 185 |
.filename = "prometheus.conf", |
| 186 |
.types = ACO_TYPES(&global_option), |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
static AO2_GLOBAL_OBJ_STATIC(global_config); |
| 191 |
|
| 192 |
static void *module_config_alloc(void); |
| 193 |
static int prometheus_config_pre_apply(void); |
| 194 |
static void prometheus_config_post_apply(void); |
| 195 |
|
| 196 |
CONFIG_INFO_STANDARD(cfg_info, global_config, module_config_alloc, |
| 197 |
.files = ACO_FILES(&prometheus_conf), |
| 198 |
.pre_apply_config = prometheus_config_pre_apply, |
| 199 |
.post_apply_config = prometheus_config_post_apply, |
| 200 |
); |
| 201 |
|
| 202 |
#define CORE_PROPERTIES_HELP "Asterisk instance properties. The value of this will always be 1." |
| 203 |
|
| 204 |
#define CORE_UPTIME_HELP "Asterisk instance uptime in seconds." |
| 205 |
|
| 206 |
#define CORE_LAST_RELOAD_HELP "Time since last Asterisk reload in seconds." |
| 207 |
|
| 208 |
#define CORE_METRICS_SCRAPE_TIME_HELP "Total time taken to collect metrics, in milliseconds" |
| 209 |
|
| 210 |
static void get_core_uptime_cb(struct prometheus_metric *metric) |
| 211 |
{ |
| 212 |
struct timeval now = ast_tvnow(); |
| 213 |
int64_t duration = ast_tvdiff_sec(now, ast_startuptime); |
| 214 |
|
| 215 |
snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration); |
| 216 |
} |
| 217 |
|
| 218 |
static void get_last_reload_cb(struct prometheus_metric *metric) |
| 219 |
{ |
| 220 |
struct timeval now = ast_tvnow(); |
| 221 |
int64_t duration = ast_tvdiff_sec(now, ast_lastreloadtime); |
| 222 |
|
| 223 |
snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static struct prometheus_metric core_scrape_metric = |
| 236 |
PROMETHEUS_METRIC_STATIC_INITIALIZATION( |
| 237 |
PROMETHEUS_METRIC_COUNTER, |
| 238 |
"asterisk_core_scrape_time_ms", |
| 239 |
CORE_METRICS_SCRAPE_TIME_HELP, |
| 240 |
NULL); |
| 241 |
|
| 242 |
#define METRIC_CORE_PROPS_ARRAY_INDEX 0 |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
static struct prometheus_metric core_metrics[] = { |
| 247 |
PROMETHEUS_METRIC_STATIC_INITIALIZATION( |
| 248 |
PROMETHEUS_METRIC_COUNTER, |
| 249 |
"asterisk_core_properties", |
| 250 |
CORE_PROPERTIES_HELP, |
| 251 |
NULL), |
| 252 |
PROMETHEUS_METRIC_STATIC_INITIALIZATION( |
| 253 |
PROMETHEUS_METRIC_COUNTER, |
| 254 |
"asterisk_core_uptime_seconds", |
| 255 |
CORE_UPTIME_HELP, |
| 256 |
get_core_uptime_cb), |
| 257 |
PROMETHEUS_METRIC_STATIC_INITIALIZATION( |
| 258 |
PROMETHEUS_METRIC_COUNTER, |
| 259 |
"asterisk_core_last_reload_seconds", |
| 260 |
CORE_LAST_RELOAD_HELP, |
| 261 |
get_last_reload_cb), |
| 262 |
}; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
static int prometheus_metric_cmp(struct prometheus_metric *left, |
| 275 |
struct prometheus_metric *right) |
| 276 |
{ |
| 277 |
int i; |
| 278 |
ast_debug(5, "Comparison: Names %s == %s\n", left->name, right->name); |
| 279 |
if (strcmp(left->name, right->name)) { |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
for (i = 0; i < PROMETHEUS_MAX_LABELS; i++) { |
| 284 |
ast_debug(5, "Comparison: Label %d Names %s == %s\n", i, |
| 285 |
left->labels[i].name, right->labels[i].name); |
| 286 |
if (strcmp(left->labels[i].name, right->labels[i].name)) { |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
ast_debug(5, "Comparison: Label %d Values %s == %s\n", i, |
| 291 |
left->labels[i].value, right->labels[i].value); |
| 292 |
if (strcmp(left->labels[i].value, right->labels[i].value)) { |
| 293 |
return 0; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
ast_debug(5, "Copmarison: %s (%p) is equal to %s (%p)\n", |
| 298 |
left->name, left, right->name, right); |
| 299 |
return 1; |
| 300 |
} |
| 301 |
|
| 302 |
int prometheus_metric_registered_count(void) |
| 303 |
{ |
| 304 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 305 |
|
| 306 |
return AST_VECTOR_SIZE(&metrics); |
| 307 |
} |
| 308 |
|
| 309 |
int prometheus_metric_register(struct prometheus_metric *metric) |
| 310 |
{ |
| 311 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 312 |
int i; |
| 313 |
|
| 314 |
if (!metric) { |
| 315 |
return -1; |
| 316 |
} |
| 317 |
|
| 318 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 319 |
struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i); |
| 320 |
struct prometheus_metric *child; |
| 321 |
|
| 322 |
if (prometheus_metric_cmp(existing, metric)) { |
| 323 |
ast_log(AST_LOG_NOTICE, |
| 324 |
"Refusing registration of existing Prometheus metric: %s\n", |
| 325 |
metric->name); |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
AST_LIST_TRAVERSE(&existing->children, child, entry) { |
| 330 |
if (prometheus_metric_cmp(child, metric)) { |
| 331 |
ast_log(AST_LOG_NOTICE, |
| 332 |
"Refusing registration of existing Prometheus metric: %s\n", |
| 333 |
metric->name); |
| 334 |
return -1; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if (!strcmp(metric->name, existing->name)) { |
| 339 |
ast_debug(3, "Nesting metric '%s' as child (%p) under existing (%p)\n", |
| 340 |
metric->name, metric, existing); |
| 341 |
AST_LIST_INSERT_TAIL(&existing->children, metric, entry); |
| 342 |
return 0; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
ast_debug(3, "Tracking new root metric '%s'\n", metric->name); |
| 347 |
if (AST_VECTOR_APPEND(&metrics, metric)) { |
| 348 |
ast_log(AST_LOG_WARNING, "Failed to grow vector to make room for Prometheus metric: %s\n", |
| 349 |
metric->name); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
int prometheus_metric_unregister(struct prometheus_metric *metric) |
| 357 |
{ |
| 358 |
if (!metric) { |
| 359 |
return -1; |
| 360 |
} |
| 361 |
|
| 362 |
{ |
| 363 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 364 |
int i; |
| 365 |
|
| 366 |
ast_debug(3, "Removing metric '%s'\n", metric->name); |
| 367 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 368 |
struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
if (prometheus_metric_cmp(existing, metric)) { |
| 375 |
struct prometheus_metric *root; |
| 376 |
|
| 377 |
AST_VECTOR_REMOVE(&metrics, i, 1); |
| 378 |
root = AST_LIST_REMOVE_HEAD(&existing->children, entry); |
| 379 |
if (root) { |
| 380 |
struct prometheus_metric *child; |
| 381 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) { |
| 382 |
AST_LIST_REMOVE_CURRENT(entry); |
| 383 |
AST_LIST_INSERT_TAIL(&root->children, child, entry); |
| 384 |
} |
| 385 |
AST_LIST_TRAVERSE_SAFE_END; |
| 386 |
AST_VECTOR_INSERT_AT(&metrics, i, root); |
| 387 |
} |
| 388 |
prometheus_metric_free(existing); |
| 389 |
return 0; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
if (!strcmp(existing->name, metric->name)) { |
| 397 |
struct prometheus_metric *child; |
| 398 |
|
| 399 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) { |
| 400 |
if (prometheus_metric_cmp(child, metric)) { |
| 401 |
AST_LIST_REMOVE_CURRENT(entry); |
| 402 |
prometheus_metric_free(child); |
| 403 |
return 0; |
| 404 |
} |
| 405 |
} |
| 406 |
AST_LIST_TRAVERSE_SAFE_END; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
return -1; |
| 412 |
} |
| 413 |
|
| 414 |
void prometheus_metric_free(struct prometheus_metric *metric) |
| 415 |
{ |
| 416 |
struct prometheus_metric *child; |
| 417 |
|
| 418 |
if (!metric) { |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
while ((child = AST_LIST_REMOVE_HEAD(&metric->children, entry))) { |
| 423 |
prometheus_metric_free(child); |
| 424 |
} |
| 425 |
ast_mutex_destroy(&metric->lock); |
| 426 |
|
| 427 |
if (metric->allocation_strategy == PROMETHEUS_METRIC_ALLOCD) { |
| 428 |
return; |
| 429 |
} else if (metric->allocation_strategy == PROMETHEUS_METRIC_MALLOCD) { |
| 430 |
ast_free(metric); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
static struct prometheus_metric *prometheus_metric_create(const char *name, const char *help) |
| 444 |
{ |
| 445 |
struct prometheus_metric *metric = NULL; |
| 446 |
|
| 447 |
metric = ast_calloc(1, sizeof(*metric)); |
| 448 |
if (!metric) { |
| 449 |
return NULL; |
| 450 |
} |
| 451 |
metric->allocation_strategy = PROMETHEUS_METRIC_MALLOCD; |
| 452 |
ast_mutex_init(&metric->lock); |
| 453 |
|
| 454 |
ast_copy_string(metric->name, name, sizeof(metric->name)); |
| 455 |
metric->help = help; |
| 456 |
|
| 457 |
return metric; |
| 458 |
} |
| 459 |
|
| 460 |
struct prometheus_metric *prometheus_gauge_create(const char *name, const char *help) |
| 461 |
{ |
| 462 |
struct prometheus_metric *metric; |
| 463 |
|
| 464 |
metric = prometheus_metric_create(name, help); |
| 465 |
if (!metric) { |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
metric->type = PROMETHEUS_METRIC_GAUGE; |
| 469 |
|
| 470 |
return metric; |
| 471 |
} |
| 472 |
|
| 473 |
struct prometheus_metric *prometheus_counter_create(const char *name, const char *help) |
| 474 |
{ |
| 475 |
struct prometheus_metric *metric; |
| 476 |
|
| 477 |
metric = prometheus_metric_create(name, help); |
| 478 |
if (!metric) { |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
metric->type = PROMETHEUS_METRIC_COUNTER; |
| 482 |
|
| 483 |
return metric; |
| 484 |
} |
| 485 |
|
| 486 |
static const char *prometheus_metric_type_to_string(enum prometheus_metric_type type) |
| 487 |
{ |
| 488 |
switch (type) { |
| 489 |
case PROMETHEUS_METRIC_COUNTER: |
| 490 |
return "counter"; |
| 491 |
case PROMETHEUS_METRIC_GAUGE: |
| 492 |
return "gauge"; |
| 493 |
default: |
| 494 |
ast_assert(0); |
| 495 |
return "unknown"; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
static void prometheus_metric_full_to_string(struct prometheus_metric *metric, |
| 507 |
struct ast_str **output) |
| 508 |
{ |
| 509 |
int i; |
| 510 |
int labels_exist = 0; |
| 511 |
|
| 512 |
ast_str_append(output, 0, "%s", metric->name); |
| 513 |
|
| 514 |
for (i = 0; i < PROMETHEUS_MAX_LABELS; i++) { |
| 515 |
if (!ast_strlen_zero(metric->labels[i].name)) { |
| 516 |
labels_exist = 1; |
| 517 |
if (i == 0) { |
| 518 |
ast_str_append(output, 0, "%s", "{"); |
| 519 |
} else { |
| 520 |
ast_str_append(output, 0, "%s", ","); |
| 521 |
} |
| 522 |
ast_str_append(output, 0, "%s=\"%s\"", |
| 523 |
metric->labels[i].name, |
| 524 |
metric->labels[i].value); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
if (labels_exist) { |
| 529 |
ast_str_append(output, 0, "%s", "}"); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
if (ast_strlen_zero(metric->value)) { |
| 536 |
ast_str_append(output, 0, " 0\n"); |
| 537 |
} else { |
| 538 |
ast_str_append(output, 0, " %s\n", metric->value); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
void prometheus_metric_to_string(struct prometheus_metric *metric, |
| 543 |
struct ast_str **output) |
| 544 |
{ |
| 545 |
struct prometheus_metric *child; |
| 546 |
|
| 547 |
ast_str_append(output, 0, "# HELP %s %s\n", metric->name, metric->help); |
| 548 |
ast_str_append(output, 0, "# TYPE %s %s\n", metric->name, |
| 549 |
prometheus_metric_type_to_string(metric->type)); |
| 550 |
prometheus_metric_full_to_string(metric, output); |
| 551 |
AST_LIST_TRAVERSE(&metric->children, child, entry) { |
| 552 |
prometheus_metric_full_to_string(child, output); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
int prometheus_callback_register(struct prometheus_callback *callback) |
| 557 |
{ |
| 558 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 559 |
|
| 560 |
if (!callback || !callback->callback_fn || ast_strlen_zero(callback->name)) { |
| 561 |
return -1; |
| 562 |
} |
| 563 |
|
| 564 |
AST_VECTOR_APPEND(&callbacks, callback); |
| 565 |
|
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
void prometheus_callback_unregister(struct prometheus_callback *callback) |
| 570 |
{ |
| 571 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 572 |
int i; |
| 573 |
|
| 574 |
for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) { |
| 575 |
struct prometheus_callback *entry = AST_VECTOR_GET(&callbacks, i); |
| 576 |
|
| 577 |
if (!strcmp(callback->name, entry->name)) { |
| 578 |
AST_VECTOR_REMOVE(&callbacks, i, 1); |
| 579 |
return; |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
static void scrape_metrics(struct ast_str **response) |
| 585 |
{ |
| 586 |
int i; |
| 587 |
|
| 588 |
for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) { |
| 589 |
struct prometheus_callback *callback = AST_VECTOR_GET(&callbacks, i); |
| 590 |
|
| 591 |
if (!callback) { |
| 592 |
continue; |
| 593 |
} |
| 594 |
|
| 595 |
callback->callback_fn(response); |
| 596 |
} |
| 597 |
|
| 598 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 599 |
struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i); |
| 600 |
|
| 601 |
if (!metric) { |
| 602 |
continue; |
| 603 |
} |
| 604 |
|
| 605 |
ast_mutex_lock(&metric->lock); |
| 606 |
if (metric->get_metric_value) { |
| 607 |
metric->get_metric_value(metric); |
| 608 |
} |
| 609 |
prometheus_metric_to_string(metric, response); |
| 610 |
ast_mutex_unlock(&metric->lock); |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
static int http_callback(struct ast_tcptls_session_instance *ser, |
| 615 |
const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, |
| 616 |
struct ast_variable *get_params, struct ast_variable *headers) |
| 617 |
{ |
| 618 |
RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup); |
| 619 |
struct ast_str *response = NULL; |
| 620 |
struct ast_str *content_type_header = NULL; |
| 621 |
struct timeval start; |
| 622 |
struct timeval end; |
| 623 |
|
| 624 |
|
| 625 |
if (!mod_cfg || !mod_cfg->general->enabled) { |
| 626 |
goto err503; |
| 627 |
} |
| 628 |
|
| 629 |
if (!ast_strlen_zero(mod_cfg->general->auth_username)) { |
| 630 |
struct ast_http_auth *http_auth; |
| 631 |
|
| 632 |
http_auth = ast_http_get_auth(headers); |
| 633 |
if (!http_auth) { |
| 634 |
goto err401; |
| 635 |
} |
| 636 |
|
| 637 |
if (strcmp(http_auth->userid, mod_cfg->general->auth_username)) { |
| 638 |
ast_debug(5, "Invalid username provided for auth request: %s\n", http_auth->userid); |
| 639 |
ao2_ref(http_auth, -1); |
| 640 |
goto err401; |
| 641 |
} |
| 642 |
|
| 643 |
if (strcmp(http_auth->password, mod_cfg->general->auth_password)) { |
| 644 |
ast_debug(5, "Invalid password provided for auth request: %s\n", http_auth->password); |
| 645 |
ao2_ref(http_auth, -1); |
| 646 |
goto err401; |
| 647 |
} |
| 648 |
|
| 649 |
ao2_ref(http_auth, -1); |
| 650 |
} |
| 651 |
|
| 652 |
response = ast_str_create(512); |
| 653 |
content_type_header = ast_str_create(32); |
| 654 |
if (!response || !content_type_header) { |
| 655 |
goto err500; |
| 656 |
} |
| 657 |
|
| 658 |
ast_str_set(&content_type_header, 0, "Content-Type: text/plain\r\n"); |
| 659 |
|
| 660 |
start = ast_tvnow(); |
| 661 |
|
| 662 |
ast_mutex_lock(&scrape_lock); |
| 663 |
|
| 664 |
last_scrape = start; |
| 665 |
scrape_metrics(&response); |
| 666 |
|
| 667 |
if (mod_cfg->general->core_metrics_enabled) { |
| 668 |
int64_t duration; |
| 669 |
|
| 670 |
end = ast_tvnow(); |
| 671 |
duration = ast_tvdiff_ms(end, start); |
| 672 |
snprintf(core_scrape_metric.value, |
| 673 |
sizeof(core_scrape_metric.value), |
| 674 |
"%" PRIu64, |
| 675 |
duration); |
| 676 |
prometheus_metric_to_string(&core_scrape_metric, &response); |
| 677 |
} |
| 678 |
ast_mutex_unlock(&scrape_lock); |
| 679 |
|
| 680 |
ast_http_send(ser, method, 200, "OK", content_type_header, response, 0, 0); |
| 681 |
|
| 682 |
return 0; |
| 683 |
|
| 684 |
err401: |
| 685 |
{ |
| 686 |
struct ast_str *auth_challenge_headers; |
| 687 |
|
| 688 |
auth_challenge_headers = ast_str_create(128); |
| 689 |
if (!auth_challenge_headers) { |
| 690 |
goto err500; |
| 691 |
} |
| 692 |
ast_str_append(&auth_challenge_headers, 0, |
| 693 |
"WWW-Authenticate: Basic realm=\"%s\"\r\n", |
| 694 |
mod_cfg->general->auth_realm); |
| 695 |
|
| 696 |
ast_http_send(ser, method, 401, "Unauthorized", auth_challenge_headers, NULL, 0, 1); |
| 697 |
} |
| 698 |
ast_free(response); |
| 699 |
ast_free(content_type_header); |
| 700 |
return 0; |
| 701 |
err503: |
| 702 |
ast_http_send(ser, method, 503, "Service Unavailable", NULL, NULL, 0, 1); |
| 703 |
ast_free(response); |
| 704 |
ast_free(content_type_header); |
| 705 |
return 0; |
| 706 |
err500: |
| 707 |
ast_http_send(ser, method, 500, "Server Error", NULL, NULL, 0, 1); |
| 708 |
ast_free(response); |
| 709 |
ast_free(content_type_header); |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
struct ast_str *prometheus_scrape_to_string(void) |
| 714 |
{ |
| 715 |
struct ast_str *response; |
| 716 |
|
| 717 |
response = ast_str_create(512); |
| 718 |
if (!response) { |
| 719 |
return NULL; |
| 720 |
} |
| 721 |
|
| 722 |
ast_mutex_lock(&scrape_lock); |
| 723 |
scrape_metrics(&response); |
| 724 |
ast_mutex_unlock(&scrape_lock); |
| 725 |
|
| 726 |
return response; |
| 727 |
} |
| 728 |
|
| 729 |
int64_t prometheus_last_scrape_duration_get(void) |
| 730 |
{ |
| 731 |
int64_t duration; |
| 732 |
|
| 733 |
if (sscanf(core_scrape_metric.value, "%" PRIu64, &duration) != 1) { |
| 734 |
return -1; |
| 735 |
} |
| 736 |
|
| 737 |
return duration; |
| 738 |
} |
| 739 |
|
| 740 |
struct timeval prometheus_last_scrape_time_get(void) |
| 741 |
{ |
| 742 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 743 |
|
| 744 |
return last_scrape; |
| 745 |
} |
| 746 |
|
| 747 |
static void prometheus_general_config_dtor(void *obj) |
| 748 |
{ |
| 749 |
struct prometheus_general_config *config = obj; |
| 750 |
|
| 751 |
ast_string_field_free_memory(config); |
| 752 |
} |
| 753 |
|
| 754 |
void *prometheus_general_config_alloc(void) |
| 755 |
{ |
| 756 |
struct prometheus_general_config *config; |
| 757 |
|
| 758 |
config = ao2_alloc(sizeof(*config), prometheus_general_config_dtor); |
| 759 |
if (!config || ast_string_field_init(config, 32)) { |
| 760 |
return NULL; |
| 761 |
} |
| 762 |
|
| 763 |
return config; |
| 764 |
} |
| 765 |
|
| 766 |
struct prometheus_general_config *prometheus_general_config_get(void) |
| 767 |
{ |
| 768 |
RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup); |
| 769 |
|
| 770 |
if (!mod_cfg) { |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
ao2_bump(mod_cfg->general); |
| 774 |
|
| 775 |
return mod_cfg->general; |
| 776 |
} |
| 777 |
|
| 778 |
void prometheus_general_config_set(struct prometheus_general_config *config) |
| 779 |
{ |
| 780 |
RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup); |
| 781 |
|
| 782 |
if (!mod_cfg) { |
| 783 |
return; |
| 784 |
} |
| 785 |
ao2_replace(mod_cfg->general, config); |
| 786 |
prometheus_config_post_apply(); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
static void module_config_dtor(void *obj) |
| 792 |
{ |
| 793 |
struct module_config *config = obj; |
| 794 |
|
| 795 |
if (config->general) { |
| 796 |
ao2_ref(config->general, -1); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
static void *module_config_alloc(void) |
| 802 |
{ |
| 803 |
struct module_config *config; |
| 804 |
|
| 805 |
config = ao2_alloc(sizeof(*config), module_config_dtor); |
| 806 |
if (!config) { |
| 807 |
return NULL; |
| 808 |
} |
| 809 |
|
| 810 |
config->general = prometheus_general_config_alloc(); |
| 811 |
if (!config->general) { |
| 812 |
ao2_ref(config, -1); |
| 813 |
config = NULL; |
| 814 |
} |
| 815 |
|
| 816 |
return config; |
| 817 |
} |
| 818 |
|
| 819 |
static struct ast_http_uri prometheus_uri = { |
| 820 |
.description = "Prometheus Metrics URI", |
| 821 |
.callback = http_callback, |
| 822 |
.has_subtree = 1, |
| 823 |
.data = NULL, |
| 824 |
.key = __FILE__, |
| 825 |
}; |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
static int prometheus_config_pre_apply(void) |
| 833 |
{ |
| 834 |
struct module_config *config = aco_pending_config(&cfg_info); |
| 835 |
|
| 836 |
if (!config->general->enabled) { |
| 837 |
|
| 838 |
return 0; |
| 839 |
} |
| 840 |
|
| 841 |
if (!ast_strlen_zero(config->general->auth_username) |
| 842 |
&& ast_strlen_zero(config->general->auth_password)) { |
| 843 |
ast_log(AST_LOG_ERROR, "'auth_username' set without a corresponding 'auth_password'\n"); |
| 844 |
return -1; |
| 845 |
} |
| 846 |
|
| 847 |
return 0; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
static void prometheus_config_post_apply(void) |
| 856 |
{ |
| 857 |
RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup); |
| 858 |
int i; |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
prometheus_uri.uri = mod_cfg->general->uri; |
| 865 |
|
| 866 |
|
| 867 |
for (i = 0; i < ARRAY_LEN(core_metrics); i++) { |
| 868 |
prometheus_metric_unregister(&core_metrics[i]); |
| 869 |
} |
| 870 |
if (mod_cfg->general->core_metrics_enabled) { |
| 871 |
char eid_str[32]; |
| 872 |
ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default); |
| 873 |
|
| 874 |
PROMETHEUS_METRIC_SET_LABEL(&core_scrape_metric, 0, "eid", eid_str); |
| 875 |
|
| 876 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX], |
| 877 |
1, "version", ast_get_version()); |
| 878 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX], |
| 879 |
2, "build_options", ast_get_build_opts()); |
| 880 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX], |
| 881 |
3, "build_date", ast_build_date); |
| 882 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX], |
| 883 |
4, "build_os", ast_build_os); |
| 884 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX], |
| 885 |
5, "build_kernel", ast_build_kernel); |
| 886 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX], |
| 887 |
6, "build_host", ast_build_hostname); |
| 888 |
snprintf(core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX].value, |
| 889 |
sizeof(core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX].value), |
| 890 |
"%d", 1); |
| 891 |
|
| 892 |
for (i = 0; i < ARRAY_LEN(core_metrics); i++) { |
| 893 |
PROMETHEUS_METRIC_SET_LABEL(&core_metrics[i], 0, "eid", eid_str); |
| 894 |
prometheus_metric_register(&core_metrics[i]); |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
void prometheus_metrics_provider_register(const struct prometheus_metrics_provider *provider) |
| 900 |
{ |
| 901 |
AST_VECTOR_APPEND(&providers, provider); |
| 902 |
} |
| 903 |
|
| 904 |
static int unload_module(void) |
| 905 |
{ |
| 906 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 907 |
int i; |
| 908 |
|
| 909 |
ast_http_uri_unlink(&prometheus_uri); |
| 910 |
|
| 911 |
for (i = 0; i < AST_VECTOR_SIZE(&providers); i++) { |
| 912 |
const struct prometheus_metrics_provider *provider = AST_VECTOR_GET(&providers, i); |
| 913 |
|
| 914 |
if (!provider->unload_cb) { |
| 915 |
continue; |
| 916 |
} |
| 917 |
|
| 918 |
provider->unload_cb(); |
| 919 |
} |
| 920 |
|
| 921 |
for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) { |
| 922 |
struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i); |
| 923 |
|
| 924 |
prometheus_metric_free(metric); |
| 925 |
} |
| 926 |
AST_VECTOR_FREE(&metrics); |
| 927 |
|
| 928 |
AST_VECTOR_FREE(&callbacks); |
| 929 |
|
| 930 |
AST_VECTOR_FREE(&providers); |
| 931 |
|
| 932 |
aco_info_destroy(&cfg_info); |
| 933 |
ao2_global_obj_release(global_config); |
| 934 |
|
| 935 |
return 0; |
| 936 |
} |
| 937 |
|
| 938 |
static int reload_module(void) { |
| 939 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 940 |
int i; |
| 941 |
struct prometheus_general_config *general_config; |
| 942 |
|
| 943 |
ast_http_uri_unlink(&prometheus_uri); |
| 944 |
if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) { |
| 945 |
return -1; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
general_config = prometheus_general_config_get(); |
| 950 |
for (i = 0; i < AST_VECTOR_SIZE(&providers); i++) { |
| 951 |
const struct prometheus_metrics_provider *provider = AST_VECTOR_GET(&providers, i); |
| 952 |
|
| 953 |
if (!provider->reload_cb) { |
| 954 |
continue; |
| 955 |
} |
| 956 |
|
| 957 |
if (provider->reload_cb(general_config)) { |
| 958 |
ast_log(AST_LOG_WARNING, "Failed to reload metrics provider %s\n", provider->name); |
| 959 |
ao2_ref(general_config, -1); |
| 960 |
return -1; |
| 961 |
} |
| 962 |
} |
| 963 |
ao2_ref(general_config, -1); |
| 964 |
|
| 965 |
if (ast_http_uri_link(&prometheus_uri)) { |
| 966 |
ast_log(AST_LOG_WARNING, "Failed to re-register Prometheus Metrics URI during reload\n"); |
| 967 |
return -1; |
| 968 |
} |
| 969 |
|
| 970 |
return 0; |
| 971 |
} |
| 972 |
|
| 973 |
static int load_module(void) |
| 974 |
{ |
| 975 |
SCOPED_MUTEX(lock, &scrape_lock); |
| 976 |
|
| 977 |
if (AST_VECTOR_INIT(&metrics, 64)) { |
| 978 |
goto cleanup; |
| 979 |
} |
| 980 |
|
| 981 |
if (AST_VECTOR_INIT(&callbacks, 8)) { |
| 982 |
goto cleanup; |
| 983 |
} |
| 984 |
|
| 985 |
if (AST_VECTOR_INIT(&providers, 8)) { |
| 986 |
goto cleanup; |
| 987 |
} |
| 988 |
|
| 989 |
if (aco_info_init(&cfg_info)) { |
| 990 |
goto cleanup; |
| 991 |
} |
| 992 |
aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options, "no", OPT_BOOL_T, 1, FLDSET(struct prometheus_general_config, enabled)); |
| 993 |
aco_option_register(&cfg_info, "core_metrics_enabled", ACO_EXACT, global_options, "yes", OPT_BOOL_T, 1, FLDSET(struct prometheus_general_config, core_metrics_enabled)); |
| 994 |
aco_option_register(&cfg_info, "uri", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct prometheus_general_config, uri)); |
| 995 |
aco_option_register(&cfg_info, "auth_username", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_username)); |
| 996 |
aco_option_register(&cfg_info, "auth_password", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_password)); |
| 997 |
aco_option_register(&cfg_info, "auth_realm", ACO_EXACT, global_options, "Asterisk Prometheus Metrics", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_realm)); |
| 998 |
if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) { |
| 999 |
goto cleanup; |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (cli_init() |
| 1003 |
|| channel_metrics_init() |
| 1004 |
|| endpoint_metrics_init() |
| 1005 |
|| bridge_metrics_init()) { |
| 1006 |
goto cleanup; |
| 1007 |
} |
| 1008 |
|
| 1009 |
if(ast_module_check("res_pjsip_outbound_registration.so")) { |
| 1010 |
|
| 1011 |
if (pjsip_outbound_registration_metrics_init()) |
| 1012 |
goto cleanup; |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (ast_http_uri_link(&prometheus_uri)) { |
| 1016 |
goto cleanup; |
| 1017 |
} |
| 1018 |
|
| 1019 |
return AST_MODULE_LOAD_SUCCESS; |
| 1020 |
|
| 1021 |
cleanup: |
| 1022 |
ast_http_uri_unlink(&prometheus_uri); |
| 1023 |
aco_info_destroy(&cfg_info); |
| 1024 |
AST_VECTOR_FREE(&metrics); |
| 1025 |
AST_VECTOR_FREE(&callbacks); |
| 1026 |
AST_VECTOR_FREE(&providers); |
| 1027 |
|
| 1028 |
return AST_MODULE_LOAD_DECLINE; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk Prometheus Module", |
| 1033 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 1034 |
.load = load_module, |
| 1035 |
.unload = unload_module, |
| 1036 |
.reload = reload_module, |
| 1037 |
.load_pri = AST_MODPRI_DEFAULT, |
| 1038 |
#ifdef HAVE_PJPROJECT |
| 1039 |
|
| 1040 |
.requires = "res_pjsip", |
| 1041 |
.optional_modules = "res_pjsip_outbound_registration", |
| 1042 |
#endif |
| 1043 |
); |