Skip to content

Commit

Permalink
This script exposes the exit status of cronjobs.
Browse files Browse the repository at this point in the history
This script was born out of a desire to monitor and alert on cronjobs using
Prometheus and Alertmanager.

This script was inspired by https://janikvonrotz.ch/2020/09/07/monitor-cron-jobs-with-prometheus-grafana-and-node-exporter/

As far as I'm aware there isn't another facility for exposing metrics related
to this in Node Exporter.

This script receives two arguments, the description of the cronjob as a string
and the exit status of the previous command. It prints the metric to stdout.

Usage:

```bash
<command> ; cronjob "<description>" $?
```

Example crontab entry:

```
* * * * * echo "Hello world!"; cronjob "greeting" $? | sponge /var/lib/prometheus/node-exporter/cronjob_greeting.prom
```

Example textfile:

```
[vagrant@rocky8 ~]$ cat /var/lib/prometheus/node-exporter/cronjob_greeting.prom
node_cronjob_status{user="vagrant", description="greeting"} 0
```

Signed-off-by: Alex Kraker <[email protected]>
  • Loading branch information
Alex Kraker committed May 11, 2024
1 parent 18fcb1c commit 6438025
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cronjob
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# Expose cronjob exit status
#
# Usage: <command> ; cronjob "<description>" $?
#
# Example crontab entry:
# * * * * * echo "Hello world!"; cronjob "greeting" $? | sponge /var/lib/prometheus/node-exporter/cronjob_greeting.prom
#
# Inspired by: https://janikvonrotz.ch/2020/09/07/monitor-cron-jobs-with-prometheus-grafana-and-node-exporter/
#
# Author: Alex Kraker (github.com/kraker)

# Unofficial strict mode
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

readonly DESCRIPTION="$1"
readonly STATUS="$2"

echo "# HELP node_cronjob_status Last exit code of cronjob."
echo "# TYPE node_cronjob_status gauge"
echo "node_cronjob_status{user=\"${USER}\", description=\"${DESCRIPTION}\"} ${STATUS}"

0 comments on commit 6438025

Please sign in to comment.