#!/bin/bash

INTERVAL="1"  # update interval in seconds
#INTERVAL='0.3' # update interval in seconds

IF=${1:-'eth0'}

if [ -z "$1" ]; then

         cat <<EOF
usage: ${0} [network-interface]
e.g. $0 eth0
shows packets-per-second
USING DEFAULT VALUE ${IF}
EOF

fi

COL='column -t'

echo ''
echo '-----------------------------------------------------------------------------------'  | ${COL}
echo '-       D A T E AND  T I M E       | Iface | TX PPS | TX MBps  | RX PPS | RX Mbps -' | ${COL}
echo '-----------------------------------------------------------------------------------' | ${COL}

while true
do
        R1=`cat /sys/class/net/${IF}/statistics/rx_packets`
        T1=`cat /sys/class/net/${IF}/statistics/tx_packets`
        BT1=`cat /sys/class/net/${IF}/statistics/tx_bytes`
        BR1=`cat /sys/class/net/${IF}/statistics/rx_bytes`
        sleep $INTERVAL
        R2=`cat /sys/class/net/${IF}/statistics/rx_packets`
        T2=`cat /sys/class/net/${IF}/statistics/tx_packets`
        BT2=`cat /sys/class/net/${IF}/statistics/tx_bytes`
        BR2=`cat /sys/class/net/${IF}/statistics/rx_bytes`
        TXPPS=`expr $T2 - $T1`
        RXPPS=`expr $R2 - $R1`
        TXBPS=`expr $BT2 - $BT1`
        RXBPS=`expr $BR2 - $BR1`
        TXMBPS=$((${TXBPS}*8/1024/1024))
        RXMBPS=$((${RXBPS}*8/1024/1024))

        #echo "$(date "+- %Y/%m/%d %H:%M:%S") INT: ${IF} | TX: $TXPPS pps | $                                                                                        {TXMBPS} Mbps | RX: ${RXPPS} pps | ${RXMBPS} Mbps -" | ${COL}
        echo "$(date "+- %Y/%m/%d %H:%M:%S") INT: ${IF} | TX: $TXPPS pps | ${TXMBPS} Mbps | RX: $RXPPS pps | ${RXMBPS} Mbps -" | ${COL}
        #echo "$(date "+- %Y/%m/%d %H:%M:%S") INT: ${IF} | RX: $RXPPS pps | ${RXMBPS} Mbps -" | ${COL}
        #echo "TX ${IF}: $TXPPS pps RX ${IF}: $RXPPS pps"
done

