Asterisk - The Open Source Telephony Project  18.5.0
res/prometheus/cli.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2019 Sangoma, Inc.
5  *
6  * Matt Jordan <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \brief Prometheus CLI Commands
22  *
23  * \author Matt Jordan <[email protected]>
24  *
25  */
26 #include "asterisk.h"
27 
28 #include "asterisk/cli.h"
29 #include "asterisk/localtime.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 {
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 }
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  * \internal
121  * \brief Callback invoked when the core module is unloaded
122  */
123 static void cli_unload_cb(void)
124 {
125  ast_cli_unregister_multiple(cli_prometheus, ARRAY_LEN(cli_prometheus));
126 }
127 
128 /*!
129  * \internal
130  * \brief Provider definition
131  */
133  .name = "cli",
134  .unload_cb = cli_unload_cb,
135 };
136 
137 int cli_init(void)
138 {
140  ast_cli_register_multiple(cli_prometheus, ARRAY_LEN(cli_prometheus));
141 
142  return 0;
143 }
static char * prometheus_show_metrics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * config
Definition: conf2ael.c:66
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
Prometheus general configuration.
descriptor for a cli entry.
Definition: cli.h:171
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
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 ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
int cli_init(void)
Initialize CLI command.
#define ast_strlen_zero(foo)
Definition: strings.h:52
static void cli_unload_cb(void)
int64_t prometheus_last_scrape_duration_get(void)
Retrieve the amount of time it took to perform the last scrape.
Custom localtime functions for multiple timezones.
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
static struct prometheus_metrics_provider provider
void prometheus_metrics_provider_register(const struct prometheus_metrics_provider *provider)
Register a metrics provider.
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
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 * name
Handy name of the provider for debugging purposes.
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 ast_str * prometheus_scrape_to_string(void)
Get the raw output of what a scrape would produce.
Standard Command Line Interface.
static char * prometheus_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
A function table for a metrics provider.
static struct ast_cli_entry cli_prometheus[]
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.
static struct test_val a