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:

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.

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


  1. You can simply to pip install prometheus-client as root. Most people seem to think this is a bad idea, but it works for me. 

  2. I think in principle it would belong into /usr/local/bin, but that does not work out with $PATH in Debian and Ubuntu