_memory 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. TYPE="${BLOCK_INSTANCE:-mem}"
  14. awk -v type=$TYPE '
  15. /^MemTotal:/ {
  16. mem_total=$2
  17. }
  18. /^MemFree:/ {
  19. mem_free=$2
  20. }
  21. /^Buffers:/ {
  22. mem_free+=$2
  23. }
  24. /^Cached:/ {
  25. mem_free+=$2
  26. }
  27. /^SwapTotal:/ {
  28. swap_total=$2
  29. }
  30. /^SwapFree:/ {
  31. swap_free=$2
  32. }
  33. END {
  34. if (type == "swap") {
  35. free=swap_free/1024/1024
  36. used=(swap_total-swap_free)/1024/1024
  37. total=swap_total/1024/1024
  38. } else {
  39. free=mem_free/1024/1024
  40. used=(mem_total-mem_free)/1024/1024
  41. total=mem_total/1024/1024
  42. }
  43. pct=0
  44. if (total > 0) {
  45. pct=(used/total)*100
  46. }
  47. # full text
  48. printf("%.1fG/%.1fG (%.f%%)\n", total, used, pct)
  49. # printf("%.1fG/%.1fG (%.f%%)\n", used, total, pct)
  50. # short text
  51. printf("%.f%%\n", pct)
  52. # color
  53. if (pct > 90) {
  54. print("#FF0000")
  55. } else if (pct > 80) {
  56. print("#FFAE00")
  57. } else if (pct > 70) {
  58. print("#FFF600")
  59. }
  60. }
  61. ' /proc/meminfo