#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --device /dev/sda" } device="" while [[ $# -gt 0 ]]; do case "$1" in --device) device="${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$device" ]]; then echo "Укажите устройство через --device" >&2 exit 2 fi if [[ ! -e "$device" ]]; then echo "Устройство не найдено: $device" >&2 exit 2 fi if ! command -v smartctl >/dev/null 2>&1; then echo "Не найден smartctl" >&2 exit 3 fi set +e output="$(smartctl -H -A "$device" 2>&1)" rc=$? set -e health="$(grep -E 'SMART overall-health|SMART Health Status' <<<"$output" | tail -n 1 || true)" if [[ -z "$health" ]]; then health="статус здоровья не найден в выводе" fi status=0 if grep -q 'FAILED' <<<"$output"; then status=2 fi if (( rc & 8 )); then status=2 fi if (( rc & 16 )); then status=2 fi if (( rc & 32 )) && (( status == 0 )); then status=1 fi if (( rc != 0 )) && (( status == 0 )); then status=1 fi echo "Устройство: $device" echo "SMART: $health" echo "Код smartctl: $rc" grep -E 'Reallocated_Sector_Ct|Current_Pending_Sector|Offline_Uncorrectable|Temperature_Celsius|Media_Wearout_Indicator' <<<"$output" || true case "$status" in 0) echo "OK" ;; 1) echo "WARNING: есть замечания SMART" ;; *) echo "CRITICAL: диск сообщает о проблеме" ;; esac exit "$status"