由於有些業務伺服器託管在了二線城市的機房,第4天同事說伺服器網路不正常是不是頻寬沒有給夠,我們租用的是5M獨享頻寬。我登陸伺服器,我們租用的是網通線路,就找了一163源下載 Centos 系統 檢查了一下下載速度,下載速度居然在 250KB/S,也就是說實際頻寬也就給了2M·拿起手機給IDC打電話,他們說讓技術查一下,隨後給我回電話說,不好意思某某個技術調交換機的時候調錯了,然後我就怒了,你們什麼職業道德。
隨後我想了想,就寫了這個指令碼·檢查下載速度,當然在網站流量大的時候速度會收到影響,你也可以設定nagios 晚上執行這個指令碼。或者把閥值調相應低點。下面我貼我的指令碼和使用方法。

#!/bin/bash
PROGNAME=`basename $0`
VERSION="Version 1.0"
AUTHOR="2010.11.17,www.nginxs.com"

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

interval=5
url="http://mirrors.163.com/centos/5.5/isos/x86_64/CentOS-5.5-x86_64-LiveCD.iso"

print_version() {
        echo "$VERSION $AUTHOR"
}

print_help() {
        print_version $PROGNAME $VERSION
        echo "$PROGNAME is a Nagios plugin to monitor download speed"
        echo "Use of wget download url file"
        echo "When using optional warning/critical thresholds all values except"
        echo "Usage parameters:"
        echo ""
        echo "$PROGNAME [-i/--interval] [-u|--url] [-w/--warning] [-c/--critical]"
        echo ""
        echo "Options:"
                echo "  --interval|-i)"
                echo "    Defines the download file times"
                echo "          propose set < 5 second and  > 10 second"
                echo "    Default is: 5 second"
                echo ""
                echo "  --url|-u)"
                echo "    Sets url page"
                echo "    Defautl is :http://mirrors.163.com/centos/5.5/isos/x86_64/CentOS-5.5-x86_64-LiveCD.iso"
                echo "          Please set Fastest url"
                echo ""
                echo "  --warning|-w)"
                echo "          Sets a warning level for download speed. Defautl                                                                                                  is: off"
                echo ""
                echo "  --critical|-c)"
                echo "          Sets a critical level for download speed. Defaut                                                                                                 l is: off"
        exit $ST_UK
}

while test -n "$1";do
        case "$1" in
                --help|-h)
                        print_help
                        exit $ST_UK
                        ;;
                --url|-u)
                        url=$2
                        shift
                        ;;
                --interval|-i)
                        interval=$2
                        shift
                        ;;
                --warning|-w)
                        warn=$2
                        shift
                        ;;
                --critical|-c)
                        crit=$2
                        shift
                        ;;
                *)
                        echo "Unknown argument: $1"
                        print_help
                        exit $ST_UK
                        ;;
        esac
        shift
done

val_wcdiff() {
    if [ ${warn} -lt ${crit} ]
    then
        wcdiff=1
    fi
}

get_speed() {
        wget -b $url > /dev/null
        sleep $interval
        BS="`cat wget-log |tail -n20 |awk `{print $8}`|sed `s/K//`|awk `{sum+=$1                                                                                                 };END{print sum}``"
        speed=`echo $BS / 19|bc`
        killall wget
        rm CentOS*
        rm wget-log
}
do_output() {
        output="speed:${speed}"
}
do_perfdata() {
        perfdata="`speed`=${speed}"
}

if [ -n "$warn" -a -n "$crit" ]
then
    val_wcdiff
    if [ "$wcdiff" = 1 ];then
        echo "Please adjust your warning/critical thresholds. The critical must                                                                                                  be lower than the warning level!"
        exit $ST_UK
    fi
fi

get_speed
do_output
do_perfdata

if [ -n "$warn" -a -n "$crit" ];then
        if [ $speed -le $warn -a $speed -gt $crit ];then
                echo  "WARNING - $output |$perfdata"
                exit $ST_WR
        elif [ $speed -lt $crit ];then
                echo  "CRITICAL - $output|$perfdata"
                exit $ST_CR
        else
                echo  "OK - $output|$perfdata"
                exit $ST_OK
        fi
else
        echo "OK - $output|$perfdata"
        exit $ST_OK
fi

使用方法:

nagios $> ./check_speed.sh -h
Version 1.0 2010.11.17,www.nginxs.com
check_speed.sh is a Nagios plugin to monitor download speed
Use of wget download url file
When using optional warning/critical thresholds all values except
Usage parameters:

check_speed.sh [-i/--interval] [-u|--url] [-w/--warning] [-c/--critical]

Options:
  --interval|-i)
          Defines the download file times
          propose set < 5 second and  > 10 second
          Default is: 5 second

  --url|-u)
          Sets url page
          Defautl is :http://mirrors.163.com/centos/5.5/isos/x86_64/CentOS-5.5-x86_64-LiveCD.iso
          Please set Fastest url

  --warning|-w)
          Sets a warning level for download speed. Defautl is: off

  --critical|-c)
          Sets a critical level for download speed. Defautl is: off

由於我的伺服器頻寬5M

nagios $> ./check_speed.sh -i 8 -u http://mirrors.163.com/centos/5.5/isos/x86_64/CentOS-5.5-x86_64-LiveCD.iso -w 400 -c 300
OK - speed:556|`speed`=556

附件:check_speed.sh