#!/bin/bash

# 磁盘监控脚本
# 使用方法: ./disk-monitor.sh [threshold]

THRESHOLD=${1:-85}  # 默认阈值 85%
INVENTORY_FILE="configuration/inventories/production/nomad-cluster.ini"

echo "🔍 开始磁盘空间监控 (阈值: ${THRESHOLD}%)"
echo "=================================="

# 运行磁盘分析
echo "📊 运行磁盘分析..."
ansible-playbook -i "$INVENTORY_FILE" configuration/playbooks/disk-analysis-ncdu.yml

echo ""
echo "⚠️  检查高磁盘使用率节点..."

# 检查所有节点的磁盘使用情况
ansible all -i "$INVENTORY_FILE" -m shell -a "df -h | awk 'NR>1 {gsub(/%/, \"\", \$5); if(\$5 > $THRESHOLD) print \$0}'" | while read line; do
    if [[ $line == *"=>"* ]]; then
        echo "🚨 节点: $line"
    elif [[ $line =~ ^/dev ]]; then
        echo "   高使用率磁盘: $line"
    fi
done

echo ""
echo "💡 如需清理，运行:"
echo "   ansible-playbook -i $INVENTORY_FILE configuration/playbooks/disk-cleanup.yml"
echo ""
echo "📁 详细报告位置: /tmp/disk-analysis/"
echo "   使用 ncdu -f /tmp/disk-analysis/ncdu-root-<hostname>.json 查看详细信息"