#!/bin/bash
# 磁盘空间告警脚本
# 用法: ./disk_alert.sh
THRESHOLD=80
EMAIL="admin@example.com"
PARTITION="/"
DISK_USAGE=$(df -h "$PARTITION" | awk 'NR==2 {print $5}' | cut -d'%' -f1)
if [ "$DISK_USAGE" -gt "$THRESHOLD" ]; then
MSG="[告警] 磁盘使用率达到 ${DISK_USAGE}%!分区: $PARTITION"
echo "$MSG"
# 方式1: 邮件告警(需配置 mail / postfix)
echo "$MSG" | mail -s "磁盘空间告警 - $(hostname)" "$EMAIL"
# 方式2: 钉钉 Webhook(推荐)
# curl -X POST "https://oapi.dingtalk.com/robot/send?access_token=xxx" \
# -H 'Content-Type: application/json' \
# -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"$MSG\"}}"
fi