#!/bin/bash
###############################################
#
# Nagios script to check network I/O status

# Copyright Juned Memon
# Modified by Jonas Meurer <jmeurer@inet.de> for INTERNET AG 2013

# See usage for command line switches

#
#
# 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/>.
#
###############################################

if [ -x /usr/local/nagios/libexec/utils.sh ]; then
	. /usr/local/nagios/libexec/utils.sh
fi
if [ -x /usr/lib64/nagios/plugins/utils.sh ]; then
	. /usr/lib64/nagios/plugins/utils.sh
fi


VERSION="1.1.1"

GREP=/bin/grep
CUT=/usr/bin/cut

FLAG_VERBOSE=FALSE
interface=""
LEVEL_WARN="0"
LEVEL_CRIT="0"
RESULT=""

STATUS="OK"

received_bytes=""
old_received_bytes=""
transmitted_bytes=""
old_transmitted_bytes=""

received_pkgs=""
old_received_pkgs=""
transmitted_pkgs=""
old_transmitted_pkgs=""

#warn=20000  # i.e 20Kbps
#crit=15000  # i.e 15Kbps
#warn=50000000  # i.e 50Mbps
#crit=90000000  # i.e 90MBps
warn=0		# i.e 100Mbps
crit=0		# i.e 100MBps

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

## Print usage
usage() {
        echo " $0 $VERSION - Nagios network I/O check script"
        echo ""
        echo " Usage: $0 {-i} [ -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
}


# This function parses /proc/net/dev file searching for a line containing $interface data.
# Within that line, the first and ninth numbers after ':' are respectively the received and transmited bytes.
function get_bytes
{
    line=$(cat /proc/net/dev | grep $interface | cut -d ':' -f 2 | awk '{print "received_bytes="$1, "transmitted_bytes="$9, "received_pkgs="$2, "transmitted_pkgs="$10}')
    eval $line
}


# Function which calculates the speed using actual and old byte number.
# Speed is shown in KByte per second when greater or equal than 1 KByte per second.
# This function should be called each second.
function get_velocity
{
    value=$1
    old_value=$2

    let vel=$value-$old_value
    let vel=$vel*8
    #let velKb=$vel/1000
    #velKb=$(echo "scale=3; $vel/1000.0" | bc ) # Convert to decimal.
    #if [ $velKb != 0 ];
    #then
 #echo -n "$velKb Kb/s";
    #else
 #echo -n "$vel b/s";
   # fi
echo -n "$vel";
}

# Write output and return result
theend() {
	RESULT="Network Bandwidth $STATUS - $interface: Total $vel_totalKb Kb/s ($vel_total_p Pkgs), Received $vel_recvKb Kb/s ($vel_recv_p Pkgs), Transmitted $vel_transKb Kb/s ($vel_trans_p Pkgs) | total=${vel_totalKb}Kb/s rx=${vel_recvKb}Kb/s tx=${vel_transKb}Kb/s total_p=${vel_total_p} rx_p=${vel_recv_p} tx_p=${vel_trans_p}\n"
        echo -en $RESULT
        exit $EXIT_STATUS
}



# Handle command line options
doopts $@

# Do the do

get_bytes
old_received_bytes=$received_bytes
old_transmitted_bytes=$transmitted_bytes
old_received_pkgs=$received_pkgs
old_transmitted_pkgs=$transmitted_pkgs
sleep 1;
# Get new transmitted and received byte number values.
get_bytes

# Calculates speeds.
vel_recv=$(get_velocity $received_bytes $old_received_bytes)
vel_trans=$(get_velocity $transmitted_bytes $old_transmitted_bytes)
let vel_total=$vel_recv+$vel_trans

vel_recvKb=$(echo "scale=2; $vel_recv/1024.0" | bc ) # Convert to decimal
vel_transKb=$(echo "scale=2; $vel_trans/1024.0" | bc ) # Convert to decimal 

vel_totalKb=$(echo "scale=2; $vel_recvKb+$vel_transKb" | bc )
#vel_totalKb=$(echo "scale=2; $vel_total/1024.0" | bc ) # Convert to decimal  
#vel_total=$(echo "scale=2;$vel_recv+$vel_trans" | bc ) 

vel_recv_p=$(echo "$received_pkgs-$old_received_pkgs" | bc)
vel_trans_p=$(echo "$transmitted_pkgs-$old_transmitted_pkgs" | bc)
let vel_total_p=$vel_recv_p+$vel_trans_p

#if crit and warn = 0
if [ $crit -eq 0 -a $warn -eq 0 ]; then
	STATUS="OK"
	EXIT_STATUS=$STATE_OK
	# Quit and return information and exit status
	theend
fi

#if crit > vel >warn then warning
if [ $vel_total -lt $crit ]; then
	if [ $vel_total -ge $warn ]; then
		STATUS="WARNING"
		EXIT_STATUS=$STATE_WARNING
		# Quit and return information and exit status
		theend
	fi
fi

# vel>crit then critical
if [ $vel_total -ge $crit ]; then
	STATUS="CRITICAL"
	EXIT_STATUS=$STATE_CRITICAL
	# Quit and return information and exit status
	theend
fi

# 0<=vel <warn
if [ $vel_total -lt $warn ]; then
	STATUS="OK"
	EXIT_STATUS=$STATE_OK
	# Quit and return information and exit status
	theend
fi


