#!/bin/bash # Script: hdtest.sh # Task: Check the SMART data of a hard disk for errors # Author: Benjamin 'blindcoder' Schieder # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License V2 as published by # the Free Software Foundation. # global variables SCRIPTNAME=$(basename ${0} .sh) EXIT_SUCCESS=0 EXIT_FAILURE=1 EXIT_ERROR=2 EXIT_BUG=10 LOGFILE=/var/log/smart.log # functions function usage { cat >&2 <<-EOF Usage: ${SCRIPTNAME} [ -l ] Examples: ${SCRIPTNAME} /dev/discs/disc0/disc short ${SCRIPTNAME} -l /var/log/hdb.log /dev/hdb long defaults to ${LOGFILE} EOF [[ ${#} -eq 1 ]] && exit ${1} || exit ${EXIT_FAILURE} } # the options -h for help should always be present. Options -v and # -o are examples. -o needs a parameter, indicated by the colon ":" # following in the getopts call while getopts ':hl:' OPTION ; do case ${OPTION} in h) usage ${EXIT_SUCCESS} ;; l) LOGFILE="${OPTARG}" ;; \?) echo "unknown option \"-${OPTARG}\"." >&2 usage ${EXIT_ERROR} ;; :) echo "option \"-${OPTARG}\" requires an argument." >&2 usage ${EXIT_ERROR} ;; *) echo "Impossible error. parameter: ${OPTION}" >&2 usage ${EXIT_BUG} ;; esac done # skip parsed options shift $(( OPTIND - 1 )) # if you want to check for a minimum or maximum number of arguments, # do it here if (( $# != 2 )) ; then echo "${SCRIPTNAME} requires exactly two arguments!" >&2 usage ${EXIT_ERROR} fi disk="${1}" mode="${2}" tmp="`mktemp`" smartctl -t ${mode} -d ata ${disk} | tee -a ${LOGFILE} >${tmp} waittime="`grep -i wait ${tmp} | cut -f3 -d' '`" echo "Sleeping for ${waittime} minutes." | tee -a ${LOGFILE} sleep $(( ${waittime} * 60 )) while smartctl -d ata -a ${disk} | grep remaining ; do echo "Disk not ready yet, sleeping another 30 seconds." | tee -a ${LOGFILE} sleep 30 done IFS=" " read type status online < <( smartctl -d ata -a ${disk} | grep \#\ 1 | sed 's, \+, ,g' | cut -f 2,3,5 ) hronline="" days=$(( ${online} / 24 )) years=$(( ${days} / 365 )) days=$(( ${days} % 365 )) online=$(( ${online} % 24 )) [ ${online} -lt 10 ] && online="0${online}" echo "Status for disk ${disk}:" | tee -a ${LOGFILE} echo " Testtype : ${type}" | tee -a ${LOGFILE} echo " Status : ${status}" | tee -a ${LOGFILE} echo " Online for: ${years} years ${days} days ${online} hours" | tee -a ${LOGFILE} exit ${EXIT_SUCCESS}