opexxx

rpitraffic.sh

Aug 8th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ##
  3. ##This script return the bandwidth in Rx and Tx by substract old Rx/Tx values by the new ones.
  4. ##Ex: If the script is called every 10s, it will compare the rx_bytes 10s ago (old_rx) with the new one (rx) and substract these two values to get a value as Bytes/10S
  5. ##
  6. ## Author : TDUVAL
  7.  
  8. # Location of the networkTraffic files
  9. LOCATION="/usr/share/rpimonitor/web/custom/net_traffic/"
  10.  
  11. # Test if directory $path exist then create it
  12. if [ ! -d $LOCATION ]
  13. then    mkdir $LOCATION
  14. fi
  15.  
  16. # Test if file rx exist then create it
  17. if [ ! -f $LOCATION/rx ]
  18. then    touch $LOCATION/rx
  19. fi
  20.  
  21. # Test if file tx exist then create it
  22. if [ ! -f $LOCATION/tx ]
  23. then    touch $LOCATION/tx
  24. fi
  25.  
  26. # Get old values in rx/tx files
  27. OLDRX=`cat $LOCATION/rx`
  28. OLDTX=`cat $LOCATION/tx`
  29.  
  30. # Get new values
  31. RX=`cat /sys/class/net/eth0/statistics/rx_bytes`
  32. TX=`cat /sys/class/net/eth0/statistics/tx_bytes`
  33.  
  34. # Store new values in rx/tx files
  35. echo $RX > $LOCATION/rx
  36. echo $TX > $LOCATION/tx
  37.  
  38. # Substract new - old to get traffic in Bytes/period
  39. RX_TRAFFIC=$(($RX-$OLDRX))
  40. TX_TRAFFIC=$(($TX-$OLDTX))
  41.  
  42. # Display traffic value in stdout
  43. echo -e "rx=$RX_TRAFFIC\ntx=$TX_TRAFFIC"
Add Comment
Please, Sign In to add comment