码迷,mamicode.com
首页 > 移动开发 > 详细

nagios监控流量脚本

时间:2014-05-06 08:46:56      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   color   

需求是我们需要对服务器上的流量进行监控,网络上有个流传的check_traffic.sh,它需要被监控机开启snmp。但是感觉都使用上了nagios还要开snmp。。。有点斧子剪刀一起用的感觉,所以就动手写了个监控流量的shell:

bubuko.com,布布扣
#!/bin/sh

usage() { echo "Usage: $0 [-n <eth0>] [-w <tx rx>] [-c <tx rx>]" 1>&2; exit 1; }

foundw=0;
foundc=0;
foundn=0;

for item in $@ ; do
    if [[ $foundn == 1 ]]; then
        n=$item;
        foundn=2;
        continue;
    fi
    if [[ $foundw == 1 ]]; then
        w1=$item;
        foundw=2;
        continue;
    fi
    if [[ $foundw == 2 ]]; then
        w2=$item;
        foundw=3;
        continue;
    fi
    if [[ $foundc == 1 ]]; then
        c1=$item;
        foundc=2;
        continue;
    fi
    if [[ $foundc == 2 ]]; then
        c2=$item;
        foundc=2;
        continue;
    fi
    if [[ "$item" == "-w" ]]; then
        foundw=1;
        continue;
    fi
    if [[ "$item" == "-c" ]]; then
        foundc=1;
        continue;
    fi
    if [[ "$item" == "-n" ]]; then
        foundn=1;
        continue;
    fi
done

if [ -z "${w1}" ] || [ -z "${w2}" ] || [ -z "${c1}" ] || [ -z "${c2}" ] || [ -z "${n}" ]; then
    usage
fi

R1=`cat /sys/class/net/$n/statistics/rx_bytes`
T1=`cat /sys/class/net/$n/statistics/tx_bytes`
sleep 1
R2=`cat /sys/class/net/$n/statistics/rx_bytes`
T2=`cat /sys/class/net/$n/statistics/tx_bytes`
TBPS=`expr $T2 - $T1`
RBPS=`expr $R2 - $R1`
TMBPS=`expr $TBPS / 1024 / 128`
RMBPS=`expr $RBPS / 1024 / 128`

if [[ $TMBPS -ge $c1 ]] || [[ $RMBPS -ge $c2 ]] ; then
    echo "Critical - current is ${TMBPS}, ${RMBPS}";
    exit 2;
fi
if [[ $TMBPS -ge $w1 ]] || [[ $RMBPS -ge $w2 ]] ; then
    echo "WARNING - current is ${TMBPS}, ${RMBPS}";
    exit 1;
fi
echo "OK - current is ${TMBPS}, ${RMBPS}";
exit 0;
bubuko.com,布布扣

其中的w和c的数值单位都是Mb。

nagios监控流量脚本,布布扣,bubuko.com

nagios监控流量脚本

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/yjf512/p/3704211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!