UPDATE: This code runs the SMART Short test and and after 5 minutes it will email you the results. In the subject line it will say PASSED or PROBLEM.
Notes: If you change this to run the long test then you must change the wait time appropriately. For my Samsung 2TB drives the wait should be a minimum of 255 minutes according to the drive. I took the sleep timer and set it to 5 hours for a long test.
I recommend creating two versions of this, a short and long test version then you can simply call the version you want to run. Here is the short test version.
Call this script as follows: sh /etc/esmart.sh drive
example: sh /etc/esmart.sh /dev/ada0
Code:
#!/usr/local/bin/sh
#
# Place this in /conf/base/etc/
# Call: sh esmart.sh /dev/ada0
# switch1 is the drive to check (passed parameter)
switch1=$1
# This will use the characters after "/dev/" for the temp file names.
# Example: /dev/ada0 becomes coverada0 or cover0ada0 or cover1ada0
# This needs to be done to keep multiple jobs from using the same files.
drv=`echo $switch1 | cut -c6-`
# Variable just so we can add a note that the drive was asleep when the
# application started but is now awake.
c=0
### Run SMART Quick Test
runsmartshort()
{
### If changing to long SMART test, swap the hash marks from the three lines below.
### You may edit the sleep to whatever your drive recommends for the test to finish.
smartctl -t short ${switch1}
# smartctl -t long ${switch1}
echo "Short Test Running, waiting 5 minutes for test to finish."
# echo "Long Test Running, waiting 255 minutes for test to finish."
sleep 300
# sleep 15300
}
### Process to run our check on the drive, setup exclusivly for only "-l error".
# Output cover0
chkdrive()
{
smartctl -n standby -l error -l selftest ${switch1} > /var/cover0${drv}
}
### Process to create the email header
# Input cover1, output cover.
makeheader()
{
(
echo "To: youremail@address.net"
printf "Subject: SMART Drive Results for ${switch1} - " ; cat /var/cover1${drv}
echo " "
) > /var/cover${drv}
}
### Process to create the email header for failure
# Input none, output cover.
makeheaderfailure()
{
(
echo "To: youremail@address.net"
printf "Subject: SMART Drive Results for ${switch1} - PROBLEM"
echo " "
) > /var/cover${drv}
}
### Process for normal results
# Input is cover0, output is cover1
procnormal()
{
### Delete lines 1 through 5 leaving the status returned, cover0 cannot be changed here.
sed '1,5d' /var/cover0${drv} > /var/cover1${drv}
### If the drive was asleep we can add a line so the user knows it was sleeping
if [ $c -eq 1 ]
then
(
echo " "
date
printf "The drive was sleeping and just woke up."
echo " "
) >> /var/cover1${drv}
fi
}
# Process to cleanup our trash files
cleanup()
{
rm /var/cover${drv}
rm /var/cover0${drv}
rm /var/cover1${drv}
}
### Lets test the drive
runsmartshort
### Lets call chkdrive, output is cover0
chkdrive
### If chkdrive returns a value 2 for sleeping then loop
while [ $? -eq "2" ]
do
### Pause the checking of the drive to about once a minute if the drive is not running.
### This can be changed to more or less frequent, it's a personal choice.
sleep 59
c=1
chkdrive
done
### If chkdrive returns a value other than 0 before or after sleeping, error.
if [ $? -ne "0" ]
then
makeheaderfailure
cat /var/cover0${drv} >> /var/cover1${drv}
else
procnormal
makeheader
### Chop off all but the most recent 5 test results
sed '11,40d' /var/cover${drv} > /var/cover1${drv}
fi
sendmail -t < /var/cover1${drv}
### Call Cleanup Process
cleanup
exit 0
Example of the output. I have it set to give you the last 5 tests vice all tests.
Code:
From: root@freenas.local
To: youremail@address.net
Subject: SMART Drive Results for /dev/ada0 - No Errors Logged
SMART Self-test log structure revision number 1
Num Test_Description Status Remaining LifeTime(hours) LBA_of_first_error
# 1 Short offline Completed without error 00% 2646 -
# 2 Extended offline Completed without error 00% 2400 -
# 3 Short offline Completed without error 00% 2168 -
# 4 Extended offline Completed without error 00% 2002 -
# 5 Extended offline Completed without error 00% 1936 -