shell监控多台主机
#!/bin/bashfor ip in `cat iplist.txt`;do
ping $ip -c1>/dev/null#先检查主机是否在线,如果在线则进行进一步监控
if [ $? -eq 0 ];then
rootused=``ssh $ip df -h|grep /$|awk '{print $4}'|cut -d% -f1` #取根目录使用情况
for hardid in ``ssh $ip df -h|grep "^\/dev\/s"|awk '{print $1}'`;do
hardused=``ssh $ip df -h|grep $hardid|awk '{print $5}'|cut -d% -f1`#取其它本地硬盘的使用情况
done
memtotal=`ssh $ip free -m|grep Mem|awk '{print $2}'`
memused=`ssh $ip free -m|grep Mem|awk '{print $3}'`
mem=`expr $memused \* 100 / $memtotal`
idelcpu=`ssh $ip top -n1|grep Cpu|awk '{print $5}'|cut -d"." -f1`
cpuused=`expr 100 - $idelcpu`
if [ $mem -gt 70 ];then #如果内存使用高于70%则邮件通知
echo "warm:$ip memory is $mem"|mutt -s "monitor report" 18507656225@163.com
fi
if [ $hardused -le 80];then #如果硬盘使用已高于80%则邮件通知
echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" 18507656225@163.com
fi
if [ $rootused -le 80];then #如果硬盘使用已高于80%则邮件通知
echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" 18507656225@163.com
fi
for hardid in `ssh $ip iostat |grep "^s"|awk '{print $1}'`;do #先取盘符
iostat=`ssh $ip iostat -x|grep $hardid|awk '{print $12}'|cut -d"." -f1` #取得io繁忙状态
echo $iostat
if [ $iostat -gt 80 ];then #如果io繁忙高于80%,则邮件通知
echo "ipaddress:$ip,hard:$hardid,iostat:$iostat"|mutt -s"warm" 18507656225@163.com
fi
done
else
echo "host:$ip is not alive"|mutt -s"monitor report" 18507656225@163.com#如果主机无法ping通则邮件通知
fi
done
页:
[1]