以下是磁盘使用空间监控的脚本:但有两个前提:1.开启sendmail服务。2.vim /etc/mail.rc,新增一行:set smtp=smtp.server.com
使用crontab设定好每几分钟运行一次。当某partition使用率超过90%时,即可发邮件报警。
#!/bin/bash
partitions=$(df -h|awk '{print $5}'|grep -v 'Use%')
recipients=user1@contoso.com,user2@contoso.com
i=0
for partition in $partitions
do
i=$((i+1))
percent=$(echo $partition|tr -d '%')
if [ $percent -ge '90' ];then
showmenu=$(df -h|sed -n "1p")
showpartition=$(df -h|grep -v 'Use%'|sed -n "${i}p")
partitionname=$(df -h|grep -v 'Use%'|awk '{print $6}'|sed -n "${i}p")
echo -e "Attention! $HOSTNAME's $partitionname Disk Occupancy rate has exceeded 90%,please notes !!\n\n$showmenu\n$showpartition\n\n"|mail -s "$HOSTNAME Partition Usage Alert!!" $recipients
fi
done