以下代码实现磁盘空间小于10G自动发送邮件
#!/bin/sh
#define alert devel of nagios
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKOWN=3
above=0
list=`df -h|grep -v "Filesystem"|grep -v "devfs"|awk '{print $6}'`
for disk_name in $list;do
url=`df -h|grep $disk_name`
size=`echo $url|awk -F\ '{print "size:" $2}'`;
used=`echo $url|awk -F\ '{print "used:" $3}'`;
avail=`echo $url|awk -F\ '{print "avail:" $4}'`;
capacity=`echo $url|awk -F\ '{print $5}'`;
mounted=`echo $url|awk -F\ '{print "mounted:" $6}'`;
percent=`echo $capacity |sed 's/%//g'`
#echo $percent
#echo -e $size $used $avail "capacity:"$capacity $mounted
# if [ "$percent" -ge 70 ];then
# above=1
# echo "the disk $mounted space used above 70%"
# echo $size $used $avail "capacity:"$capacity $mounted
# exit $STATE_WARNING
# else
# above=0
# fi
useage=`echo $avail |sed 's/avail://g'|sed 's/G//g'`
echo $useage
if [ "$useage" -le 10 ];then
above=1
echo $mounted
echo "the disk $mounted space used less 10G" $size $used $avail "capacity:"$capacity $mounted > warn.txt
#echo $size $used $avail "capacity:"$capacity $mounted
sh mail.sh
exit $STATE_WARNING
else
above=0
fi
done;
if [ "$above" -eq 0 ];then
echo "the disk used of space normal"
exit $STATE_OK
else
echo "the disk used state unknow"
exit $STATE_CRITICAL
fi
# !/bin/sh
from_name="from@zhl"
from="xxxxx@yyyy.com"
to="xxxxx@yyyy.com"
email_title="10.18.102.141 disk space less then 10G"
email_content="/home/work/watchdisk/warn.txt"
email_subject="10.18.102.141 disk space less then 10G"
echo -e "To: \"${email_title}\" <${to}>\nFrom: \"${from_name}\" <${from}>\nSubject: ${email_subject}\n\n`cat ${email_content}`" | /usr/sbin/sendmail -t