shell編程磁盤監控(評估可用空間耗盡時間,郵件通知管理員)
設定監控磁盤為/dev/md0;如果文件系統使用率大於90%,則會依次調用這3個函數估算文件系統耗盡時間,並將結果郵件發給管理員。
#!/bin/bash
##This script is used to monitor disk.
#6/20/11
#定義函數計算文件系統的寫入速度
function disk_speed()
{
A=`df -k | grep "$1" | awk '{print $3}'`
sleep 30
B=`df -k | grep "$1" | awk '{print $3}'`
D=`expr $B - $A`
SPEED=`expr $D / 30`
echo $SPEED
unset A B D SPEED
}
#定義估計磁盤空間耗盡時間
function avai_time()
{
FREE=`df -k | grep "$1" | awk '{print $4}'`
if TIME=`expr $FREE / $2`
then
if [ $TIME -ge 60 ]
then
A=`expr $TIME / 60`
echo $A" minutes"
unset FREE TIME
return 0
else
echo "Unknow"
unset FREE TIME
return 0
fi
else
echo "Unknow"
unset FREE TIME
return 1
fi
}
#定義設置郵件內容函數
function content()
{
TEMP_FILE=/root/disk_monitor.tmp
echo "Warning disk:"$1 >$TEMP_FILE
echo "Used space:"$2 >>$TEMP_FILE
echo "Available space:"$3 >>$TEMP_FILE
echo "Use%:"$4"%" >>$TEMP_FILE
echo "Write speed:"$5 >>$TEMP_FILE
echo "Estimated time remaining:"$6 >>$TEMP_FILE
echo "Now time:"`date +"%Y-%m-%d %H:%M"` >>$TEMP_FILE
unset TEMP_FILE
return 0
}
#定義要監控的文件系統
MON_DISK=/dev/md0
USED=`df -h | grep "$MON_DISK" | awk '{print $5}' | sed 's/%//'`
#判斷使用率是否大於90%,如果是,則執行then後面的語句
if [ $USED -ge 90 ]
then
USED_SPACE=`df -h | grep "$MON_DISK" | awk '{print $4}'`
AVAI_SPACE=`df -h | grep "$MON_DISK" | awk '{print $3}'`
W_SPEED=`disk_speed $MON_DISK`
if [ $W_SPEED != 0 ]
then
S_TIME=`avai_time $MON_DISK $W_SPEED`
else
S_TIME="Unknow"
fi
content $MON_DISK $USED_SPACE $AVAI_SPACE $USED $W_SPEED $S_TIME
mail -s "Disk warning" root </root/disk_monitor.tmp
rm -rf /root/disk_monitor.tmp
unset MON_DISK USED USED_SPACE AVAI_SPACE W_SPEED S_TIME
fi