#!/usr/bin/env bash # Ulepszony skrypt sprawdzania kondycji dysków SMART + testy # Poprawiona wersja – bez błędów "local can only be used in a function" # 2025 set -euo pipefail SMARTCTL=$(command -v smartctl) || { echo "Brak smartctl – zainstaluj smartmontools"; exit 1; } # Krytyczne atrybuty declare -A CRITICAL_ATTR=( [5]="Reallocated_Sector_Ct" [9]="Power_On_Hours" [10]="Spin_Retry_Count" [187]="Reported_Uncorrect" [188]="Command_Timeout" [194]="Temperature_Celsius" [196]="Reallocated_Event_Count" [197]="Current_Pending_Sector" [198]="Offline_Uncorrectable" [199]="UDMA_CRC_Error_Count" ) # Kolory RED='\033[31m' GREEN='\033[32m' YELLOW='\033[33m' WHITE='\033[37m' NC='\033[0m' # Pomoc usage() { cat << EOF Użycie: $(basename "$0") [opcja] Opcje: (bez parametru) – sprawdzenie kondycji i atrybutów SMART --test short – uruchom krótki test SMART --test long – uruchom długi test SMART (trwa długo!) --test conveyance – uruchom test conveyance --test abort – przerwij trwające testy --status – pokaż status bieżących testów --help – ta pomoc Uruchamiaj jako root/sudo. EOF exit 1 } # Funkcja sprawdzająca urządzenie check_device() { local dev=$1 local type_opt=$2 echo -e "=== $dev $type_opt ===" # Health if ! health=$("$SMARTCTL" -H $type_opt "$dev" 2>/dev/null | grep "SMART overall-health"); then echo -e "${YELLOW}Nie można odczytać statusu health${NC}" else echo "$health" if echo "$health" | grep -q "PASSED"; then echo -e "${GREEN}Health: OK${NC}" else echo -e "${RED}Health: PROBLEM!${NC}" fi fi # Temperatura if temp=$("$SMARTCTL" -A $type_opt "$dev" 2>/dev/null | awk '/Temperature_Celsius/{print $10}'); then [[ -n $temp ]] && echo "Temperatura: ${temp}°C" [[ $temp -gt 50 ]] && echo -e "${RED}Uwaga: wysoka temperatura!${NC}" fi # Atrybuty local output=$("$SMARTCTL" -A $type_opt "$dev" 2>/dev/null) local problem=0 for id in "${!CRITICAL_ATTR[@]}"; do local line=$(echo "$output" | grep "^[[:space:]]*$id ") if [[ -n $line ]]; then local raw=$(echo "$line" | awk '{print $10}') local name=${CRITICAL_ATTR[$id]} if [[ $id -eq 9 || $id -eq 194 ]]; then echo -e "$dev: ${WHITE}$name = $raw${NC}" elif [[ $raw -gt 0 ]]; then echo -e "$dev: ${RED}$name = $raw (PROBLEM!)$NC" problem=1 else echo -e "$dev: ${GREEN}$name = $raw${NC}" fi fi done (( problem == 1 )) && echo -e "${RED}$dev: Wykryto problemy z atrybutami!${NC}" echo } # Uruchamianie testów run_tests() { local test_type=$1 echo "Uruchamianie testu '$test_type' na wszystkich dyskach..." for dev_line in "${devices[@]}"; do read -r dev type <<< "$dev_line" opt="" # zwykła zmienna, nie local [[ -n $type && $type != "scsi" ]] && opt="-d $type" echo -n "$dev $opt: " if "$SMARTCTL" $opt -t "$test_type" "$dev"; then echo -e "${GREEN}OK${NC}" else echo -e "${RED}BŁĄD${NC}" fi done } # Przerwanie testów abort_tests() { echo "Przerywanie trwających testów..." for dev_line in "${devices[@]}"; do read -r dev type <<< "$dev_line" opt="" [[ -n $type && $type != "scsi" ]] && opt="-d $type" echo -n "$dev $opt: " "$SMARTCTL" $opt -X "$dev" >/dev/null 2>&1 || true echo -e "${YELLOW}wysłano abort${NC}" done } # Status testów show_status() { echo "Status bieżących testów SMART:" for dev_line in "${devices[@]}"; do read -r dev type <<< "$dev_line" opt="" [[ -n $type && $type != "scsi" ]] && opt="-d $type" echo "=== $dev $opt ===" "$SMARTCTL" $opt -l selftest "$dev" 2>/dev/null | tail -20 || echo "Brak informacji o teście" echo done } # Wykrywanie urządzeń mapfile -t devices < <("$SMARTCTL" --scan | awk '{print $1 " " $3}') if [[ ${#devices[@]} -eq 0 ]]; then echo "Nie znaleziono urządzeń wspierających SMART." exit 1 fi # Główna logika case "${1:-}" in "--test") if [[ -z "${2:-}" ]]; then usage; fi case "$2" in short|long|conveyance) run_tests "$2" echo "Testy uruchomione. Sprawdź status: $(basename "$0") --status" ;; abort) abort_tests ;; *) echo "Nieznany typ testu: $2" >&2 usage ;; esac ;; "--status") show_status ;; "--help"|"-h") usage ;; "") echo "Sprawdzanie kondycji dysków SMART ($(date))" echo for dev_line in "${devices[@]}"; do read -r dev type <<< "$dev_line" opt="" [[ -n $type && $type != "scsi" ]] && opt="-d $type" check_device "$dev" "$opt" done echo "Zakończono." ;; *) echo "Nieznana opcja: $1" >&2 usage ;; esac