#!/usr/bin/env bash
# disk_usage_check.sh — disk usage and inode usage per mountpoint.
set -euo pipefail

MOUNTS=$(df -k 2>/dev/null | awk 'NR>1 && $1 ~ /^\// && $NF !~ /^\/(snap|dev|proc|sys|run|private)/ {print $NF}' || true)
FIRST=1

echo -n '{"health":"healthy","metrics":{'
for m in $MOUNTS; do
  LINE=$(df -k "$m" 2>/dev/null | awk 'NR==2 {print $3, $4, $5, $7}' | tr -d '%' || true)
  if [ -z "$LINE" ]; then continue; fi
  USED=$(echo "$LINE" | awk '{print $1}')
  FREE=$(echo "$LINE" | awk '{print $2}')
  PCT=$(echo "$LINE" | awk '{print $3}')

  INODE_LINE=$(df -i "$m" 2>/dev/null | awk 'NR==2 {print $5}' | tr -d '%' || echo "0")
  INODE_PCT=$(echo "${INODE_LINE:-0}" | sed 's/-/0/')

  KEY=$(echo "$m" | sed 's|/|_|g' | sed 's|^_||')
  [ -z "$KEY" ] && KEY="root"

  if [ $FIRST -eq 0 ]; then echo -n ','; fi
  FIRST=0
  echo -n "\"disk_${KEY}_pct\":$PCT,\"inode_${KEY}_pct\":$INODE_PCT"
done
echo '}}'
