码迷,mamicode.com
首页 > 其他好文 > 详细

流量监控脚本

时间:2018-04-24 17:23:08      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:nagios 流量监控

#!/bin/bash
#set -x

========================================================================================

INTERFACE Traffic Statistics plugin for Nagios

#

Written by : majun

Release : 1.0

Creation date : 2012-4-4

Usage : ./check_iftra.sh [-w <in:warn,out:warn>] [-c <in:crit,out:crit>]

[-i <DEV>]

----------------------------------------------------------------------------------------

========================================================================================

#

HISTORY :

Release | Date | Authors | Description

--------------+-----------------+---------------+------------------------------------------

1.0 | 04.04.12 | majun |INTERFACE Traffic Statistics

--------------+-----------------+---------------+------------------------------------------

1.1 | 04.17.12 | majun |Add Ignore up rate ,Ignore the notice when traffic is too small.

--------------+-----------------+---------------+------------------------------------------

1.2 | 05.08.17 | majun |Add Ignore time hour ,Ignore the traffic jiffter warning where ignore time.

--------------+-----------------+---------------+------------------------------------------

Paths to commands used in this script. These may have to be modified to match your system setup.

USER=whoami

Nagios return codes

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

DW=K

ENABLE_IGNORE=1
IGNORE_UP_RATE=0.2

ifIn="ifHCInOctets"
ifOut="ifHCOutOctets"
Min_Interval=10
Max_Interval=1800

Functions plugin usage

print_release() {
echo "$RELEASE $AUTHOR"
}

print_usage() {
echo ""
echo "$PROGNAME $RELEASE - INTERFACE Traffic Statistics plugin for Nagios"
echo ""
echo "$0 -H host -C snmp-community -I interface [-L] -w in,out-warning-value -c in,out-critical-value -jw Jitter in,out-warning -jc Jitter in,out-critical -K/M"
echo ""
echo "Flags:"
echo " -H hostname"
echo " -C snmp-community "
echo " -I Interface Number "
echo " -w <number>K|M,<number>K|M : in && out ,For example ‘-w 50:60‘"
echo " -c <number>K|M,<number>K|M : in && out ,For example ‘-c 50:60‘"
echo " -jw Jitter in && out Change %,For example ‘-jw 50:60‘"
echo " -jc Jitter in && out Change %,For example ‘-jc 50:60‘"
echo " -h Show this page"
echo ""
echo "Examle"
echo "$0 -H 101.4.60.1 -C asjdkajskdaaqweqwxzcm -I 504 -L"
echo "$0 -H 101.4.60.1 -C asjdkajskdaaqweqwxzcm -I 504 -w 300:4000 -c 400:5000 -jw 10:10 -jc 20:20 -M"
echo "$0 -H 101.4.60.1 -C asjdkajskdaaqweqwxzcm -I 504 -w 300000:4000000 -c 40000:5000000 -jw 10:10 -jc 20:20 -K"
echo ""
}
print_help() {
print_usage
echo ""
echo "This plugin will check interface traffic for nagios"
echo ""
exit 0
}

if [ $# == 0 ]
then
print_help
fi

Parse parameters

while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
print_help
exit $STATE_OK
;;
-v | --version)
print_release
exit $STATE_OK
;;
-L )
NAMELIST=TURE
;;
-H | --host)
shift
HOST=$1
;;
-C | --community)
shift
COMMUNITY=$1
;;
-I | --INTERFACE)
shift
INTERFACE=$1
;;
-w | --warning)
shift
IN_WARN=echo $1|awk -F ‘:‘ ‘{print $1}‘
OUT_WARN=echo $1|awk -F ‘:‘ ‘{print $2}‘
;;
-c | --critical)
shift
IN_CRIT=echo $1|awk -F ‘:‘ ‘{print $1}‘
OUT_CRIT=echo $1|awk -F ‘:‘ ‘{print $2}‘
;;
-jw)
shift
IN_JWARN=echo $1|awk -F ‘:‘ ‘{print $1}‘
OUT_JWARN=echo $1|awk -F ‘:‘ ‘{print $2}‘
;;
-jc)
shift
IN_JCRIT=echo $1|awk -F ‘:‘ ‘{print $1}‘
OUT_JCRIT=echo $1|awk -F ‘:‘ ‘{print $2}‘
;;
-ij)
shift
IGBG=echo $1|awk -F ‘:‘ ‘{print $1}‘
IGED=echo $1|awk -F ‘:‘ ‘{print $2}‘
;;
-M)
DW=M
;;
-K)
DW=K
;;
*) echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done

