Linux檢查磁碟空間並發送郵件警示
目的:建立HdSpaceAlert.sh,並設定排程每日自動執行HdSpaceAlert.sh,檢查Linux各FileSystem硬碟剩餘空間,如任一FileSystem之空間小於90%,便發送郵件通知。
HdSpaceAlert.sh
#郵件通知地址 ADMIN="adams@xxx.xxx.xxx" # 設定空間使用達??%便通知,以下設90% ALERT=90 #定出不需要檢查的FileSystem,中間用「|」隔開,以下為不檢查/dev/sda1及/dev/sda2 ExceptionalPartition="/dev/sda1|/dev/sda2"
#因df -H第一列是TITLE,所以awk完後使用grep -v 'Use%'過濾掉第一列TITLE列 df -H | grep -vE $ExceptionalPartition | awk '{ print $5 " " $1 }' | grep -v 'Use%' | while read output; do usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge $ALERT ]; then echo -ne "SOS! SOS! SOS! \n out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Warning: out of disk space $usep %" $ADMIN fi done |