From 04cd1a4ee30788947e6256163218a9d7323feea7 Mon Sep 17 00:00:00 2001 From: Daniel Dolezal Date: Sun, 12 Apr 2026 00:43:19 +0200 Subject: [PATCH] fix borg_exporter datediff error becuase alpine not have it, changed to python --- prometheus-borg-exporter/borg_exporter.sh | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/prometheus-borg-exporter/borg_exporter.sh b/prometheus-borg-exporter/borg_exporter.sh index 2942df0..e137ea9 100755 --- a/prometheus-borg-exporter/borg_exporter.sh +++ b/prometheus-borg-exporter/borg_exporter.sh @@ -3,11 +3,6 @@ source /etc/borg_exporter.rc source /variables.sh TMP_FILE="/tmp/prometheus-borg" -DATEDIFF=`which datediff` -if [ -z "$DATEDIFF" ]; then - #ubuntu packages have a different executable name - DATEDIFF=`which dateutils.ddiff` -fi [ -e $TMP_FILE ] && rm -f $TMP_FILE @@ -50,6 +45,20 @@ function calc_bytes { esac } +function calc_hours_diff { + local start_ts="$1" + local end_ts="$2" + + python3 - "$start_ts" "$end_ts" <<'PY' +from datetime import datetime +import sys + +start = datetime.strptime(sys.argv[1], "%Y-%m-%d %H:%M:%S") +end = datetime.strptime(sys.argv[2], "%Y-%m-%d %H:%M:%S") +print(int((end - start).total_seconds() // 3600)) +PY +} + function writeDefinitionsToMetrics() { #print the definition of the metrics echo "# HELP borg_hours_from_last_archive How many hours have passed since the last archive was added to the repo (counted by borg_exporter.sh)" >> $TMP_FILE @@ -101,7 +110,7 @@ function getBorgDataForRepository { if [ -n "${LAST_ARCHIVE}" ]; then LAST_ARCHIVE_DATE=$(echo $LAST_ARCHIVE | awk '{print $3" "$4}') CURRENT_DATE="$(date '+%Y-%m-%d %H:%M:%S')" - NB_HOUR_FROM_LAST_BCK=$($DATEDIFF "$LAST_ARCHIVE_DATE" "$CURRENT_DATE" -f '%H') + NB_HOUR_FROM_LAST_BCK="$(calc_hours_diff "$LAST_ARCHIVE_DATE" "$CURRENT_DATE")" # in case the date parsing from BORG didn't work (e.g. archive with space in it), datediff will output # a usage message on stdout and will break prometheus formatting. We need to