#!/bin/sh
###############################################
#
# Nagios script to check network I/O status
#
# Copyright 2007, 2008 Ian Yates
#
# See usage for command line switches
# 
# NOTE: Because of the method of gathering information, bytes/s values are calculated here, so no wanring/critical values can be set to trigger. 
#       Consequently, this check plugin always returns OK.
#       This plugin is a means of returning stats to nagios for graphing (recommend DERIVE graph in RRD)
#
# Created: 2007-09-06 (i.yates@uea.ac.uk)
# Updated: 2007-09-06 (i.yates@uea.ac.uk)
# Updated: 2008-11-27 (i.yates@uea.ac.uk) - Added GPLv3 licence
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
###############################################

PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
. $PROGPATH/utils.sh


VERSION="1.1"

IFCONFIG=/sbin/ifconfig
GREP=/bin/grep
CUT=/bin/cut
#MII=/sbin/mii-tool
MII=/sbin/ethtool

FLAG_VERBOSE=TRUE
INTERFACE=""
LEVEL_WARN="0"
LEVEL_CRIT="0"
RESULT=""
EXIT_STATUS=$STATE_OK



###############################################
#
## FUNCTIONS 
#

## Print usage
usage() {
	echo " check_netio $VERSION - Nagios network I/O check script"
	echo ""
	#echo " Usage: check_netio {-i} [ -v ] [ -h ]"
	echo " Usage: check_netio [ -v ] [ -h ]"
	echo ""
	#echo "		 -i  Interface to check (e.g. eth0)"
	echo "		 -v  Verbose output (ignored for now)"
	echo "		 -h  Show this page"
	echo ""
}
 
## Process command line options
doopts() {
	if ( `test 0 -lt $#` )
	then
		while getopts i:vh myarg "$@"
		do
			case $myarg in
				h|\?)
					usage
					exit;;
				i)
					INTERFACE=$OPTARG;;
				v)
					FLAG_VERBOSE=TRUE;;
				*)	# Default
					usage
					exit;;
			esac
		done
	else
		usage
		exit
	fi
}


# Write output and return result
theend() {
	echo $RESULT
	exit $EXIT_STATUS
}


#
## END FUNCTIONS 
#

#############################################
#
## MAIN 
#


# Handle command line options
#doopts $@

# Do the do

### get the active interface
# add auto-check of active_interface
# by cailiuqing 2012-04-11 begin
ACTIVE_INTERFACE=""

if [ -z "$ACTIVE_INTERFACE" ]
then
	for INTERFACE in {br0,eth0,eth1,eth2,eth3,eth4,eth5,eth6,eth7,em0,em1,em2,em3,em4,em5,em6,em7}
	do
		INTERFACE_STAT=$(sudo $MII $INTERFACE 2>/dev/null)
                #INTERFACE_OK=`echo "$INTERFACE_STAT" | grep "link ok"`
                INTERFACE_OK=`echo "$INTERFACE_STAT" | grep "Link detected: yes"`
                if [ "$INTERFACE_OK" != "" ];then
                        ACTIVE_INTERFACE="$ACTIVE_INTERFACE $INTERFACE"
                fi

	done
fi
# add by cailiuqing 2012-04-11 end

### get the active interface's rx/tx_bytes
if [ "$ACTIVE_INTERFACE" != "" ];then
	RESULT="NETIO OK -"
	for interface in $ACTIVE_INTERFACE
	do
		BYTES_RX=`sudo $IFCONFIG $interface | $GREP 'bytes' | $CUT -d"(" -f2 | $CUT -d")" -f1`
		BYTES_TX=`sudo $IFCONFIG $interface | $GREP 'bytes' | $CUT -d"(" -f3 | $CUT -d")" -f1`
		bytes_rx=`sudo $IFCONFIG $interface | $GREP 'bytes' | $CUT -d":" -f2 | $CUT -d" " -f1`
		bytes_tx=`sudo $IFCONFIG $interface | $GREP 'bytes' | $CUT -d":" -f3 | $CUT -d" " -f1`
#	if [ "$BYTES_RX" != "0.0" ] & [ "$BYTES_TX" != "0.0" ];then
		#key-key
		#RESULT="NETIO OK - $ACTIVE_INTERFACE: RX=$BYTES_RX, TX=$BYTES_TX | 'BYTES_RX'=$bytes_rx; 'BYTES_TX'=$bytes_tx;"
		#RESULT=$RESULT"#$interface#$BYTES_RX#$BYTES_TX "
		RESULT=$RESULT" $interface: RX=$BYTES_RX, TX=$BYTES_TX "
		PERFDATA=$PERFDATA"'BYTES_RX_$interface'=$bytes_rx; 'BYTES_TX_$interface'=$bytes_tx;"
#		elif [ `echo "$BYTES_RX" | grep "Device not found"` ];then
#			RESULT="NETIO ERROR - No interface is aviliabled."
#			EXIT_STATUS=$STATE_UNKNOWN
#		fi
	done
	#RESULT="service_netio_ok"$RESULT"|"$PERFDATA
        RESULT=$RESULT"|"$PERFDATA
        EXIT_STATUS=$STATE_OK
else
		#key-key
		RESULT="NETIO ERROR - No interface is aviliabled."
	#RESULT="service_netio_error"
        EXIT_STATUS=$STATE_UNKNOWN
fi
#   fi
#done


# Quit and return information and exit status
theend
