Files
homelab-monitoring/scripts/check-all-vm-disks.sh

38 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Check disk usage on all VMs via SSH
set -euo pipefail
SEND_NTFY="/usr/local/bin/send-ntfy.sh"
# VM configurations: "VMID:NAME:IP"
# Note: VM 100 (haos14.0) excluded - Home Assistant OS doesn't have SSH, monitored via thin pool instead
VMS=(
"101:debianvm:DEBIANVM"
"282:ubuntu-server-xfce:ubuntu-server-xfce"
)
for vm_config in "${VMS[@]}"; do
IFS=':' read -r VMID NAME HOST <<< "$vm_config"
# Try to SSH and get disk usage
DISK_INFO=$(timeout 10 sshpass -p 'admin' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@$HOST "df -h / 2>/dev/null | tail -1" 2>/dev/null || echo "FAILED")
if [ "$DISK_INFO" = "FAILED" ]; then
$SEND_NTFY warning "VM Disk Check Failed" "🟡 WARNING: Cannot check disk on $NAME (VMID $VMID) - SSH failed" "warning,computer"
continue
fi
USAGE=$(echo "$DISK_INFO" | awk '{print $5}' | sed 's/%//')
USED=$(echo "$DISK_INFO" | awk '{print $3}')
TOTAL=$(echo "$DISK_INFO" | awk '{print $2}')
FREE=$(echo "$DISK_INFO" | awk '{print $4}')
if [ "$USAGE" -gt 90 ]; then
$SEND_NTFY critical "VM Disk Critical" "🔴 CRITICAL: $NAME (VMID $VMID) root partition at ${USAGE}%\nUsed: $USED/$TOTAL, Free: $FREE" "cd,skull,computer"
elif [ "$USAGE" -gt 80 ]; then
$SEND_NTFY warning "VM Disk Warning" "🟡 WARNING: $NAME (VMID $VMID) root partition at ${USAGE}%\nUsed: $USED/$TOTAL, Free: $FREE" "cd,warning,computer"
fi
logger -t vm-disk-monitor "$NAME (VMID $VMID): ${USAGE}%"
done