From 64380258a6265a63882a3f6ea735e39f18efa49c Mon Sep 17 00:00:00 2001 From: Alex Kraker Date: Sat, 11 May 2024 10:50:57 -0500 Subject: [PATCH 1/3] 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 ; cronjob "" $? ``` 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 --- cronjob | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 cronjob diff --git a/cronjob b/cronjob new file mode 100755 index 0000000..4a08404 --- /dev/null +++ b/cronjob @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# +# Expose cronjob exit status +# +# Usage: ; cronjob "" $? +# +# 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}" From 77a77bbe895e9b781ddc7e22007fed0616a13eba Mon Sep 17 00:00:00 2001 From: Alex Kraker Date: Sat, 25 May 2024 11:06:15 -0500 Subject: [PATCH 2/3] Rename cronjob to cronjob_status Give this script a more descriptive name. This script will be seen in crontab files and this will be more descriptive of what the script is doing. Signed-off-by: Alex Kraker --- cronjob => cronjob_status | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cronjob => cronjob_status (100%) diff --git a/cronjob b/cronjob_status similarity index 100% rename from cronjob rename to cronjob_status From 2ca521766c0a2ee5014b2e4d9b2bcf222dd4113d Mon Sep 17 00:00:00 2001 From: Alex Kraker Date: Sat, 25 May 2024 11:10:52 -0500 Subject: [PATCH 3/3] Update usage comments to reflect filename change Signed-off-by: Alex Kraker --- cronjob_status | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cronjob_status b/cronjob_status index 4a08404..a52915c 100755 --- a/cronjob_status +++ b/cronjob_status @@ -2,10 +2,10 @@ # # Expose cronjob exit status # -# Usage: ; cronjob "" $? +# Usage: ; cronjob_status "" $? # # Example crontab entry: -# * * * * * echo "Hello world!"; cronjob "greeting" $? | sponge /var/lib/prometheus/node-exporter/cronjob_greeting.prom +# * * * * * echo "Hello world!"; cronjob_status "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/ #