Skip to content

Smart script

This script reads the status of the hard disks on my system (defined in array1). It looks for the word "PASSED" in the output of each run of the smartctl command, and reports a success/fail condition accordingly.

#! /bin/bash

declare -a array1
array1=( /dev/hda /dev/hdc /dev/hdd )
outfile=/root/scripts/smartmsg.txt
rm $outfile
problemflag=0

date >> $outfile

# --------------------------------
# check disks for "PASSED" message
# --------------------------------
for element in ${array1[@]}
do
   OUTPUT=`smartctl -H $element | grep PASSED | wc -l`
   if [ $OUTPUT -eq 0 ] ;
   then
        echo "===============================================" >> $outfile
        echo "Problem with $element on `hostname`" >> $outfile
        echo "===============================================" >> $outfile
        /usr/sbin/smartctl -a $element >> $outfile
        problemflag=1
   else
        echo Disc $element is ok. >> $outfile
   fi
done

# --------------------------------
# send good or bad email
# --------------------------------
runstatus=null
if [ $problemflag -eq 0 ] ;
then
        runstatus=NOMINAL
else
        runstatus=PROBLEM
fi

/usr/bin/mail -s "`hostname`: $runstatus" my@emailaddress.com < $outfile

The script writes a temporary file called smartstatus.txt in the same directory where it lives. Yes, it would be a good idea to move the temp file into the /tmp folder. Feel free to do that. :-)