Advertisement
msjche

Network traffic module for dwm

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