码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2017-02-05 22:47:03      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:固定   bsp   email   命令   主题   收件人   free   device   定时   

任何一个分区使用到90%就发送一个邮件给指定的收件人,到95%就在邮件主题出警告(warning),说明发送邮件程序EMAIL

#!/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

 

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

标签:固定   bsp   email   命令   主题   收件人   free   device   定时   

原文地址:http://www.cnblogs.com/peter316/p/6368670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!