首页 > nagios > nagios监控带宽插件更新

nagios监控带宽插件更新

2010年12月8日 eric 发表评论 阅读评论

以前写的一个nagios 监控带宽的插件,今天看了下有个小BUG,看代码

BS="`cat wget-log |tail -n20 |awk '{print $8}'|sed 's/K//'|awk '{sum+=$1};END{print sum}'`"
       speed=`echo $BS / 19|bc`

我是统计20行数据,在我算平均值的时候是除以了19,是按19行算的,因为最后一行90%数据是不完整,没有统计到下载速度,所以我是按19行来算的,但是考虑到也有10%的可能有数据,这样按照19行来统计平均值会不准确。所以加一条管道更新了一下脚本:

#!/bin/bash
PROGNAME=`basename $0`
VERSION="Version 1.1"
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. Defaut is: off"
                echo ""
                echo "  --critical|-c)"
                echo "          Sets a critical level for download speed. Defaut 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 -n21 |head -20|awk '{print $8}'|sed 's/K//'|awk '{sum+=$1};END{print sum}'`"
        speed=`echo $BS / 20|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

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/363.html

分类: nagios 标签: , ,
订阅评论
1+7= (必填)