您的位置:首页 > 运维架构 > Shell

监视磁盘使用情况的Shell脚本(本地+远程)

2013-01-23 00:00 741 查看
monitordisk.sh如下
#!/bin/bash 
#Updated:2008-03-03 PM By:leif(liangliwen@163.com) 
EMAIL=/usr/local/bin/email 
/bin/df -h >/tmp/df.txt 

USE=`df -H | grep -o [0-9]*% | grep -o ‘[0-9]\+'` 

for i in $USE 
do 
if (( $i > 95 )) 
then 
$EAMIL -s “WARNING Low disk space for $i” liangliwen@163.com break 
fi 
if (( $i > 90 )) 
then 
$EMAIL -s “Low disk space for $i” liangliwen@163.com fi 
done 

/bin/rm -f /tmp/df.txt

实现目的,任何一个分区使用到90%就发送一个邮件给指定的收件人,到95%就在邮件主题出警告(warning),说明发送邮件程序EMAIL,是从http://www.cleancode.org/projects/email 下载安装,比较灵活.

把这个shell根据需要放在crontab 实现定时检查磁盘情况

以下是补充内容:

用于监视远程主机磁盘使用情况的shell脚本,文件名:disklog.sh
#!/bin/bash 
# 文件名:disklog.sh 
# 用途:监视远程系统的磁盘使用情况 
logfile="diskusage.log" 
if [[ -n $1 ]] 
then 
logfile=$1 
if 
if [ ! -e $logfile ] 
then 
printf "%-8s %-14s %-9s %-8s %-6s %-6s %-6s %s\n" "Date" "IP ADDRESS" "Device" "Capacity" "Used" "Free" "Percent" "Status" > $logfile 
fi 

IP_LIST="127.0.0.1 0.0.0.0" 
# 提供远程主机IP地址列表 
( 
for ip in $IP_LIST 
do 
ssh slynux@$ip 'df -H' | grep ^/dev/ > /tmp/$$.df 

while read line; 
do 
cur_date=$(date +%D) 
printf "%-8s %-14s " $cur_date $ip 
echo $line | awk '{ printf("%-9s %-8s %-6s %-6s %-8s", $1,$2,$3,$4,$5); }' 

pusg=$(echo $line | egrep -o "[0-9]+%") 
pusg=${pusg/\%/}; 
if [ $pusg -lt 80 ]; 
then 
echo SAFT 
else 
echo ALERT 
fi 
done< /tmp/$$.df 
done 
)>>$logfile

我们可以用cron以固定的间隔来调度脚本执行,例如在crontab中加入如此条目,以实现每天上午10点自动运行脚本:
00 10 * * * /home/sh/disklog.sh /home/log/diskusg.log

执行crontab -e命令,添加上面一行内容并保存。

也可以手动执行:

$ ./disklog.sh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: