Chinaunix首页 | 论坛 | 博客
  • 博客访问: 124530
  • 博文数量: 69
  • 博客积分: 1550
  • 博客等级: 上尉
  • 技术积分: 676
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-22 11:13
文章分类

全部博文(69)

文章存档

2011年(1)

2010年(5)

2008年(4)

2007年(46)

2006年(13)

我的朋友

分类: LINUX

2007-03-01 13:27:38

脚本实现检查磁盘空间并发送邮件通知

1。配置ssh
在本地主机运行ssh-keygen -t rsa 生成公钥id_rsa.pub、秘钥id_rsa 
将/root/.ssh/id_rsa.pub拷贝至被监控主机/root/.ssh/下,并重命名为authorized_keys
这样配置,可以在本地直接ssh至远程主机执行命令
ssh 192.168.3.51 du -lh
2. 建立Checksitelist.conf,将所有检查主机IP地址列表
   192.168.3.41
   192.168.3.51
3。自动发送mail脚本:mailto.sh
    #!/bin/sh
function echo_help(){
echo "Usage:"
echo "sh mailto.sh -to mailaddr -file messagefilename [-from frommailaddr] [-subject mailsubject] [-fname showname] [-tname toshowname]"
}
email=""
emailname="Test Mail"
messagebody="tmp.tmp"
from=""
fromname="MailReport"
subject="Mail Report"
until [ $# -eq 0 ]
do
tmpV=$1
if [ $tmpV = "-from" ] ; then
shift
tmpV=$1
from=$tmpV
elif [ $tmpV = "-to" ] ; then
shift
tmpV=$1
email=$tmpV
elif [ $tmpV = "-subject" ] ; then
shift
tmpV=$1
subject=$tmpV
elif [ $tmpV = "-file" ] ; then
shift
tmpV=$1
messagebody=$tmpV
elif [ $tmpV = "-fname" ] ; then
shift
tmpV=$1
fromname=$tmpV
elif [ $tmpV = "-tname" ] ; then
shift
tmpV=$1
emailname=$tmpV
elif [ $tmpV = "--help" -o $tmpV = "-h" ] ; then
echo_help
exit 1
fi
shift
done
if ! test -f ${messagebody} ; then
echo "${messagebody} not exists!"
echo_help
exit 2
fi
echo "begin send..."
echo -e "To: \"${emailname}\" <${email}>\nFrom: \"${fromname}\" <${from}>\nSubject: ${subject}\n\n`cat ${messagebody}`" | /usr/sbin/sendmail -t
echo "send OK."
4.磁盘空间检查脚本:checkdisk.sh
  conffile=/usr/local/sbin/hdspacecheck/CheckSiteList.conf
date +%Y_%m_%d_%H:%M:%S > /usr/local/sbin/hdspacecheck/today_space.txt ;
echo check disk space result >> /usr/local/sbin/hdspacecheck/today_space.txt ;
for site in `cat $conffile|grep -v ^#`
do
echo -n "test ping $site ...."
ping -c 3 -q -n $site > /dev/null 2> /dev/null
if [ $? -ne 0 ]
   then
       echo fail
   else
       echo ok \;
       echo check disk space start...
       ssh $site ifconfig | awk '$1=="inet" {print $2}' | grep 192  >> /usr/local/sbin/hdspacecheck/today_space.txt
       echo "--------------------------------------------------------" >> /usr/local/sbin/hdspacecheck/today_space.txt
       ssh $site df -lh >> /usr/local/sbin/hdspacecheck/today_space.txt ;
       echo "========================================================" >> /usr/local/sbin/hdspacecheck/today_space.txt ;
       echo check disk space finished.
fi
done
sh /usr/local/sbin/hdspacecheck/mailto.sh  -to -file /usr/local/sbin/hdspacecheck/today_space.txt -from -subject diskspace ;
sh /usr/local/sbin/hdspacecheck/mailto.sh  -to -file /usr/local/sbin/hdspacecheck/today_space.txt -from -subject diskspace ;
阅读(1514) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~