Use jq to filter output

This commit is contained in:
phil 2023-12-02 11:45:43 +01:00
parent 8404625755
commit f1972140ff

View file

@ -9,46 +9,17 @@ PROM_FILE=$TEXTFILE_COLLECTOR_DIR/backup.prom
TMP_FILE=$PROM_FILE.$$
HOSTNAME=$(hostname)
BORG_INFO=$(borgmatic --no-color info --last 1 ) || mv $TMP_FILE $PROM_FILE
echo "backup_last_run{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | grep "Time (start)" | awk '{print $4, $5}' | xargs -i date -d "{}" +"%s")" >> $TMP_FILE
echo "backup_files{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | grep "Number of files" | awk '{print $4}')" >> $TMP_FILE
echo "backup_chunks_unique{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | grep "Chunk index" | awk '{print $3}')" >> $TMP_FILE
echo "backup_chunks_total{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | grep "Chunk index" | awk '{print $4}')" >> $TMP_FILE
BORG_INFO=$(borgmatic --no-color info --last 1 --json ) || mv $TMP_FILE $PROM_FILE
echo "backup_last_run{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r .[].repository.last_modified)" >> $TMP_FILE
echo "backup_files{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.nfiles')" >> $TMP_FILE
echo "backup_chunks_unique{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.total_unique_chunks')" >> $TMP_FILE
echo "backup_chunks_total{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.total_chunks')" >> $TMP_FILE
function calc_bytes {
NUM=$1
UNIT=$2
case "$UNIT" in
kB)
echo $NUM | awk '{ print $1 * 1024 }'
;;
MB)
echo $NUM | awk '{ print $1 * 1024 * 1024 }'
;;
GB)
echo $NUM | awk '{ print $1 * 1024 * 1024 * 1024 }'
;;
TB)
echo $NUM | awk '{ print $1 * 1024 * 1024 * 1024 * 1024 }'
;;
esac
}
# byte size
LAST_SIZE=$(calc_bytes $(echo "$BORG_INFO" |grep "This archive" |awk '{print $3}') $(echo "$BORG_INFO" |grep "This archive" |awk '{print $4}'))
LAST_SIZE_COMPRESSED=$(calc_bytes $(echo "$BORG_INFO" |grep "This archive" |awk '{print $5}') $(echo "$BORG_INFO" |grep "This archive" |awk '{print $6}'))
LAST_SIZE_DEDUP=$(calc_bytes $(echo "$BORG_INFO" |grep "This archive" |awk '{print $7}') $(echo "$BORG_INFO" |grep "This archive" |awk '{print $8}'))
TOTAL_SIZE=$(calc_bytes $(echo "$BORG_INFO" |grep "All archives" |awk '{print $3}') $(echo "$BORG_INFO" |grep "All archives" |awk '{print $4}'))
TOTAL_SIZE_COMPRESSED=$(calc_bytes $(echo "$BORG_INFO" |grep "All archives" |awk '{print $5}') $(echo "$BORG_INFO" |grep "All archives" |awk '{print $6}'))
TOTAL_SIZE_DEDUP=$(calc_bytes $(echo "$BORG_INFO" |grep "All archives" |awk '{print $7}') $(echo "$BORG_INFO" |grep "All archives" |awk '{print $8}'))
echo "backup_last_size{host=\"${HOSTNAME}\"} $LAST_SIZE" >> $TMP_FILE
echo "backup_last_size_compressed{host=\"${HOSTNAME}\"} $LAST_SIZE_COMPRESSED" >> $TMP_FILE
echo "backup_last_size_dedup{host=\"${HOSTNAME}\"} $LAST_SIZE_DEDUP" >> $TMP_FILE
echo "backup_total_size{host=\"${HOSTNAME}\"} $TOTAL_SIZE" >> $TMP_FILE
echo "backup_total_size_compressed{host=\"${HOSTNAME}\"} $TOTAL_SIZE_COMPRESSED" >> $TMP_FILE
echo "backup_total_size_dedup{host=\"${HOSTNAME}\"} $TOTAL_SIZE_DEDUP" >> $TMP_FILE
echo "backup_last_size{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.original_size')" >> $TMP_FILE
echo "backup_last_size_compressed{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.compressed_size')" >> $TMP_FILE
echo "backup_last_size_dedup{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[].archives | .[] | .stats.deduplicated_size')" >> $TMP_FILE
echo "backup_total_size{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" |j q -r '.[] | .cache.stats.total_size')" >> $TMP_FILE
echo "backup_total_size_compressed{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.total_csize')" >> $TMP_FILE
echo "backup_total_size_dedup{host=\"${HOSTNAME}\"} $(echo "$BORG_INFO" | jq -r '.[] | .cache.stats.unique_csize')" >> $TMP_FILE
mv $TMP_FILE $PROM_FILE