Asterisk - The Open Source Telephony Project GIT-master-f36a736
Functions | Variables
res/prometheus/cli.c File Reference

Prometheus CLI Commands. More...

#include "asterisk.h"
#include "asterisk/cli.h"
#include "asterisk/localtime.h"
#include "asterisk/res_prometheus.h"
#include "prometheus_internal.h"
Include dependency graph for res/prometheus/cli.c:

Go to the source code of this file.

Functions

int cli_init (void)
 Initialize CLI command. More...
 
static void cli_unload_cb (void)
 
static char * prometheus_show_metrics (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * prometheus_show_status (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 

Variables

static struct ast_cli_entry cli_prometheus []
 
static struct prometheus_metrics_provider provider
 

Detailed Description

Prometheus CLI Commands.

Author
Matt Jordan mjord.nosp@m.an@d.nosp@m.igium.nosp@m..com

Definition in file res/prometheus/cli.c.

Function Documentation

◆ cli_init()

int cli_init ( void  )

Initialize CLI command.

Return values
0success
-1error

Definition at line 137 of file res/prometheus/cli.c.

138{
141
142 return 0;
143}
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static struct ast_cli_entry cli_prometheus[]
static struct prometheus_metrics_provider provider
void prometheus_metrics_provider_register(const struct prometheus_metrics_provider *provider)
Register a metrics provider.
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_cli_register_multiple, cli_prometheus, prometheus_metrics_provider_register(), and provider.

Referenced by load_module().

◆ cli_unload_cb()

static void cli_unload_cb ( void  )
static

Definition at line 123 of file res/prometheus/cli.c.

124{
126}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

References ARRAY_LEN, ast_cli_unregister_multiple(), and cli_prometheus.

◆ prometheus_show_metrics()

static char * prometheus_show_metrics ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 33 of file res/prometheus/cli.c.

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}
#define ast_free(a)
Definition: astmm.h:180
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define CLI_FAILURE
Definition: cli.h:46
struct ast_str * prometheus_scrape_to_string(void)
Get the raw output of what a scrape would produce.
#define NULL
Definition: resample.c:96
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Support for dynamic strings.
Definition: strings.h:623
static struct test_val a

References a, ast_cli(), ast_free, ast_str_buffer(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, prometheus_scrape_to_string(), and ast_cli_entry::usage.

◆ prometheus_show_status()

static char * prometheus_show_status ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 63 of file res/prometheus/cli.c.

64{
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
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}
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static const char config[]
Definition: chan_ooh323.c:111
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
struct timeval prometheus_last_scrape_time_get(void)
Retrieve the timestamp when the last scrape occurred.
int64_t prometheus_last_scrape_duration_get(void)
Retrieve the amount of time it took to perform the last scrape.
struct prometheus_general_config * prometheus_general_config_get(void)
Retrieve the current configuration of the module.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Prometheus general configuration.

References a, ao2_ref, ast_cli(), ast_localtime(), ast_strftime(), ast_strlen_zero(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, config, NULL, prometheus_general_config_get(), prometheus_last_scrape_duration_get(), prometheus_last_scrape_time_get(), and ast_cli_entry::usage.

Variable Documentation

◆ cli_prometheus

struct ast_cli_entry cli_prometheus[]
static
Initial value:
= {
{ .handler = prometheus_show_metrics , .summary = "Display the current metrics and their values" ,},
{ .handler = prometheus_show_status , .summary = "Display the status of Prometheus metrics collection" ,},
}
static char * prometheus_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * prometheus_show_metrics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Definition at line 114 of file res/prometheus/cli.c.

Referenced by cli_init(), and cli_unload_cb().

◆ provider

struct prometheus_metrics_provider provider
static
Initial value:
= {
.name = "cli",
.unload_cb = cli_unload_cb,
}
static void cli_unload_cb(void)

Definition at line 132 of file res/prometheus/cli.c.

Referenced by cli_init().