SMART Status Report - BASH

From Michael's Information Zone
Revision as of 14:30, 26 April 2017 by Michael.mast (talk | contribs) (→‎First Version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

As I work more and more with direct disk access filesystems (ie ZFS) I find a need to keep tabs on my disks. The following was created as a side project to keep an eye on my home server that have hard to reach drives, so knowing when one is about to fail is important.

For reporting

for i in {a..z}
do
smartctl -H /dev/sd$i > /tmp/asdasd
if grep -q "No such device" /tmp/asdasd; then
rm -f /tmp/asdasd
else
rm -f /tmp/asdasd
echo "*****************************************************************************"	>> /tmp/drivereport
echo /dev/sd$i >> /tmp/drivereport
smartctl -H /dev/sd$i >> /tmp/drivereport
smartctl -H /dev/sd$i > /tmp/assessmentstatus
if grep -q -i passed /tmp/assessmentstatus; then
echo "$line  : GOOD" >> /dev/null
elif grep -q -i failed /tmp/assessmentstatus; then
echo "$line  : $(tput setaf 1)REPLACE$(tput sgr 0)" >> /tmp/drivereport
else 
echo "$line  : UNKOWN STATUS" >> /tmp/drivereport
fi
rm -f /tmp/assessmentstatus
echo "Last Smart Test Results" >> /tmp/drivereport
smartctl -a /dev/sd$i > /tmp/smartoutput
grep offline /tmp/smartoutput >> /tmp/drivereport
rm -f /tmp/smartoutput
fi
done #< /tmp/drivelist
if grep -E -i 'UNKNOWN STATUS|REPLACE' /tmp/drivereport; then
echo "Subject: Drive Health Warning" > /tmp/reportdrivehealth
cat /tmp/drivereport >> /tmp/reportdrivehealth
sendmail -f <some user>@<some domain>.com <your email>@<your domain>.com < /tmp/reportdrivehealth
rm -f /tmp/reportdrivehealth
else
rm -f /tmp/drivereport
fi

First Version

The first version prints out in the terminal

  • The device
  • Whether or not it has passed overall SMART health assessments
  • and all previous test results.

Frankly this is not very elegant and is not presented in a clean manner but it is a start.

#!/bin/bash
##Create list of all possible sata block devices
for i in {a..z}
do
echo sd$i >> /tmp/drivelist
done
##Loop through the list and check to see if the drive exists, then record the SMART status as well as the
##records of all tests run
while read line
do
smartctl -H /dev/$line > /tmp/asdasd
if grep -q "No such device" /tmp/asdasd; then
rm -f /tmp/asdasd
else
rm -f /tmp/asdasd
echo "*****************************************************************************"    >> /tmp/drivereport
echo /dev/$line >> /tmp/drivereport
smartctl -H /dev/$line >> /tmp/drivereport
echo "Last Smart Test Results" >> /tmp/drivereport
smartctl -a /dev/$line > /tmp/smartoutput
grep offline /tmp/smartoutput >> /tmp/drivereport
rm -f /tmp/smartoutput
fi
done < /tmp/drivelist
rm -f /tmp/drivelist
clear
cat /tmp/drivereport
rm -f /tmp/drivereport

Second Version

This one prints out the health general health assessment on the terminal as either passed or failed, but then creates a more "detailed" report in /var/log.

#!/bin/bash
##Create list of all possible sata block devices

##Loop through the list and check to see if the drive exists, then record the SMART status as well as the
##records of all tests run. Also prints out a general status of the drives
##onto the terminal.
for i in {a..z}
do
smartctl -H /dev/sd"$1" > /tmp/asdasd
if grep -q "No such device" /tmp/asdasd; then
rm -f /tmp/asdasd
else
rm -f /tmp/asdasd
echo "*****************************************************************************"    >> /tmp/drivereport
echo /dev/sd"$i" >> /tmp/drivereport
smartctl -H /dev/sd$i >> /tmp/drivereport
smartctl -H /dev/sd$i > /tmp/assessmentstatus
if grep -q -i passed /tmp/assessmentstatus; then
echo "sd$i  : GOOD"
elif grep -q -i failed /tmp/assessmentstatus; then
echo "sd$i  : $(tput setaf 1)REPLACE$(tput sgr 0)"
else
echo "sd$i  : UNKOWN STATUS"
fi
rm -f /tmp/assessmentstatus
echo "Last Smart Test Results" >> /tmp/drivereport
smartctl -a /dev/sd$i > /tmp/smartoutput
grep offline /tmp/smartoutput >> /tmp/drivereport
rm -f /tmp/smartoutput
fi
done
rm -f /tmp/drivelist
##Create the "Full log" and place it in /var/log/ with today's date.
now1=$(date +%m-%d-%y)
cp /tmp/drivereport /var/log/smart_$now1
rm -f /tmp/drivereport
echo "Full report available in /var/log/smart_$now1"

Third Version

for i in {a..z}
do
smartctl -H /dev/sd$i > /tmp/asdasd
if grep -q "No such device" /tmp/asdasd; then
rm -f /tmp/asdasd
else
rm -f /tmp/asdasd
echo "*****************************************************************************"	>> /tmp/drivereport
echo /dev/sd$i >> /tmp/drivereport
smartctl -H /dev/sd$i >> /tmp/drivereport
smartctl -H /dev/sd$i > /tmp/assessmentstatus
if grep -q -i passed /tmp/assessmentstatus; then
echo "$line  : GOOD"
elif grep -q -i failed /tmp/assessmentstatus; then
echo "$line  : $(tput setaf 1)REPLACE$(tput sgr 0)"
else
echo "$line  : UNKOWN STATUS"
fi
rm -f /tmp/assessmentstatus
echo "Last Smart Test Results" >> /tmp/drivereport
smartctl -a /dev/sd$i > /tmp/smartoutput
grep offline /tmp/smartoutput >> /tmp/drivereport
rm -f /tmp/smartoutput
fi
done #< /tmp/drivelist