#!/bin/bash

IFCONFIG="/sbin/ifconfig"

## Device Set
IF=$1

## Path Set
IF_CUR="/tmp/${IF}_interface.cur"
IF_AFTER="/tmp/${IF}_interface.after"

## Get packet
$IFCONFIG -s $IF > $IF_CUR
sleep 1
$IFCONFIG -s $IF > $IF_AFTER


SET_PACKET() {
## current packet 
RX1=`cat $IF_CUR|awk '{print $4}'|tail -1`
TX1=`cat $IF_CUR|awk '{print $8}'|tail -1`

## After packet
RX2=`cat $IF_AFTER|awk '{print $4}'|tail -1`
TX2=`cat $IF_AFTER|awk '{print $8}'|tail -1`
}

for ((i=1;i<=3;i++))
do

  SET_PACKET

  if [ -n "$RX1" ] && [ -n "$TX1" ] && [ -n "$RX2" ] && [ -n "$TX2" ]
  then
      if [ $RX2 -ge $RX1 ] && [ $TX2 -ge $TX1 ]
      then
          echo "`date +%Y%m%d-%H:%M:%S` $RX1 $TX1 $RX2 $TX2" >> /tmp/${IF}_interface.log

          ## pps calculation  
          RX_DIFF=`expr $RX2 - $RX1`
          TX_DIFF=`expr $TX2 - $TX1`

          break
      else
          RX_DIFF=0
          TX_DIFF=0          

          break
      fi
  else
      RX_DIFF=0
      TX_DIFF=0
  fi
done

echo "$RX_DIFF $TX_DIFF"
