| 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 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include "asterisk/cli.h" |
| 29 |
#include "asterisk/localtime.h" |
| 30 |
#include "asterisk/res_prometheus.h" |
| 31 |
#include "prometheus_internal.h" |
| 32 |
|
| 33 |
static char *prometheus_show_metrics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 34 |
{ |
| 35 |
struct ast_str *response; |
| 36 |
|
| 37 |
if (cmd == CLI_INIT) { |
| 38 |
e->command = "prometheus show metrics"; |
| 39 |
e->usage = |
| 40 |
"Usage: prometheus show metrics\n" |
| 41 |
" Displays the current metrics and their values,\n" |
| 42 |
" without counting as an actual scrape.\n"; |
| 43 |
return NULL; |
| 44 |
} else if (cmd == CLI_GENERATE) { |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
if (a->argc != 3) { |
| 49 |
return CLI_SHOWUSAGE; |
| 50 |
} |
| 51 |
|
| 52 |
response = prometheus_scrape_to_string(); |
| 53 |
if (!response) { |
| 54 |
ast_cli(a->fd, "Egads! An unknown error occurred getting the metrics\n"); |
| 55 |
return CLI_FAILURE; |
| 56 |
} |
| 57 |
ast_cli(a->fd, "%s\n", ast_str_buffer(response)); |
| 58 |
ast_free(response); |
| 59 |
|
| 60 |
return CLI_SUCCESS; |
| 61 |
} |
| 62 |
|
| 63 |
static char *prometheus_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 64 |
{ |
| 65 |
struct prometheus_general_config *config; |
| 66 |
char time_buffer[64]; |
| 67 |
struct ast_tm last_scrape_local; |
| 68 |
struct timeval last_scrape_time; |
| 69 |
int64_t scrape_duration; |
| 70 |
|
| 71 |
if (cmd == CLI_INIT) { |
| 72 |
e->command = "prometheus show status"; |
| 73 |
e->usage = |
| 74 |
"Usage: prometheus show status\n" |
| 75 |
" Displays the status of metrics collection.\n"; |
| 76 |
return NULL; |
| 77 |
} else if (cmd == CLI_GENERATE) { |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
if (a->argc != 3) { |
| 82 |
return CLI_SHOWUSAGE; |
| 83 |
} |
| 84 |
|
| 85 |
config = prometheus_general_config_get(); |
| 86 |
|
| 87 |
ast_cli(a->fd, "Prometheus Metrics Status:\n"); |
| 88 |
ast_cli(a->fd, "\tEnabled: %s\n", config->enabled ? "Yes" : "No"); |
| 89 |
ast_cli(a->fd, "\tURI: %s\n", config->uri); |
| 90 |
ast_cli(a->fd, "\tBasic Auth: %s\n", ast_strlen_zero(config->auth_username) ? "No": "Yes"); |
| 91 |
ast_cli(a->fd, "\tLast Scrape Time: "); |
| 92 |
last_scrape_time = prometheus_last_scrape_time_get(); |
| 93 |
if (last_scrape_time.tv_sec == 0 && last_scrape_time.tv_usec == 0) { |
| 94 |
snprintf(time_buffer, sizeof(time_buffer), "%s", "(N/A)"); |
| 95 |
} else { |
| 96 |
ast_localtime(&last_scrape_time, &last_scrape_local, NULL); |
| 97 |
ast_strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", &last_scrape_local); |
| 98 |
} |
| 99 |
ast_cli(a->fd, "%s\n", time_buffer); |
| 100 |
|
| 101 |
ast_cli(a->fd, "\tLast Scrape Duration: "); |
| 102 |
scrape_duration = prometheus_last_scrape_duration_get(); |
| 103 |
if (scrape_duration < 0) { |
| 104 |
ast_cli(a->fd, "(N/A)\n"); |
| 105 |
} else { |
| 106 |
ast_cli(a->fd, "%" PRIu64 " ms\n", scrape_duration); |
| 107 |
} |
| 108 |
|
| 109 |
ao2_ref(config, -1); |
| 110 |
|
| 111 |
return CLI_SUCCESS; |
| 112 |
} |
| 113 |
|
| 114 |
static struct ast_cli_entry cli_prometheus[] = { |
| 115 |
AST_CLI_DEFINE(prometheus_show_metrics, "Display the current metrics and their values"), |
| 116 |
AST_CLI_DEFINE(prometheus_show_status, "Display the status of Prometheus metrics collection"), |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static void cli_unload_cb(void) |
| 124 |
{ |
| 125 |
ast_cli_unregister_multiple(cli_prometheus, ARRAY_LEN(cli_prometheus)); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static struct prometheus_metrics_provider provider = { |
| 133 |
.name = "cli", |
| 134 |
.unload_cb = cli_unload_cb, |
| 135 |
}; |
| 136 |
|
| 137 |
int cli_init(void) |
| 138 |
{ |
| 139 |
prometheus_metrics_provider_register(&provider); |
| 140 |
ast_cli_register_multiple(cli_prometheus, ARRAY_LEN(cli_prometheus)); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |