30-zpool-bar 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/bash
  2. # config
  3. max_usage=90
  4. bar_width=50
  5. # colors
  6. white="\e[39m"
  7. green="\e[1;32m"
  8. red="\e[1;31m"
  9. dim="\e[2m"
  10. undim="\e[0m"
  11. # zpool status
  12. printf "\nzpool status:\n"
  13. zpool status -x | sed -e 's/^/ /'
  14. # zpool usage
  15. mapfile -t zpools < <(zpool list -Ho name,cap,size)
  16. printf "\nzpool usage:\n"
  17. for line in "${zpools[@]}"; do
  18. # get zpool usage
  19. usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
  20. used_width=$((($usage*$bar_width)/100))
  21. # color is green if usage < max_usage, else red
  22. if [ "${usage}" -ge "${max_usage}" ]; then
  23. color=$red
  24. else
  25. color=$green
  26. fi
  27. # print green/red bar until used_width
  28. bar="[${color}"
  29. for ((i=0; i<$used_width; i++)); do
  30. bar+="="
  31. done
  32. # print dimmmed bar until end
  33. bar+="${white}${dim}"
  34. for ((i=$used_width; i<$bar_width; i++)); do
  35. bar+="="
  36. done
  37. bar+="${undim}]"
  38. # print usage line & bar
  39. echo "${line}" | awk '{ printf("%-30s%+3s used out of %+5s\n", $1, $2, $3); }' | sed -e 's/^/ /'
  40. echo -e "${bar}" | sed -e 's/^/ /'
  41. done