2
0

36-diskstatus 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. # config
  3. MAX_TEMP=40
  4. # set column width
  5. COLUMNS=2
  6. # colors
  7. white="\e[39m"
  8. green="\e[1;32m"
  9. red="\e[1;31m"
  10. dim="\e[2m"
  11. undim="\e[0m"
  12. # disks to check
  13. disks=(sda sdb sdc sdd sde sdf sdg sdi)
  14. disknames=(sda sdb sdc sdd sde sdf sdg sdi)
  15. # hddtemp
  16. hddtemp_host=localhost
  17. hddtemp_port=7634
  18. # logfiles to check
  19. logfiles='/var/log/syslog /var/log/syslog.1'
  20. # get all lines with smartd entries from syslog
  21. lines=$(tac $logfiles | grep -hiP 'smartd\[[[:digit:]]+\]:' | grep -iP "previous self-test")
  22. # use nc to query temps from hddtemp daemon
  23. hddtemp=$(timeout 0.01 nc $hddtemp_host $hddtemp_port | sed 's/|//m' | sed 's/||/ \n/g')
  24. out=""
  25. for i in "${!disks[@]}"; do
  26. disk=${disks[$i]}
  27. # use disknames if given
  28. diskname=${disknames[$i]}
  29. if [ -z "${diskname}" ]; then
  30. diskname=$disk
  31. fi
  32. uuid=$(blkid -s UUID -o value "/dev/${disk}")
  33. status=$( (grep "${uuid}" <<< "${lines}") | grep -m 1 -oP "previous self-test.*" | awk '{ print $4 " " $5 }')
  34. temp=$( (grep "${disk}" <<< "${hddtemp}") | awk -F'|' '{ print $3 }')
  35. # color green if temp <= MAX_TEMP, else red
  36. if [[ "${temp}" -gt "${MAX_TEMP}" ]]; then
  37. color=$red
  38. else
  39. color=$green
  40. fi
  41. # add "C" if temp is numeric
  42. if [[ "$temp" =~ ^[0-9]+$ ]]; then
  43. temp="${temp}C"
  44. fi
  45. # color green if status is "without error", else red
  46. if [[ "${status}" == "without error" ]]; then
  47. status_color=$green
  48. else
  49. status_color=$red
  50. fi
  51. # print temp & smartd error
  52. out+="${diskname}:,${color}${temp}${undim} | ${status_color}${status}${undim},"
  53. # insert \n every $COLUMNS column
  54. if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
  55. out+="\n"
  56. fi
  57. done
  58. out+="\n"
  59. printf "\ndisk status:\n"
  60. printf "$out" | column -ts $',' | sed -e 's/^/ /'