#!/bin/bash
#===============================================================================
#
#          FILE:  check_traffic.sh
#
#         USAGE:  ./check_traffic.sh
#
#   DESCRIPTION:  Check traffic on all network devices in kb/s per device
#
#       OPTIONS:  -w WARN_OUT -c CRIT_OUT
#		  -e WARN_IN -v CRIT_IN [-p] [-h]
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Sebastian Grewe (), sebastiang@jammicron.com
#       COMPANY:  Jammicron Technology Corp.
#       VERSION:  1.0
#       CREATED:  04/12/08 02:23:41 PM PST
#      REVISION:  14
#===============================================================================

# status "DEVOCE" "COUNTER"
# Create the status string for all the devices found
function status() {
	DEV=$1
	COUNT=$2
	if [[ ${RXS[$COUNT]} -le $WI ]]; then
		STATUS="OK"
		[[ $EXIT != 2 && $EXIT != 1 ]] && EXIT=0
	elif [[ ${RXS[$COUNT]} -lt $CI ]]; then
		MSG="$MSG $DEV rx rates warning"
		STATUS="WARNING"
		[[ $EXIT != 2 ]] && EXIT=1
	elif [[ ${RXS[$COUNT]} -ge $CI ]]; then
		MSG="$MSG $DEV rx rates critical"
		STATUS="CRITICAL"
		EXIT=2
	fi
	
	if [[ ${TXS[$COUNT]} -le $WO ]]; then
		[[ $STATUS != "WARNING" ]] && [[ $STATUS != "CRITICAL" ]] && STATUS="OK"
		[[ $EXIT != 2 && $EXIT != 1 ]] && EXIT=0
	elif [[ ${TXS[$COUNT]} -lt $CO ]]; then
		MSG="$MSG $DEV tx rates warning"
		[[ $STATUS != "CRITICAL" ]] && STATUS="WARNING"
		[[ $EXIT != 2 ]] && EXIT=1
	elif [[ ${TXS[$COUNT]} -ge $CO ]]; then
		MSG="$MSG $DEV tx rates critical"
		STATUS="CRITICAL"
		EXIT=2
	fi
}

# perfor "DEVICE" "COUNTER"
# Create the performance string for DEVICE
function perfor() {
	local DEV=$1
	local COUNT=$2
	if [[ $perf == "yes" ]]; then
		PERFORMANCE="$PERFORMANCE rxrate-$DEV=${RXS[$COUNT]};$WI;$CI;; txrate-$DEV=${TXS[$COUNT]};$WO;$CO;;"
	fi
}

# Parse Arguments
while getopts 'w:c:e:v:hp' OPT; do
  case $OPT in
    w)  WO=$OPTARG;;
    c)  CO=$OPTARG;;
    e)  WI=$OPTARG;;
    v)  CI=$OPTARG;;
    h)  hlp="yes";;
    p)  perf="yes";;
    *)  unknown="yes";;
  esac
done

# usage
HELP="
    usage: $0 [ -w value -c value -p -h ]

    syntax:

            -w --> Warning integer value outgoing traffic
            -c --> Critical integer value outgoing traffic
            -e --> Warning integer value incoming traffic
            -v --> Critical integer value incoming traffic
            -p --> print out performance data
            -h --> print this help screen
"
if [ "$hlp" = "yes" -o $# -lt 8 ]; then
  echo "$HELP"
  exit 0
fi

# Get all interfaces with a MAC address
INTERFACES=`/sbin/ifconfig | grep HWaddr | awk '{print $1}'`

# Parse traffic for each interface
for interface in $INTERFACES; do
	RX1=(`/sbin/ifconfig $INTERFACE | grep "RX bytes" | awk '{print $2}' | sed -e "s/bytes://"`)
	TX1=(`/sbin/ifconfig $INTERFACE | grep "RX bytes" | awk '{print $6}' | sed -e "s/bytes://"`)
done

sleep 5

COUNT=0

# Parse it again and calculate kb/s
for interface in $INTERFACES; do
	# Get traffic again
	RX2=(`/sbin/ifconfig $INTERFACE | grep "RX bytes" | awk '{print $2}' | sed -e "s/bytes://"`)
	TX2=(`/sbin/ifconfig $INTERFACE | grep "RX bytes" | awk '{print $6}' | sed -e "s/bytes://"`)

	# Make temp values with total transferred kb/s
	((tmpr=((${RX2[$COUNT]}-${RX1[$COUNT]})/1024)/5))
	((tmpt=((${TX2[$COUNT]}-${TX1[$COUNT]})/1024)/5))

	# Fill into array
	RXS[$COUNT]=$tmpr
	TXS[$COUNT]=$tmpt

	# Generate status output
	status $interface $COUNT
	perfor $interface $COUNT
	((COUNT=$COUNT+1))
done

# do the nagios magic
echo "TRAFFIC $STATUS - $COUNT interfaces checked |$PERFORMANCE"
exit $EXIT
