#!/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
SED=/bin/sed
#MII=/sbin/mii-tool
MII=/sbin/ethtool
HEAD=/bin/head

FLAG_VERBOSE=TRUE
INTERFACE=""
LEVEL_WARN="0"
LEVEL_CRIT="0"
RESULT=""
EXIT_STATUS=$STATE_OK
RESULTstate="NETIO 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=""
testinterface=`sudo $IFCONFIG | $GREP 'mtu' | $CUT -d":" -f1`
if [ -z "$ACTIVE_INTERFACE" ]
then
	#for INTERFACE in {enp4s0f0,enp4s0f1,enp4s0f2,enp4s0f3,enp4s0f4,enp4s0f5,enp4s0f6,enp4s0f7,br0,eth0,eth1,eth2,eth3,eth4,eth5,eth6,eth7,em0,em1,em2,em3,em4,em5,em6,em7}
	for  INTERFACE in $testinterface
	do
		if [ $INTERFACE != "lo" ];then
		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
		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 -"
    if [ $5 ];then
        BYTES_RX=`sar -n DEV 1 1 | grep $5 |$HEAD -n 1 | $SED 's/  */ /g' | $CUT -d" " -f5`
        BYTES_TX=`sar -n DEV 1 1 | grep $5 |$HEAD -n 1 | $SED 's/  */ /g' | $CUT -d" " -f6`
        RESULT=" $5: RXKB/S=$BYTES_RX, TXKB/S=$BYTES_TX "
        PERFDATA="$5'RXKB/S'=$BYTES_RX; $5'TXKB/S'=$BYTES_TX;"
    else
	for interface in $ACTIVE_INTERFACE
	do
		#BYTES_RX=`sudo $IFCONFIG $interface | $GREP 'RX packets' | $CUT -d"(" -f2 | $CUT -d")" -f1`
		#BYTES_TX=`sudo $IFCONFIG $interface | $GREP 'TX packets' | $CUT -d"(" -f2 | $CUT -d")" -f1`
		#bytes_rx=`sudo $IFCONFIG $interface | $GREP 'RX packets' | $SED 's/^ *//g'| $CUT -d" " -f6`
		#bytes_tx=`sudo $IFCONFIG $interface | $GREP 'TX packets' | $SED 's/^ *//g'| $CUT -d" " -f6`
        	BYTES_RX=`sar -n DEV 1 1 | grep $interface |$HEAD -n 1 | $SED 's/  */ /g' | $CUT -d" " -f5`
        	BYTES_TX=`sar -n DEV 1 1 | grep $interface |$HEAD -n 1 | $SED 's/  */ /g' | $CUT -d" " -f6`
#	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: RXKB/S=$BYTES_RX, TXKB/S=$BYTES_TX "
		PERFDATA=$PERFDATA"$interface'RXKB/S'=$BYTES_RX; $interface'TXKB/S'=$BYTES_TX;"
#		elif [ `echo "$BYTES_RX" | grep "Device not found"` ];then
#			RESULT="NETIO ERROR - No interface is aviliabled."
#			EXIT_STATUS=$STATE_UNKNOWN
#		fi
	done
    fi
    if [ $1 ] && [ $2 ] && [ $3 ] && [ $4 ]; then
        warningrx=$1
        warningtx=$2 
        crirx=$3
        critx=$4
    
    if [ `echo "$warningrx < $BYTES_RX" | bc` -eq 1 ] || [ `echo "$warningtx < $BYTES_TX" | bc` -eq 1 ];then
        EXIT_STATUS=$STATE_WARNING
        RESULTstate="NETIO WARNING -"
    fi
    if [ `echo "$crirx < $BYTES_RX" | bc` -eq 1 ] || [ `echo "$critx < $BYTES_TX" | bc` -eq 1 ];then
         EXIT_STATUS=$STATE_CRITICAL
         RESULTstate="NETIO CRITICAL -"
    fi
    fi 
	#RESULT="service_netio_ok"$RESULT"|"$PERFDATA
        #RESULT=$RESULT" $interface: RXKB/S=$BYTES_RX, TXKB/S=$BYTES_TX "
        #PERFDATA=$PERFDATA"'RX KB/S'=$BYTES_RX; 'TX KB/S'=$BYTES_TX;"
	RESULT=$RESULTstate$RESULT
        RESULT=$RESULT"|"$PERFDATA
        #EXIT_STATUS=$STATE_OK
		#key-key
   
else
	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
