disk_io 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/sh -e
  2. #
  3. # disk_io: calculate the disk io rate
  4. #
  5. # Copyright (C) 2010 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. __disk_io_detail() {
  22. if eval $BYOBU_TEST iostat >/dev/null; then
  23. iostat -d -m -h
  24. else
  25. printf "%s\n" "Please install iostat if you want detailed information on your disk throughput"
  26. fi
  27. }
  28. getdisk() {
  29. local t=""
  30. if [ -L "${1}" ]; then
  31. t=$($BYOBU_READLINK -f "$1")
  32. else
  33. t="$1"
  34. fi
  35. t="${t##*/}";
  36. [ -h "/sys/block/$t" ] && _RET="$t" || rtrim "$t" "0-9"
  37. }
  38. __disk_io() {
  39. local part= i=
  40. # Default to disk providing /, but let users override with MONITORED_DISK
  41. [ -z "$MONITORED_DISK" ] && mount_point="/" || mount_point="$MONITORED_DISK"
  42. # By default, we won't bug the user with the display of network traffic
  43. # below DISK_IO_THRESHOLD in kB/s; override in $BYOBU_CONFIG_DIR/status
  44. [ -n "$DISK_IO_THRESHOLD" ] || DISK_IO_THRESHOLD=50
  45. case "$mount_point" in
  46. /dev/*) part="${mount_point}";;
  47. *) part=$(awk '$2 == mp { print $1 ; exit(0); }' "mp=$mount_point" /etc/mtab);;
  48. esac
  49. [ -e "$part" ] || return
  50. getdisk "$part"
  51. local disk=${_RET}
  52. local t2=$(date +%s) t1=
  53. for i in "read" "write"; do
  54. local cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/disk.$i"
  55. t1=$(stat -c %Y "$cache") 2>/dev/null || t1=0
  56. local unit="kB/s"
  57. local rate=0 x1=0 x2=0 symbol= unit=
  58. if [ $t2 -le $t1 ]; then
  59. rate=0
  60. else
  61. [ -r "$cache" ] && read x1 < "$cache" || x1=0
  62. local a1= a2= a3= a4= a5= a6= a7= a8= a9= a10=
  63. read a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 other < "/sys/block/$disk/stat"
  64. if [ "$i" = "read" ]; then
  65. symbol="$ICON_RD"
  66. [ -n "$a3" ] && x2="$a3" || x2=0
  67. else
  68. symbol="$ICON_WR"
  69. [ -n "$a7" ] && x2="$a7" || x2=0
  70. fi
  71. printf "%s" "$x2" > "$cache"
  72. rate=$((($x2 - $x1) / ($t2 - $t1) * 512 / 1024))
  73. if [ $rate -lt $DISK_IO_THRESHOLD ]; then
  74. # Below threshold, don't print
  75. continue
  76. elif [ "$rate" -lt 0 ]; then
  77. rate=0
  78. elif [ "$rate" -gt 1048576 ]; then
  79. unit="GB/s"
  80. fpdiv "$rate" 1048576 0
  81. rate=${_RET}
  82. elif [ "$rate" -gt 1024 ]; then
  83. unit="MB/s"
  84. fpdiv "$rate" 1024 0
  85. rate=${_RET}
  86. else
  87. unit="kB/s"
  88. fi
  89. fi
  90. if [ -z "$rate" ] || [ "$rate" = "0" ]; then
  91. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/disk_io"*
  92. else
  93. color b M W; printf "%s%s" "$symbol" "$rate"; color -; color M W; printf "%s" "$unit"; color --
  94. fi
  95. done
  96. }
  97. # vi: syntax=sh ts=4 noexpandtab