Skip to content
Snippets Groups Projects
Commit 398b0bd4 authored by ale's avatar ale
Browse files

Fix Prometheus exporter

Hopefully by explicitly setting type and help messages the node exporter
will be able to merge values for the same metric when spread across
multiple files.
parent 197e5977
No related branches found
No related tags found
No related merge requests found
runcron (1.1.1) unstable; urgency=medium
* Fix Prometheus exporter.
-- ale <ale@incal.net> Sat, 09 Feb 2019 10:55:59 +0000
runcron (1.1) unstable; urgency=medium runcron (1.1) unstable; urgency=medium
* Fix version number. * Fix version number.
......
...@@ -466,6 +466,14 @@ int run(char **argv) { ...@@ -466,6 +466,14 @@ int run(char **argv) {
return exit_status; return exit_status;
} }
static void print_prom_metric(FILE *fp, const char *name, const char *help,
const char *type, long value) {
fprintf(fp, "# HELP %s %s\n"
"# TYPE %s %s\n"
"%s{cronjob=\"%s\"} %ld\n",
name, help, name, type, name, job_name, value);
}
int export_metrics(int exit_status) { int export_metrics(int exit_status) {
struct stat sb; struct stat sb;
char *tmp_file = NULL, *metrics_file = NULL; char *tmp_file = NULL, *metrics_file = NULL;
...@@ -478,12 +486,12 @@ int export_metrics(int exit_status) { ...@@ -478,12 +486,12 @@ int export_metrics(int exit_status) {
if (asprintf(&tmp_file, "%s/.cron-%s.prom.tmp~", node_exporter_dir, if (asprintf(&tmp_file, "%s/.cron-%s.prom.tmp~", node_exporter_dir,
job_name) < 0) { job_name) < 0) {
fprintf(stderr, "Error: could not allocate memory for metrics\n"); fprintf(stderr, "Error: could not allocate memory\n");
return -1; return -1;
} }
if (asprintf(&metrics_file, "%s/cron-%s.prom", node_exporter_dir, job_name) < if (asprintf(&metrics_file, "%s/cron-%s.prom", node_exporter_dir, job_name) <
0) { 0) {
fprintf(stderr, "Error: could not allocate memory for metrics\n"); fprintf(stderr, "Error: could not allocate memory\n");
return -1; return -1;
} }
...@@ -496,14 +504,17 @@ int export_metrics(int exit_status) { ...@@ -496,14 +504,17 @@ int export_metrics(int exit_status) {
strerror(errno)); strerror(errno));
return -1; return -1;
} }
fprintf(fp, "cronjob_status{cronjob=\"%s\"} %d\n", job_name, print_prom_metric(fp, "cronjob_status", "Status of the cron job.", "gauge",
(exit_status == 0) ? 1 : 0); (exit_status == 0) ? 1 : 0);
fprintf(fp, "cronjob_last_start{cronjob=\"%s\"} %ld\n", job_name, start_time); print_prom_metric(fp, "cronjob_last_start", "Last cron job start time.",
fprintf(fp, "cronjob_last_end{cronjob=\"%s\"} %ld\n", job_name, end_time); "gauge", start_time);
fprintf(fp, "cronjob_runtime_seconds{cronjob=\"%s\"} %d\n", job_name, print_prom_metric(fp, "cronjob_last_end", "Last cron job end time.", "gauge",
elapsed); end_time);
print_prom_metric(fp, "cronjob_runtime_seconds",
"Duration of the last cron job run.", "gauge", elapsed);
if (exit_status == 0) { if (exit_status == 0) {
fprintf(fp, "cronjob_last_success{cronjob=\"%s\"} %ld\n", job_name, print_prom_metric(fp, "cronjob_last_success",
"End time of the last successful cron job.", "gauge",
end_time); end_time);
} }
fclose(fp); fclose(fp);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment