memory 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh -e
  2. #
  3. # mem_available: grab the current memory 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. __memory_detail() {
  22. free
  23. }
  24. __memory() {
  25. local free="" total="" buffers="" cached=""
  26. local kb_main_used=0 buffers_plus_cached=0 fo_buffers=0 fo_cached=0
  27. if [ -r /proc/meminfo ]; then
  28. while read tok val unit; do
  29. case "$tok" in
  30. MemTotal:) total=${val};;
  31. MemFree:) free=${val};;
  32. Buffers:) buffers=${val};;
  33. Cached:) cached=${val};;
  34. esac
  35. [ -n "${free}" -a -n "${total}" -a -n "${buffers}" -a -n "${cached}" ] && break;
  36. done < /proc/meminfo
  37. elif eval $BYOBU_TEST vm_stat >/dev/null 2>&1; then
  38. # MacOS support
  39. # calculation borrowed from http://apple.stackexchange.com/a/48195/18857
  40. free_blocks=$(vm_stat | grep free | awk '{ print $3 }' | sed -e 's/\.//')
  41. inactive_blocks=$(vm_stat | grep inactive | awk '{ print $3 }' | sed -e 's/\.//')
  42. speculative_blocks=$(vm_stat | grep speculative | awk '{ print $3 }' | sed -e 's/\.//')
  43. free=$((($free_blocks+speculative_blocks)*4))
  44. inactive=$(($inactive_blocks*4))
  45. total=$((($free+$inactive)))
  46. buffers=0
  47. cached=0
  48. fi
  49. kb_main_used=$(($total-$free))
  50. buffers_plus_cached=$(($buffers+$cached))
  51. # "free output" buffers and cache (output from 'free')
  52. fo_buffers=$(($kb_main_used - $buffers_plus_cached))
  53. fpdiv $((100*${fo_buffers})) "${total}" 0;
  54. usage=${_RET}
  55. if [ $total -ge 1048576 ]; then
  56. fpdiv "$total" 1048567 1
  57. total=${_RET}
  58. unit="$ICON_GB"
  59. elif [ $total -ge 1024 ]; then
  60. fpdiv "$total" 1024 0
  61. total=${_RET}
  62. unit="$ICON_MB"
  63. else
  64. unit="$ICON_KB"
  65. fi
  66. if [ $usage -gt 90 ]; then
  67. # Change foreground to yellow, if usage over 90%
  68. fg="Y"
  69. else
  70. fg="W"
  71. fi
  72. [ -n "$total" ] || return
  73. color b g W; printf "%s" "$total"; color -; color g W; printf "%s" "$unit"; color -; color b g "$fg"; printf "%s" "$usage"; color -; color g "$fg"; printf "%s" "$PCT"; color --
  74. }
  75. # vi: syntax=sh ts=4 noexpandtab