#!/bin/bash
#VARS: HOST,SSH_PORT,LOGIN,PASSWORD
#for not empty password required sshpass
#$1 - iface name
#$2 - max byte rate (kbit per second)

if [ ! -f ./scripts/val/net_tx_$HOST:$SSH_PORT ]
then
    OLD_VALUE="0"
    OLD_TIME="0"
else
    OLD_VALUE=`cat ./scripts/val/net_tx_$HOST:$SSH_PORT`
    OLD_TIME=`cat ./scripts/val/net_tx_time_$HOST:$SSH_PORT`
fi

NEW_TIME=`date +%s`

if [ -z "$PASSWORD" ]
then
    NEW_VALUE=`ssh $LOGIN@$HOST -p $SSH_PORT cat /proc/net/dev|grep $1|awk '{print $10}'`
else
    NEW_VALUE=`sshpass -p $PASSWORD ssh $LOGIN@$HOST -p $SSH_PORT cat /proc/net/dev|grep $1|awk '{print $10}'`
fi

echo $NEW_VALUE > ./scripts/val/net_tx_$HOST:$SSH_PORT
echo $NEW_TIME > ./scripts/val/net_tx_time_$HOST:$SSH_PORT

if [ "$NEW_VALUE" -gt "$OLD_VALUE" ]
then
 DELTA=$(expr $NEW_VALUE - $OLD_VALUE)
 TIME=$(expr $NEW_TIME - $OLD_TIME)
 RATE=$(expr $DELTA / $TIME)
 awk -vr=$RATE -vmr=$2 'BEGIN{print(r/128"/"mr)}'
else 
 echo "0"
fi


#Percentage RESULT VAR TYPE (kbitps/kbitps)
