首先看一下,我的磁盘使用情况:
[root@sjtj jiaoben]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/hda3 9.5G 5.7G 3.4G 64% /
/dev/hda5 7.9G 147M 7.4G 2% /backup
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdb1 99M 24M 70M 26% /opt/lampp/htdocs/sjtj
/dev/mapper/VolGroup01-LogVol00
73G 36G 34G 52% /ftp
文件系统 容量 已用 可用 已用% 挂载点
/dev/hda3 9.5G 5.7G 3.4G 64% /
/dev/hda5 7.9G 147M 7.4G 2% /backup
tmpfs 252M 0 252M 0% /dev/shm
/dev/hdb1 99M 24M 70M 26% /opt/lampp/htdocs/sjtj
/dev/mapper/VolGroup01-LogVol00
73G 36G 34G 52% /ftp
下面写个脚本,来监控磁盘空间,当超过一定大小之后,可以给我们发邮件,可以做个cron任务,这样就不用我们手动来执行。
#!/bin/bash
# This script does a very simple test for checking disk space.
space=`df -h | awk '{print $5}' | grep % | grep -v 已用 | sort -n | tail -1 | cut -d "%" -f1 -`
alertvalue="80"
if [ "$space" -ge "$alertvalue" ]; then
echo "At least one of my disks is nearly full!" | mail -s "daily diskcheck" root
else
echo "Disk space normal" | mail -s "daily diskcheck" root
fi
# This script does a very simple test for checking disk space.
space=`df -h | awk '{print $5}' | grep % | grep -v 已用 | sort -n | tail -1 | cut -d "%" -f1 -`
alertvalue="80"
if [ "$space" -ge "$alertvalue" ]; then
echo "At least one of my disks is nearly full!" | mail -s "daily diskcheck" root
else
echo "Disk space normal" | mail -s "daily diskcheck" root
fi
这样之后,会发一封邮件到我们的root账号里。
查看 # cat /var/spool/mail/root
如果,要发到邮箱里,可以把root改为邮件地址,这样,就可以发消息到我们的邮箱里了。
如果无法发送邮件,则应该是sendmail没有启动,启动即可
service sendmail start
使用case语句,嵌套 if 语句可能比较美观,但是只要你面临可能采取的一系列的不同动作时,你可能会迷惑。要处理复杂条件时,使用 case 语法:
#!/bin/bash
# This script does a very simple test for checking disk space.
space=`df -h | awk '{print $5}' | grep % | grep -v 已用 | sort -n | tail -1 | cut -d "%" -f1 -`
case $space in
[1-6]*)
Message="All is quiet."
;;
[7-8]*)
Message="Start thinking about cleaning out some stuff. There's a partition that is $space % full."
;;
9[1-8])
Message="Better hurry with that new disk... One partition is $space % full."
;;
99)
Message="I'm drowning here! There's a partition at $space %!"
;;
*)
Message="I seem to be running with an nonexitent amount of disk space..."
;;
esac
echo $Message | mail -s "disk report `date`" aa@aa.com
# This script does a very simple test for checking disk space.
space=`df -h | awk '{print $5}' | grep % | grep -v 已用 | sort -n | tail -1 | cut -d "%" -f1 -`
case $space in
[1-6]*)
Message="All is quiet."
;;
[7-8]*)
Message="Start thinking about cleaning out some stuff. There's a partition that is $space % full."
;;
9[1-8])
Message="Better hurry with that new disk... One partition is $space % full."
;;
99)
Message="I'm drowning here! There's a partition at $space %!"
;;
*)
Message="I seem to be running with an nonexitent amount of disk space..."
;;
esac
echo $Message | mail -s "disk report `date`" aa@aa.com
本文转自 zhangzj1030 51CTO博客,原文链接:http://blog.51cto.com/tech110/234757