Commit d2a9826e authored by ale's avatar ale

Complete Prometheus integration

If the --node-exporter-dir exists, runcron will write a small textfile
snippet in there (depending on the cron job name) at the end of every
execution.
parent 0c63b5fe
runcron (1.1) unstable; urgency=medium
* Fix version number.
-- ale <ale@incal.net> Fri, 12 Oct 2018 18:02:08 +0100
runcron (0.1) unstable; urgency=low
* Initial Release.
......
......@@ -8,7 +8,7 @@ DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk
# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
......
......@@ -27,6 +27,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
......@@ -49,6 +50,8 @@ char *lock_dir = "/var/tmp";
struct timespec one_second = {.tv_sec = 1, .tv_nsec = 0};
time_t start_time, end_time;
int usage() {
fprintf(
stderr,
......@@ -96,6 +99,17 @@ int usage() {
" wait forever. Specifying this option implies "
"--wait.\n"
"\n"
"Options for Prometheus integration:\n"
"\n"
" --node-exporter-dir PATH\n"
" Look for the prometheus-node-exporter state directory "
"in PATH\n"
" (default /var/lib/prometheus/node-exporter). If the "
"directory\n"
" exists, the program will write a small node-exporter "
"textfile\n"
" snippet in there.\n"
"\n"
"If you are passing options to COMMAND, remember to use `--' to tell the "
"runcron\n"
"option parser to stop interpreting options for itself.\n"
......@@ -378,6 +392,8 @@ int run(char **argv) {
}
}
start_time = time(NULL);
child_pid = fork();
if (child_pid < 0) {
fprintf(stderr, "Error: fork(): %s\n", strerror(errno));
......@@ -433,8 +449,52 @@ int run(char **argv) {
return exit_status;
}
int export_metrics(int exit_status) {
struct stat sb;
char *tmp_file = NULL, *metrics_file = NULL;
int elapsed;
FILE *fp;
if (stat(node_exporter_dir, &sb) < 0 || ((sb.st_mode & S_IFMT) != S_IFDIR)) {
return 0;
}
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");
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");
return -1;
}
end_time = time(NULL);
elapsed = end_time - start_time;
fp = fopen(tmp_file, "w");
if (fp == NULL) {
fprintf(stderr, "Error: could not open %s: %s\n", metrics_file, 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);
if (exit_status == 0) {
fprintf(fp, "cronjob_last_success{cronjob=\"%s\"} %ld\n", job_name, end_time);
}
fclose(fp);
if (rename(tmp_file, metrics_file) < 0) {
fprintf(stderr, "Error: could not write metrics: %s\n", strerror(errno));
unlink(tmp_file);
return -1;
}
return 0;
}
int main(int argc, char **argv) {
int c;
int c, r;
while (1) {
int option_index = 0;
......@@ -541,5 +601,7 @@ int main(int argc, char **argv) {
lock_with_timeout();
}
return run(argv + optind);
r = run(argv + optind);
export_metrics(r);
return r;
}
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