_bandwidth 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. # Copyright (C) 2012 Stefan Breunig <stefan+measure-net-speed@mathphys.fsk.uni-heidelberg.de>
  3. # Copyright (C) 2014 kaueraal
  4. # Copyright (C) 2015 Thiago Perrotta <perrotta dot thiago at poli dot ufrj dot br>
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. # Use the provided interface, otherwise the device used for the default route.
  16. if [[ -n $BLOCK_INSTANCE ]]; then
  17. INTERFACE=$BLOCK_INSTANCE
  18. else
  19. INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }')
  20. fi
  21. # Issue #36 compliant.
  22. if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || ! [ "`cat /sys/class/net/${INTERFACE}/operstate`" = "up" ]
  23. then
  24. echo "$INTERFACE down"
  25. echo "$INTERFACE down"
  26. echo "#FF0000"
  27. exit 0
  28. fi
  29. # path to store the old results in
  30. path="/dev/shm/$(basename $0)-${INTERFACE}"
  31. # grabbing data for each adapter.
  32. read rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes"
  33. read tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes"
  34. # get time
  35. time=$(date +%s)
  36. # write current data if file does not exist. Do not exit, this will cause
  37. # problems if this file is sourced instead of executed as another process.
  38. if ! [[ -f "${path}" ]]; then
  39. echo "${time} ${rx} ${tx}" > "${path}"
  40. chmod 0666 "${path}"
  41. fi
  42. # read previous state and update data storage
  43. read old < "${path}"
  44. echo "${time} ${rx} ${tx}" > "${path}"
  45. # parse old data and calc time passed
  46. old=(${old//;/ })
  47. time_diff=$(( $time - ${old[0]} ))
  48. # sanity check: has a positive amount of time passed
  49. [[ "${time_diff}" -gt 0 ]] || exit
  50. # calc bytes transferred, and their rate in byte/s
  51. rx_diff=$(( $rx - ${old[1]} ))
  52. tx_diff=$(( $tx - ${old[2]} ))
  53. rx_rate=$(( $rx_diff / $time_diff ))
  54. tx_rate=$(( $tx_diff / $time_diff ))
  55. # shift by 10 bytes to get KiB/s. If the value is larger than
  56. # 1024^2 = 1048576, then display MiB/s instead
  57. # incoming
  58. echo -n " "
  59. rx_kib=$(( $rx_rate >> 10 ))
  60. if [[ "$rx_rate" -gt 1048576 ]]; then
  61. printf '%sM' "`echo "scale=1; $rx_kib / 1024" | bc`"
  62. else
  63. echo -n "${rx_kib}K"
  64. fi
  65. echo -n " "
  66. # outgoing
  67. echo -n " "
  68. tx_kib=$(( $tx_rate >> 10 ))
  69. if [[ "$tx_rate" -gt 1048576 ]]; then
  70. printf '%sM' "`echo "scale=1; $tx_kib / 1024" | bc`"
  71. else
  72. echo -n "${tx_kib}K"
  73. fi