Skip to content

Commit 6438025

Browse files
author
Alex Kraker
committed
This script exposes the exit status of cronjobs.
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]>
1 parent 18fcb1c commit 6438025

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cronjob

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Expose cronjob exit status
4+
#
5+
# Usage: <command> ; cronjob "<description>" $?
6+
#
7+
# Example crontab entry:
8+
# * * * * * echo "Hello world!"; cronjob "greeting" $? | sponge /var/lib/prometheus/node-exporter/cronjob_greeting.prom
9+
#
10+
# Inspired by: https://janikvonrotz.ch/2020/09/07/monitor-cron-jobs-with-prometheus-grafana-and-node-exporter/
11+
#
12+
# Author: Alex Kraker (github.com/kraker)
13+
14+
# Unofficial strict mode
15+
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
16+
set -euo pipefail
17+
IFS=$'\n\t'
18+
19+
readonly DESCRIPTION="$1"
20+
readonly STATUS="$2"
21+
22+
echo "# HELP node_cronjob_status Last exit code of cronjob."
23+
echo "# TYPE node_cronjob_status gauge"
24+
echo "node_cronjob_status{user=\"${USER}\", description=\"${DESCRIPTION}\"} ${STATUS}"

0 commit comments

Comments
 (0)