Advertisement
zefie

drivecheck

Jan 16th, 2022
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.51 KB | None | 0 0
  1. #!/bin/bash
  2. Z_WORKDIR="$(realpath "$(dirname "${0}")")"
  3. source "${Z_WORKDIR}/ansihelper.sh"
  4.  
  5. drives=($(ls -1 /dev/sd?))
  6.  
  7. function zcmd() {
  8.                if [ -n "${DEBUG}" ]; then
  9.                        echo "-- Executing ${*} ..." >&2
  10.                fi
  11.                "${@}"
  12. }
  13.  
  14. function zsmart() {
  15.                local drive sopts;
  16.                sopts=("${@}")
  17.                drive="${sopts[$((${#sopts[@]}-1))]}"
  18.                unset -v "sopts[$((${#sopts[@]}-1))]"
  19.                case "${drive}" in
  20.                        "/dev/sdi")
  21.                                        sopts+=("-dsat")
  22.                                        ;;
  23.                esac
  24.                zcmd sudo smartctl "${sopts[@]}" "${drive}";
  25. }
  26.  
  27. function zgetsts() {
  28.        local sts;
  29.        sts="$(sudo smartctl -c "${1}" | grep "Self-test execution status" | cut -d'(' -f2 | cut -d')' -f1 | sed 's| ||g')"
  30.        case "${sts}" in
  31.                0)
  32.                        echo "No Self-test in Progress"
  33.                        ;;
  34.                24?)
  35.                        echo "Extended Self-test in Progress (${sts:2:1}0% remaining)"
  36.                        ;;
  37.                *)
  38.                        echo "Unknown Self-test status code ${sts}"
  39.                        ;;
  40.        esac
  41. }
  42.  
  43. function zgetpoh() {
  44.        local poh;
  45.        poh=$(zsmart -A "${1}" | grep Power_On | rev | cut -d'-' -f1 | rev | sed 's| ||g' | cut -d'(' -f1)
  46.        if [ ${poh} -gt 65535 ]; then
  47.                poh="${poh} (SMART Self-test Log POH: $((poh - 65535)))"
  48.        fi
  49.        echo "${poh}"
  50. }
  51.  
  52. function smartctl_all_drives_cond() {
  53.                if [ "${2}"  == "status" ]; then
  54.                        if [ -e "${1}" ]; then
  55.                                d="${1}";
  56.                                str_color "green" "-- Working on ${d} ..." >&2
  57.                                echo ""
  58.                                zsmart -H "${d}"
  59.                                str_color "yellow" "-- Power On Hours: $(str_color "green" "$(zgetpoh "${d}")")"
  60.                                echo ""
  61.                                str_color "yellow" "-- Self-test status: $(str_color "green" "$(zgetsts "${d}")")"
  62.                                echo ""
  63.                                zsmart --log=xselftest "${d}"| grep -A3 "SMART Extended Self-test Log"
  64.                        else
  65.                                for d in "${drives[@]}"; do
  66.                                        str_color "green" "-- Working on ${d} ..." >&2
  67.                                        echo ""
  68.                                        zsmart -H "${d}"
  69.                                        str_color "yellow" "-- Power On Hours: $(str_color "green" "$(zgetpoh "${d}")")"
  70.                                        echo ""
  71.                                        str_color "yellow" "-- Self-test status: $(str_color "green" "$(zgetsts "${d}")")"
  72.                                        echo ""
  73.                                        zsmart --log=xselftest "${d}"| grep -A3 "SMART Extended Self-test Log"
  74.                                done
  75.                        fi
  76.                else
  77.                        if [ -e "${1}" ]; then
  78.                                d="${1}";
  79.                                shift;
  80.                                str_color "green" "-- Working on ${d} ..." >&2
  81.                                echo ""
  82.                                zsmart "${@}" "${d}";
  83.                        else
  84.                                shift;
  85.                                for d in "${drives[@]}"; do
  86.                                        str_color "green" "-- Working on ${d} ..." >&2
  87.                                        echo ""
  88.                                        zsmart "${@}" "${d}";
  89.                                done
  90.                        fi
  91.                fi
  92. }
  93.  
  94. case "${1}" in
  95.        "extended")
  96.                smartctl_all_drives_cond "${2}" --test=long
  97.                ;;
  98.        "short")
  99.                smartctl_all_drives_cond "${2}" --test=short
  100.                ;;
  101.        "rawcmd")
  102.                shift;
  103.                smartctl_all_drives_cond "${@}"
  104.                ;;
  105.  
  106.        "cancel")
  107.                smartctl_all_drives_cond "${2}" -X
  108.                ;;
  109.        "status")
  110.                smartctl_all_drives_cond "${2}" "${1}"
  111.                ;;
  112.        *)
  113.                echo "Usage: ${0} [status|extended|short|cancel] (/dev/drive)"
  114.                exit 1;
  115.                ;;
  116. esac
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement