nagios监控带宽插件

简介:

由于有些业务服务器托管在了二线城市的机房,第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

本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/426369,如需转载请自行联系原作者

相关文章
|
监控 测试技术 PHP
|
Web App开发 监控 PHP