bandwidth2 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2015 James Murphy
  4. # Licensed under the terms of the GNU GPL v2 only.
  5. #
  6. # i3blocks blocklet script to monitor bandwidth usage
  7. iface="${BLOCK_INSTANCE}"
  8. iface="${IFACE:-$iface}"
  9. dt="${DT:-3}"
  10. unit="${UNIT:-MB}"
  11. LABEL="${LABEL:-<span font='FontAwesome'> </span>}" # down arrow up arrow
  12. printf_command="${PRINTF_COMMAND:-"printf \"${LABEL}%1.0f/%1.0f %s/s\\n\", rx, wx, unit;"}"
  13. function default_interface {
  14. ip route | awk '/^default via/ {print $5; exit}'
  15. }
  16. function check_proc_net_dev {
  17. if [ ! -f "/proc/net/dev" ]; then
  18. echo "/proc/net/dev not found"
  19. exit 1
  20. fi
  21. }
  22. function list_interfaces {
  23. check_proc_net_dev
  24. echo "Interfaces in /proc/net/dev:"
  25. grep -o "^[^:]\\+:" /proc/net/dev | tr -d " :"
  26. }
  27. while getopts i:t:u:p:lh opt; do
  28. case "$opt" in
  29. i) iface="$OPTARG" ;;
  30. t) dt="$OPTARG" ;;
  31. u) unit="$OPTARG" ;;
  32. p) printf_command="$OPTARG" ;;
  33. l) list_interfaces && exit 0 ;;
  34. h) printf \
  35. "Usage: bandwidth3 [-i interface] [-t time] [-u unit] [-p printf_command] [-l] [-h]
  36. Options:
  37. -i\tNetwork interface to measure. Default determined using \`ip route\`.
  38. -t\tTime interval in seconds between measurements. Default: 3
  39. -u\tUnits to measure bytes in. Default: Mb
  40. \tAllowed units: Kb, KB, Mb, MB, Gb, GB, Tb, TB
  41. \tUnits may have optional it/its/yte/ytes on the end, e.g. Mbits, KByte
  42. -p\tAwk command to be called after a measurement is made.
  43. \tDefault: printf \"<span font='FontAwesome'> </span>%%-5.1f/%%5.1f %%s/s\\\\n\", rx, wx, unit;
  44. \tExposed variables: rx, wx, tx, unit, iface
  45. -l\tList available interfaces in /proc/net/dev
  46. -h\tShow this help text
  47. " && exit 0;;
  48. esac
  49. done
  50. check_proc_net_dev
  51. iface="${iface:-$(default_interface)}"
  52. while [ -z "$iface" ]; do
  53. echo No default interface
  54. sleep "$dt"
  55. iface=$(default_interface)
  56. done
  57. case "$unit" in
  58. Kb|Kbit|Kbits) bytes_per_unit=$((1024 / 8));;
  59. KB|KByte|KBytes) bytes_per_unit=$((1024));;
  60. Mb|Mbit|Mbits) bytes_per_unit=$((1024 * 1024 / 8));;
  61. MB|MByte|MBytes) bytes_per_unit=$((1024 * 1024));;
  62. Gb|Gbit|Gbits) bytes_per_unit=$((1024 * 1024 * 1024 / 8));;
  63. GB|GByte|GBytes) bytes_per_unit=$((1024 * 1024 * 1024));;
  64. Tb|Tbit|Tbits) bytes_per_unit=$((1024 * 1024 * 1024 * 1024 / 8));;
  65. TB|TByte|TBytes) bytes_per_unit=$((1024 * 1024 * 1024 * 1024));;
  66. *) echo Bad unit "$unit" && exit 1;;
  67. esac
  68. scalar=$((bytes_per_unit * dt))
  69. init_line=$(cat /proc/net/dev | grep "^[ ]*$iface:")
  70. if [ -z "$init_line" ]; then
  71. echo Interface not found in /proc/net/dev: "$iface"
  72. exit 1
  73. fi
  74. init_received=$(awk '{print $2}' <<< $init_line)
  75. init_sent=$(awk '{print $10}' <<< $init_line)
  76. (while true; do cat /proc/net/dev; sleep "$dt"; done) |\
  77. stdbuf -oL grep "^[ ]*$iface:" |\
  78. awk -v scalar="$scalar" -v unit="$unit" -v iface="$iface" '
  79. BEGIN{old_received='"$init_received"';old_sent='"$init_sent"'}
  80. {
  81. received=$2
  82. sent=$10
  83. rx=(received-old_received)/scalar;
  84. wx=(sent-old_sent)/scalar;
  85. tx=rx+wr;
  86. old_received=received;
  87. old_sent=sent;
  88. if(rx >= 0 && wx >= 0){
  89. '"$printf_command"';
  90. fflush(stdout);
  91. }
  92. }
  93. '