fix borg_exporter datediff error becuase alpine not have it, changed to python
Build and Push Docker Container / build-and-push (push) Waiting to run

This commit is contained in:
2026-04-12 00:43:19 +02:00
parent 1da0105b61
commit 04cd1a4ee3
+15 -6
View File
@@ -3,11 +3,6 @@ source /etc/borg_exporter.rc
source /variables.sh source /variables.sh
TMP_FILE="/tmp/prometheus-borg" 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 [ -e $TMP_FILE ] && rm -f $TMP_FILE
@@ -50,6 +45,20 @@ function calc_bytes {
esac 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() { function writeDefinitionsToMetrics() {
#print the definition of the metrics #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 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 if [ -n "${LAST_ARCHIVE}" ]; then
LAST_ARCHIVE_DATE=$(echo $LAST_ARCHIVE | awk '{print $3" "$4}') LAST_ARCHIVE_DATE=$(echo $LAST_ARCHIVE | awk '{print $3" "$4}')
CURRENT_DATE="$(date '+%Y-%m-%d %H:%M:%S')" 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 # 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 # a usage message on stdout and will break prometheus formatting. We need to