disk 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/sh -e
  2. #
  3. # disk: print the current disk space and usage
  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. __disk_detail() {
  22. df -h -P
  23. }
  24. __disk() {
  25. local out="" MP="" size="" pct="" unit=""
  26. # Default to /, but let users override
  27. [ -z "$MONITORED_DISK" ] && MP="/" || MP="$MONITORED_DISK"
  28. case $MP in
  29. /dev/*) MP=$(awk '$1 == m { print $2; exit(0); }' "m=$MP" /proc/mounts);;
  30. esac
  31. # this could be done faster with 'stat --file-system --format'
  32. # but then we'd have to do blocks -> human units ourselves
  33. out=$({ df -h -P "$MP" 2>/dev/null || df -h "$MP"; } | awk 'END { printf("%s %s", $2, $5); }')
  34. set -- ${out}
  35. size=${1}; pct=${2};
  36. unit=${size#${size%?}} # get the unit (last char)
  37. size=${size%?}; # take the unit off
  38. pct=${pct%?}; # take off the '%'
  39. case "$unit" in
  40. k*|K*) unit="$ICON_KB" ;;
  41. m*|M*) unit="$ICON_MB" ;;
  42. g*|G*) unit="$ICON_GB" ;;
  43. t*|T*) unit="$ICON_TB" ;;
  44. esac
  45. [ -n "$size" ] || return
  46. color b m W; printf "%s" "$size"; color -; color m W; printf "%s" "$unit"; color -;
  47. color b m W; printf "%s" "$pct"; color -; color m W; printf "%s" "$PCT"; color --;
  48. }
  49. # vi: syntax=sh ts=4 noexpandtab