Commit 398b0bd4 authored by ale's avatar ale

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
Pipeline #2205 passed with stages
in 3 minutes and 21 seconds
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
* Fix version number.
......
......@@ -466,6 +466,14 @@ int run(char **argv) {
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) {
struct stat sb;
char *tmp_file = NULL, *metrics_file = NULL;
......@@ -478,12 +486,12 @@ int export_metrics(int exit_status) {
if (asprintf(&tmp_file, "%s/.cron-%s.prom.tmp~", node_exporter_dir,
job_name) < 0) {
fprintf(stderr, "Error: could not allocate memory for metrics\n");
fprintf(stderr, "Error: could not allocate memory\n");
return -1;
}
if (asprintf(&metrics_file, "%s/cron-%s.prom", node_exporter_dir, job_name) <
0) {
fprintf(stderr, "Error: could not allocate memory for metrics\n");
fprintf(stderr, "Error: could not allocate memory\n");
return -1;
}
......@@ -496,15 +504,18 @@ int export_metrics(int exit_status) {
strerror(errno));
return -1;
}
fprintf(fp, "cronjob_status{cronjob=\"%s\"} %d\n", job_name,
(exit_status == 0) ? 1 : 0);
fprintf(fp, "cronjob_last_start{cronjob=\"%s\"} %ld\n", job_name, start_time);
fprintf(fp, "cronjob_last_end{cronjob=\"%s\"} %ld\n", job_name, end_time);
fprintf(fp, "cronjob_runtime_seconds{cronjob=\"%s\"} %d\n", job_name,
elapsed);
print_prom_metric(fp, "cronjob_status", "Status of the cron job.", "gauge",
(exit_status == 0) ? 1 : 0);
print_prom_metric(fp, "cronjob_last_start", "Last cron job start time.",
"gauge", start_time);
print_prom_metric(fp, "cronjob_last_end", "Last cron job end time.", "gauge",
end_time);
print_prom_metric(fp, "cronjob_runtime_seconds",
"Duration of the last cron job run.", "gauge", elapsed);
if (exit_status == 0) {
fprintf(fp, "cronjob_last_success{cronjob=\"%s\"} %ld\n", job_name,
end_time);
print_prom_metric(fp, "cronjob_last_success",
"End time of the last successful cron job.", "gauge",
end_time);
}
fclose(fp);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment