neo  2010-8-25  qq:276109493

监控服务器存活状态及丢包率

脚本功能:

1.用ping命令探测服务器是否存活, 如果ping不到主机,则发送邮件给MIS

2.如果ping测试主机正常则探测丢包率,当丢包率大于/等于50%,发送邮件给MIS

3.在crontab中设置每5分钟运行一次

脚本如下:

#!/bin/bash
export LC_ALL=C
server_all_list=(10.13.0.1:link 10.13.0.2:fs 10.13.0.3:mail 10.13.0.33:freepbx \ 10.13.0.15:mantis 10.13.0.17:dop 10.13.0.8:cvs 10.13.0.20:svn 10.13.0.11:gaia \ 202.99.96.68:cnc)
date=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")
server_all_len=${#server_all_list[*]}
packet_loss="is packet loss 50%"
host_down="is down"
function send_mail() {
        sendEmail  -f [email protected] -t [email protected] \
                   -s mail.aaa.com.cn -u $@ \
                   -xu neo -xp neo1234
}

i=0
while [ $i -lt $server_all_len ]; do
        server_ip=$(echo ${server_all_list[$i]} | awk -F ':' '{print $1}')
        server_host=$(echo ${server_all_list[$i]} | awk -F ':' '{print $2}')

        if ping -c 4 $server_ip | sed -n "7,9"p > bb.txt; then
                if grep $server_ip bb.txt; then
                        if [ `cat bb.txt | sed -n "2"p | awk '{print $6}' |
                             sed 's/%//g'` -ge 50 ]; then
                                echo $date $server_host:$server_ip is packet loss 50% | send_mail $server_host $server_ip $packet_loss
                        fi
                else
                echo $date $server_host:$server_ip is down | send_mail $server_host $server_ip $host_down
                fi
        fi

        let i++
done