Asterisk - The Open Source Telephony Project  18.5.0
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.

References ARRAY_LEN, ast_cli_register_multiple, and prometheus_metrics_provider_register().

Referenced by load_module().

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

◆ cli_unload_cb()

static void cli_unload_cb ( void  )
static

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

References ARRAY_LEN, and ast_cli_unregister_multiple().

124 {
126 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ast_cli_entry 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.

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

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 }
const int argc
Definition: cli.h:160
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
const int fd
Definition: cli.h:159
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_FAILURE
Definition: cli.h:46
#define ast_free(a)
Definition: astmm.h:182
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
struct ast_str * prometheus_scrape_to_string(void)
Get the raw output of what a scrape would produce.

◆ 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.

References ao2_ref, ast_cli_args::argc, ast_cli(), ast_localtime(), ast_strftime(), ast_strlen_zero, prometheus_general_config::auth_username, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, config, prometheus_general_config::enabled, ast_cli_args::fd, NULL, prometheus_general_config_get(), prometheus_last_scrape_duration_get(), prometheus_last_scrape_time_get(), prometheus_general_config::uri, and ast_cli_entry::usage.

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 }
char * config
Definition: conf2ael.c:66
Prometheus general configuration.
const int argc
Definition: cli.h:160
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
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define ast_strlen_zero(foo)
Definition: strings.h:52
int64_t prometheus_last_scrape_duration_get(void)
Retrieve the amount of time it took to perform the last scrape.
struct timeval prometheus_last_scrape_time_get(void)
Retrieve the timestamp when the last scrape occurred.
const int fd
Definition: cli.h:159
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
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
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
const ast_string_field uri
The HTTP URI we register ourselves to.
struct prometheus_general_config * prometheus_general_config_get(void)
Retrieve the current configuration of the module.
const ast_string_field auth_username
Auth username for Basic Auth.
unsigned int enabled
Whether or not the module is enabled.

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_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)

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

◆ 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.