bandwidth 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # Copyright (C) 2019 Jesús E. <heckyel@hyperbola.info>
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # Get custom IN and OUT labels if provided by command line arguments
  17. while [[ $# -gt 1 ]]; do
  18. key="$1"
  19. case "$key" in
  20. -i|--inlabel)
  21. INLABEL="$2"
  22. shift;;
  23. -o|--outlabel)
  24. OUTLABEL="$2"
  25. shift;;
  26. esac
  27. shift
  28. done
  29. [[ -z "$INLABEL" ]] && INLABEL="IN "
  30. [[ -z "$OUTLABEL" ]] && OUTLABEL="OUT "
  31. # Use the provided interface, otherwise the device used for the default route.
  32. if [[ -z $INTERFACE ]] && [[ -n $BLOCK_INSTANCE ]]; then
  33. INTERFACE=$BLOCK_INSTANCE
  34. elif [[ -z $INTERFACE ]]; then
  35. # Verify tun, vpn connection
  36. if [[ $(ip route | awk '/^default/ { print $5 ; exit }') = "tun0" ]]; then
  37. INTERFACE=$(ip route | awk -v i=5 -v j=3 'FNR == i {print $j}')
  38. else
  39. INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }')
  40. fi
  41. fi
  42. # Exit if there is no default route
  43. [[ -z "$INTERFACE" ]] && exit
  44. # Issue #36 compliant.
  45. if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || \
  46. { ! [ "$TREAT_UNKNOWN_AS_UP" = "1" ] &&
  47. ! [ "$(cat "/sys/class/net/${INTERFACE}/operstate")" = "up" ]; }
  48. then
  49. echo "$INTERFACE down"
  50. echo "$INTERFACE down"
  51. echo "#FF0000"
  52. exit 0
  53. fi
  54. # path to store the old results in
  55. path="/dev/shm/$(basename "$0")-${INTERFACE}"
  56. # grabbing data for each adapter.
  57. read -r rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes"
  58. read -r tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes"
  59. # get time
  60. time=$(date +%s)
  61. # write current data if file does not exist. Do not exit, this will cause
  62. # problems if this file is sourced instead of executed as another process.
  63. if ! [[ -f "${path}" ]]; then
  64. echo "${time} ${rx} ${tx}" > "${path}"
  65. chmod 0666 "${path}"
  66. fi
  67. # read previous state and update data storage
  68. read -r old < "${path}"
  69. echo "${time} ${rx} ${tx}" > "${path}"
  70. # parse old data and calc time passed
  71. old=(${old//;/ })
  72. time_diff=$(( time - old[0] ))
  73. # sanity check: has a positive amount of time passed
  74. [[ "${time_diff}" -gt 0 ]] || exit
  75. # calc bytes transferred, and their rate in byte/s
  76. rx_diff=$(( rx - old[1] ))
  77. tx_diff=$(( tx - old[2] ))
  78. rx_rate=$(( rx_diff / time_diff ))
  79. tx_rate=$(( tx_diff / time_diff ))
  80. # shift by 10 bytes to get KiB/s. If the value is larger than
  81. # 1024^2 = 1048576, then display MiB/s instead
  82. # incoming
  83. echo -n "$INLABEL"
  84. rx_kib=$(( rx_rate >> 10 ))
  85. if hash bc 2>/dev/null && [[ "$rx_rate" -gt 1048576 ]]; then
  86. printf '%sM' "$(echo "scale=1; $rx_kib / 1024" | bc)"
  87. else
  88. echo -n "${rx_kib}K"
  89. fi
  90. echo -n " "
  91. # outgoing
  92. echo -n "$OUTLABEL"
  93. tx_kib=$(( tx_rate >> 10 ))
  94. if hash bc 2>/dev/null && [[ "$tx_rate" -gt 1048576 ]]; then
  95. printf '%sM\n' "$(echo "scale=1; $tx_kib / 1024" | bc)"
  96. else
  97. echo -n "${tx_kib}K"
  98. fi