45 lines
1.7 KiB
Bash
45 lines
1.7 KiB
Bash
#!/usr/bin/env bash
|
|
# Collects ZFS pool utilization and boot partition usage for Prometheus textfile collector
|
|
set -euo pipefail
|
|
|
|
TEXTFILE="${TEXTFILE:?TEXTFILE env required}"
|
|
TMP="${TEXTFILE}.$$"
|
|
|
|
{
|
|
echo '# HELP zpool_size_bytes Total size of ZFS pool in bytes'
|
|
echo '# TYPE zpool_size_bytes gauge'
|
|
echo '# HELP zpool_used_bytes Used space in ZFS pool in bytes'
|
|
echo '# TYPE zpool_used_bytes gauge'
|
|
echo '# HELP zpool_free_bytes Free space in ZFS pool in bytes'
|
|
echo '# TYPE zpool_free_bytes gauge'
|
|
|
|
# -Hp: scripting mode, parseable, bytes
|
|
zpool list -Hp -o name,size,alloc,free | while IFS=$'\t' read -r name size alloc free; do
|
|
echo "zpool_size_bytes{pool=\"${name}\"} ${size}"
|
|
echo "zpool_used_bytes{pool=\"${name}\"} ${alloc}"
|
|
echo "zpool_free_bytes{pool=\"${name}\"} ${free}"
|
|
done
|
|
|
|
echo '# HELP partition_size_bytes Total size of partition in bytes'
|
|
echo '# TYPE partition_size_bytes gauge'
|
|
echo '# HELP partition_used_bytes Used space on partition in bytes'
|
|
echo '# TYPE partition_used_bytes gauge'
|
|
echo '# HELP partition_free_bytes Free space on partition in bytes'
|
|
echo '# TYPE partition_free_bytes gauge'
|
|
|
|
# Boot drive partitions: /boot (ESP), /persistent, /nix
|
|
# Use df with 1K blocks and convert to bytes
|
|
for mount in /boot /persistent /nix; do
|
|
if mountpoint -q "$mount" 2>/dev/null; then
|
|
read -r size used avail _ <<< "$(df -k --output=size,used,avail "$mount" | tail -1)"
|
|
size_b=$((size * 1024))
|
|
used_b=$((used * 1024))
|
|
avail_b=$((avail * 1024))
|
|
echo "partition_size_bytes{mount=\"${mount}\"} ${size_b}"
|
|
echo "partition_used_bytes{mount=\"${mount}\"} ${used_b}"
|
|
echo "partition_free_bytes{mount=\"${mount}\"} ${avail_b}"
|
|
fi
|
|
done
|
|
} > "$TMP"
|
|
mv "$TMP" "$TEXTFILE"
|