功能:

如果系统中的某个分区使用率Inodes使用率 一旦 大于或等于90%时,则发出邮件告警信息给管理员,通知管理员采取相关措施避免分区爆满。

把脚本设置成crond任务使其周期往复执行

脚本内容:
#!/bin/bash
#Author:kashu
#Date:2013-07-26
#Description:Monitoringharddiskspaceusage.

#checkifthemailisthere
if [!-x/bin/mail ]; then
yum-yinstallmailx|| { echo "mailxinstallationfailed!"; exit1; }
fi

log =/tmp/LackOfAvailableSpace.txt
> $log
private_ip = "$(ip-4-oadsh`ipro|awk'/default/{print$NF}'`|awk'{print$4}')"
#ifyouwanttousethefollowingcommands,makesurethatthecurlisinstalledcorrectly.
#public_ip="`curl-sifconfig.me2>/dev/null`"
#You'dbetterputyourpublicipaddresshere.
public_ip =61.181.xx.xx
hostname = `uname-n `
#PutyourpersonalE-mailaddresshere.
email_addr =xxx@163.com
#Checkdiskspaceusage
forIin `df|awk '{print$(5)*1}'|sort-nu `; do
if [ "$I"-gt "89" ]; then
#Ifthecommandbelowexecutionfailed,itmeansthat/tmppartitionisalreadyfull!
#Soyouwillnotbeabletowriteanydatato/tmpfolder.
if! echo-e "Hostname:$hostname\nPrivateIP:$private_ip\nPublicIP:$public_ip\n">> "$log"; then
echo-e 'Ohmygosh!\nPartitionisfull!\nTakesomeactionassoonaspossible!\n'| \
mail-s "WARNING,SpaceisFULL.PrivateIP:$private_ipHostname:$hostnamePublicIP:$public_ip" "$email_addr"
fi
df-hT>> "$log";date>> "$log";mail-s 'WARNING,FreeSpaceistooless!' "$email_addr"< "$log" && break
fi
done

#Checkinodesusage
foriin `df-i|awk '{print$(NF-1)*1}'|sort-nu `; do
if [ "$i"-gt "89" ]; then
echo-e "Hostname:$hostname\nPrivateIP:$private_ip\nPublicIP:$public_ip\n">> "$log"
df-i>> "$log";date>> "$log";mail-s 'WARNING,HavingtoofewInodes!' "$email_addr"< "$log" && break
fi
done

脚本测试结果:
分区使用率的监控脚本 - 卡书 - Self-Study
测试时,可以把脚本中分区使用率的那个对比数值调小(我测试时把它调整到14了,因此,只要使用率大于或等于15时,就会发告警邮件了)。看上图,这是实际收到的邮件,可以在邮件中看到这么一些信息(信息量的多少可以在脚本中自己增减),这样一看就知道哪个分区快满了,以及去哪台服务器上采取相关措施。
分区使用率的监控脚本 - 卡书 - Self-Study
上图是当/tmp所在的分区满了时,发出的邮件告警信息。