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 ;