to_debug(){
if [ "$Debug" = "true" ]; then
echo "$" #>> /tmp/check_traffic.log.$$ 2>&1
#$Echo "$
" >> /tmp/check_traffic.log 2>&1
fi
}

CF_HIST_DATA="/var/tmp/checktraffic${HOST}_${INTERFACE}.histdat${USER}"
to_debug $HOST w=$IN_WARN OUT_WARN=$OUT_WARN IN_CRIT=$IN_CRIT OUT_CRIT=$OUT_CRIT IN_JWARN=$IN_JWARN OUT_JWARN=$OUT_JWARN IN_JCRIT=$IN_JCRIT OUT_JCRIT=$OUT_JCRIT

SNMPWALK=which snmpwalk
ifName=$SNMPWALK -v 2c -c $COMMUNITY $HOST IF-MIB::ifDescr.${INTERFACE}| awk -F ":" ‘{print $4}‘
_result_status=$SNMPWALK -v 2c -c $COMMUNITY $HOST "IF-MIB::ifOperStatus.${INTERFACE}"| awk ‘{print $4}‘ | awk -F ‘(‘ ‘{print $1}‘
if [ "$NAMELIST"aa = TUREaa ]
then
$SNMPWALK -v 2c -c $COMMUNITY $HOST IF-MIB::ifDescr
#then echo Interface.$INTERFACE=$ifName
exit 0
fi

if [ ! -f $CF_HIST_DATA ]; then
IsFirst="True"
touch $CF_HIST_DATA
if [ $? -ne 0 ];then
Severity=3
Msg="Unknown"
OutPut="Create File $CF_HIST_DATA Error with user id."
$Echo "$Msg" "-" $OutPut
exit $Severity
fi

fi

_result_in=$SNMPWALK -v 2c -c $COMMUNITY $HOST "IF-MIB::${ifIn}.$INTERFACE"
_result_out=$SNMPWALK -v 2c -c $COMMUNITY $HOST "IF-MIB::${ifOut}.$INTERFACE"

if [ -z "$_result_in" -o -z "$_result_out" ] ; then
$Echo "No Data been get here. Please confirm your ARGS and re-check it with Verbose mode"
exit 3
fi
to_debug time is $Time, $SNMPWALK check result in is $_result_in, out is $_result_out
TIME=date +%s
_result_in=echo $_result_in |awk ‘{print $4}‘
_result_out=echo $_result_out|awk ‘{print $4}‘

CIN=echo "$_result_in * 1 " |bc
COUT=echo "$_result_out * 1 " |bc

to_debug Time is $Time, In is $In, Out is $Out

HISTDATA=cat $CF_HIST_DATA| head -n 1
HISTTIME=echo $HISTDATA| awk -F "|" ‘{print $1}‘|sed ‘s/ //g‘
HISTIN=echo $HISTDATA| awk -F "|" ‘{print $2}‘|sed ‘s/ //g‘
HISTOUT=echo $HISTDATA| awk -F "|" ‘{print $3}‘|sed ‘s/ //g‘
HISTPDATA=cat $CF_HIST_DATA| tail -n 1
HISTINPS=echo $HISTPDATA| awk -F "|" ‘{print $1}‘|sed ‘s/ //g‘
HISTOUTPS=echo $HISTPDATA| awk -F "|" ‘{print $2}‘|sed ‘s/ //g‘
to_debug HistTime is $HISTTIME, HistIn is $HISTIN, HistOut is $HISTOUT
DIFFIN=echo "$CIN - $HISTIN" | bc
DIFFOUT=echo "$COUT - $HISTOUT" | bc
Interval=echo "$TIME - $HISTTIME" | bc
INPS=echo "$DIFFIN / $Interval" | bc
OUTPS=echo "$DIFFOUT / $Interval" | bc

if [ -z "$HISTTIME" -o -z "$HISTIN" -o -z "$HISTOUT" ] ; then
echo "$TIME|$CIN|$COUT" > $CF_HIST_DATA
if [ "$IsFirst" = "True" ]; then
Severity="0"
Msg="OK"
OutPut="It‘s the first time for this plugins run. We‘ll get the data from the next time."
else
Severity="3"
Msg="Unknown"
OutPut="Can not found data in the history data file. \
Please to check the file $CF_HIST_DATA ,or use use verbose mode and check the debug file"
fi
echo "$Msg" "-" $OutPut
exit $Severity
fi

LINE=cat $CF_HIST_DATA|wc -l
if [ $LINE == 1 ]
then
echo "$TIME|$CIN|$COUT" > $CF_HIST_DATA
echo "$INPS|$OUTPS" >>$CF_HIST_DATA
echo OK "It‘s the Second time for this plugins run. We‘ll get the data from the next time."
exit 0
fi

if [ $INPS = 0 ]
then
INPS=1
fi
if [ $OUTPS = 0 ]
then
OUTPS=1
fi
if [ $Interval -lt $Min_Interval ] ; then
echo "The check interval must greater than $Min_Interval Seconds. But now it‘s $Interval. Please retry it later."
exit 3
fi
#if [ $Interval -gt $Max_Interval ] ; then

echo "The check interval is too large(It\‘s greate than $Max_Interval). The result is droped. We\‘ll use the fresh data at the next time."

exit 3

#fi

echo "$TIME|$CIN|$COUT" >$CF_HIST_DATA
echo "$INPS|$OUTPS" >>$CF_HIST_DATA

INPSK=echo "$INPS * 8 / 1024"|bc
OUTPSK=echo "$OUTPS * 8 / 1024" |bc

INPSM=echo "$INPSK /1024"|bc
OUTPSM=echo "$OUTPSK / 1024"|bc

if [ $DW = M ]
then
INPSRE=${INPSM}
OUTPSRE=${OUTPSM}

else

    INPSRE=${INPSK}
    OUTPSRE=${OUTPSK}

fi

    check_INW=`echo "$INPSRE < $IN_WARN" | bc`
    check_OUTW=`echo "$OUTPSRE < $OUT_WARN" | bc`
    check_INC=`echo "$INPSRE < $IN_CRIT" | bc`
    check_OUTC=`echo "$OUTPSRE < $OUT_CRIT" | bc`

    if [ $check_INW -eq 1 -a $check_OUTW -eq 1 ] ; then
            Severity="0";
            Msg="OK";
            to_debug Severity is $Severity , Msg is $Msg
    elif [ $check_INC -eq 1 -a $check_OUTC -eq 1 ] ; then
            Severity="1";
            Msg="Warning";
            to_debug Severity is $Severity , Msg is $Msg
    else
            Severity="2";
            Msg="Critical";
            to_debug Severity is $Severity , Msg is $Msg
    fi

    DiffRIAVG=`echo "$INPS - $HISTINPS"|bc`
    DiffROAVG=`echo "$OUTPS - $HISTOUTPS"|bc`

    if [ "$DiffRIAVG" -gt "0" ]
    then
            RID=Up
    else
            RID=Down
    fi
    if [ "$DiffROAVG" -gt "0" ]
    then
            ROD=Up
    else
            ROD=Down
    fi
    DiffRIAVG=`echo $DiffRIAVG | sed ‘s/-//‘`
    DiffROAVG=`echo $DiffROAVG | sed ‘s/-//‘`

    DiffRIAVG=`echo "scale=0; $DiffRIAVG * 100 / $HISTINPS  " |bc`
    DiffROAVG=`echo "scale=0; $DiffROAVG * 100 / $HISTOUTPS " |bc`

    check_INJW=`echo "$DiffRIAVG < $IN_JWARN" | bc`
    check_OUTJW=`echo "$DiffROAVG < $OUT_JWARN" | bc`
    check_INJC=`echo "$DiffRIAVG < $IN_JCRIT" | bc`
    check_OUTJC=`echo "$DiffROAVG < $OUT_JCRIT" | bc`

    IGN_IN=`echo "$IN_CRIT * $IGNORE_UP_RATE < $INPSRE"|bc`
    IGN_OUT=`echo "$OUT_CRIT * $IGNORE_UP_RATE < $OUTPSRE"|bc`
    if [ "$ENABLE_IGNORE" == "1" ]
    then
            if [ $RID == Up -a $IGN_IN == 0 ] 
            then
                    check_INJW=1
                    check_INJC=1
            fi

            if [ $ROD == Up -a $IGN_OUT == 0 ]
            then
                    check_OUTJW=1
                    check_OUTJC=1
            fi
    fi

    if [ $check_INJW -eq 1 -a $check_OUTJW -eq 1 ] ; then
            JSeverity="0";
            JMsg="OK";
            to_debug Severity is $Severity , Msg is $Msg
    elif [ $check_INJC -eq 1 -a $check_OUTJC -eq 1 ] ; then
            JSeverity="1";
            JMsg="Warning";
            to_debug Severity is $Severity , Msg is $Msg
    else
            JSeverity="2";
            JMsg="Critical";
            to_debug Severity is $Severity , Msg is $Msg
    fi

if [ "$IGBG" != "0" ] && [ ! -z "$IGED" ]
then
NHOUR=date +%H
if [ $IGED -ge $IGBG ]
then
if [ "$NHOUR" -le "$IGED" ] && [ "$NHOUR" -ge "$IGBG" ]
then
JSeverity="0"
fi
else
if [ "$NHOUR" -ge "$IGBG" ] || [ "$NHOUR" -le "$IGED" ]
then
JSeverity="0"
fi
fi
fi

if [ "$JSeverity" -eq "2" -o "$Severity" -eq "2" ]
then
echo "Critical" "-" $ifName Traffic In BW="$INPSRE""${DW}bps Jitter is $RID "$DiffRIAVG""%", Out BW="$OUTPSRE""${DW}bps Jitter is $ROD "$DiffROAVG""%". The Check Interval is "$Interval"s |In\=${INPSRE}${DW} Out\=${OUTPSRE}${DW} JIn\=${DiffRIAVG}% JOut\=${DiffROAVG}%
exit 2
elif [ "$JSeverity" = 1 -o "$Severity" = "1" ]
then
echo "Warning" "-" $ifName Traffic In BW="$INPSRE""${DW}bps Jitter is $RID "$DiffRIAVG""%", Out BW="$OUTPSRE""${DW}bps Jitter is $ROD "$DiffROAVG""%". The Check Interval is "$Interval" s |In\=${INPSRE}${DW} Out\=${OUTPSRE}${DW} JIn\=${DiffRIAVG}% JOut\=${DiffROAVG}%
exit 1
else

            echo "OK" "-" $ifName Traffic In BW="$INPSRE""${DW}bps  Jitter is $RID "$DiffRIAVG""%", Out BW="$OUTPSRE""${DW}bps Jitter is $ROD "$DiffROAVG""%". The Check Interval is "$Interval"s \|In\=${INPSRE}${DW} Out\=${OUTPSRE}${DW} JIn\=${DiffRIAVG}% JOut\=${DiffROAVG}%
            exit 0

fi

流量监控脚本

标签:nagios 流量监控

原文地址:http://blog.51cto.com/13718151/2107341

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