A simple script for cron Prometheus export
30 Nov 2022
I like Prometheus for collecting metrics from servers and programs. Sadly the cron deamon does not support it. So I wrote a simple python script to report some metrics:
- Start time
- Duration of time
- Exit code
It works with the help of the node exporter that is most likely used in most Prometheus setups.
The script
#!/usr/bin/env python3
from prometheus_client import REGISTRY, write_to_textfile, Gauge
import prometheus_client
from time import time
import sys
import subprocess
# debian/ubuntu
#OUTPUT_DIR = "/var/lib/prometheus/node-exporter"
#freebsd
OUTPUT_DIR = "/var/tmp/node_exporter"
jobname = sys.argv[1]
command = sys.argv[2:]
output_file = OUTPUT_DIR+"/"+jobname+".prom"
prometheus_client.REGISTRY.unregister(prometheus_client.GC_COLLECTOR)
prometheus_client.REGISTRY.unregister(prometheus_client.PLATFORM_COLLECTOR)
prometheus_client.REGISTRY.unregister(prometheus_client.PROCESS_COLLECTOR)
start_time = time()
completed = subprocess.run(command)
end_time = time()
exit_code = completed.returncode
Gauge('cron_starttime', 'Start time of cron job', ['job']).labels(jobname).set(start_time)
Gauge('cron_duration', 'Duration of cron job in seconds', ['job']).labels(jobname).set(end_time-start_time)
Gauge('cron_exitcode', 'Exit code of cron job', ['job']).labels(jobname).set(exit_code)
write_to_textfile(output_file, registry=REGISTRY)
How to install
This assumes basic familiarity with Prometheus and node_exporter.
- Install, setup and run the prometheus node_exporter
- Install the
prometheus_client
package for python 1 - Copy the script to
/usr/bin/cron_exporter
2 - Make it executable
- Adjust the
OUTPUT_DIR
variable for your system. I already have examples for your
Usage
Edit your crontab as usual, but put the cron_exporter
and the name of the job in front of every line. So for example
@daily /usr/bin/certbot renew
should become
@daily cron_exporter certbot /usr/bin/certbot renew
Access from prometheus with PromQL
- Duration of certbot:
cron_duration{exported_job="certbot"}
- Time since the last run for all jobs (in minutes):
(time()-cron_starttime)/60
- Exit codes for a job named backup:
cron_exitcode{exported_job="backup"}