Bash: Script to monitor hard disk health and send an e-mail on error.

Here is a simple script to test for reallocated sector count and report through mail if it finds any.
Reallocated sectors means your disk has sectors where it can’t read/write thus it uses a reserved space to reallocate those sectors.
In general reallocated sectors are bad news for your disk and you should consider a backup and send to warranty.
The script reads the smart value and reports. It should be self explanatory to use it just type ./scriptname.sh nameofthehost. Afterwards set a cron to run daily.
Credits to Thorsten Rhau for the base script.

#!/bin/bash
# This script checks the health of disks

#Define a friendly name for this machine
mname="$1"

#Location to temporary store the error log
logloc="/root/scripts"
#Set default for not sendig mails
sendm="0"

# Disks to check
disks="/dev/sda
/dev/sdb"

# Setting up path
PATH="$PATH:/usr/bin:/usr/sbin"

# variable containing all needed commands
needed_commands="smartctl awk mail"

# Checking if all needed programs are available on system
for command in $needed_commands
do
  if ! hash "$command" > /dev/null 2>&1
  then
    echo "$command not found on system" 1>&2
    exit 1
  fi
done

# Checking disk
for disk in $disks
do
  # Creating a array with results
  declare -a status=(`smartctl -a -d ata $disk | awk '/Reallocated_Sector_Ct/ || /Seek_Error_Rate/ { print $2" "$NF }'`)
  # Checking that we do not have any Reallocated Sectors
  if [ "${status[1]}" -ne 0 ]
  then
    echo "$mname Warning: Disk $disk has errors! ${status[0]} ${status[1]} ${status[2]} ${status[3]}. Following complete smartctl output." >> diskerror.log
    smartctl -a -d ata $disk >> $logloc/diskerror.log
    failed=("${failed[@]}" "$disk")
    sendm="1"
  fi
done

#Send an e-mail if needed containing the failed diks (fdisks) info.
if [ $sendm == 1 ]; then
  fdisks=${failed[@]}
  mail -s "$mname Disks $fdisks are about to fail." user@localhost < $logloc/diskerror.log
  rm -rf $logloc/diskerror.log
fi
Advertisements

#bash-2, #disk, #hard-disk, #health, #mail-2, #s-m-a-r-t, #script, #sda, #smart, #smartctl