Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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 <mjordan@digium.com>
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 <mjordan@digium.com>
24 *
25 */
26#include "asterisk.h"
27
28#include "asterisk/cli.h"
29#include "asterisk/localtime.h"
31#include "prometheus_internal.h"
32
33static 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
63static 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
114static 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 */
123static void cli_unload_cb(void)
124{
126}
127
128/*!
129 * \internal
130 * \brief Provider definition
131 */
133 .name = "cli",
134 .unload_cb = cli_unload_cb,
135};
136
137int cli_init(void)
138{
141
142 return 0;
143}
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#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
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
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
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
Custom localtime functions for multiple timezones.
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
Prometheus Metric Internal API.
struct ast_str * prometheus_scrape_to_string(void)
Get the raw output of what a scrape would produce.
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.
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)
int cli_init(void)
Initialize CLI command.
static void cli_unload_cb(void)
static struct ast_cli_entry cli_prometheus[]
static struct prometheus_metrics_provider provider
Asterisk Prometheus Metrics.
void prometheus_metrics_provider_register(const struct prometheus_metrics_provider *provider)
Register a metrics provider.
struct prometheus_general_config * prometheus_general_config_get(void)
Retrieve the current configuration of the module.
#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
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Support for dynamic strings.
Definition: strings.h:623
Prometheus general configuration.
A function table for a metrics provider.
const char * name
Handy name of the provider for debugging purposes.
static struct test_val a
#define ARRAY_LEN(a)
Definition: utils.h:666