network 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/sh -e
  2. #
  3. # network: calculate the network up/down rates
  4. #
  5. # Copyright (C) 2008 Canonical Ltd.
  6. # Copyright (C) 2011-2014 Dustin Kirkland
  7. #
  8. # Authors: Dustin Kirkland <kirkland@byobu.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. __network_detail() {
  22. get_network_interface; local interface="$_RET"
  23. LC_ALL=C /sbin/ip addr show "$interface" | $BYOBU_SED 's/\s*$//'
  24. }
  25. __network() {
  26. get_network_interface; local interface="$_RET"
  27. local x1=0 x2=0 tx1=0 i= t= unit= symbol= cache= rate=
  28. status_freq network
  29. t="$_RET"
  30. # By default, we won't bug the user with the display of network traffic
  31. # below NETWORK_THRESHOLD in kbps; override in $BYOBU_CONFIG_DIR/status
  32. [ -n "$NETWORK_THRESHOLD" ] || NETWORK_THRESHOLD=20
  33. for i in up down; do
  34. unit="kb"
  35. case $i in
  36. up) symbol="$ICON_UP" ;;
  37. down) symbol="$ICON_DN" ;;
  38. esac
  39. cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/network.$i"
  40. [ -r "$cache" ] && read x1 < "$cache" || tx1=0
  41. local iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed
  42. cat /proc/net/dev > "$cache".dev
  43. while read iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed; do
  44. case "$iface" in
  45. ${interface}:)
  46. [ "$i" = "up" ] && x2=${tbytes} || x2=${rbytes}
  47. break;
  48. ;;
  49. ${interface}:*)
  50. # Interface and tbytes got munged together
  51. [ "$i" = "up" ] && x2=${rmulticast##*:} || x2=${iface##*:}
  52. break;
  53. ;;
  54. esac
  55. done < "$cache".dev
  56. printf "%s" "$x2" > "$cache"
  57. rate=$((8*($x2 - $x1) / $t / 1024)) # in kbps
  58. [ "$rate" -lt 0 ] && rate=0
  59. if [ $rate -gt $NETWORK_THRESHOLD ]; then
  60. case "$NETWORK_UNITS" in
  61. bytes)
  62. rate=$(($rate/8))
  63. if [ "$rate" -gt 1048576 ]; then
  64. fpdiv "$rate" 1048576 1
  65. rate=${_RET}
  66. unit="GB/s"
  67. elif [ "$rate" -gt 1024 ]; then
  68. fpdiv "$rate" 1024 1
  69. rate=${_RET}
  70. unit="MB/s"
  71. else
  72. unit="kB/s"
  73. fi
  74. ;;
  75. *)
  76. # Default to bps
  77. # Why 1000 and not 1024? http://en.wikipedia.org/wiki/Data_rate_units
  78. if [ "$rate" -gt 1000000 ]; then
  79. fpdiv "$rate" 1000000 1
  80. rate=${_RET}
  81. unit="Gb"
  82. elif [ "$rate" -gt 1000 ]; then
  83. fpdiv "$rate" 1000 1
  84. rate=${_RET}
  85. unit="Mb"
  86. fi
  87. ;;
  88. esac
  89. [ -n "$rate" ] || continue
  90. color b m w; printf "%s%s" "$symbol" "$rate"; color -; color m w; printf "%s" "$unit"; color --
  91. else
  92. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/network"*
  93. fi
  94. done
  95. }
  96. # vi: syntax=sh ts=4 noexpandtab