Difference between revisions of "SMART Status Report - BASH"

From Michael's Information Zone
Jump to navigation Jump to search
Line 43: Line 43:
 
#!/bin/bash
 
#!/bin/bash
 
##Create list of all possible sata block devices
 
##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
 
##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
 
##records of all tests run. Also prints out a general status of the drives
 
##onto the terminal.
 
##onto the terminal.
while read line
+
for i in {a..z}
 
do
 
do
smartctl -H /dev/$line > /tmp/asdasd
+
smartctl -H /dev/sd"$1" > /tmp/asdasd
 
if grep -q "No such device" /tmp/asdasd; then
 
if grep -q "No such device" /tmp/asdasd; then
 
rm -f /tmp/asdasd
 
rm -f /tmp/asdasd
Line 58: Line 55:
 
rm -f /tmp/asdasd
 
rm -f /tmp/asdasd
 
echo "*****************************************************************************"    >> /tmp/drivereport
 
echo "*****************************************************************************"    >> /tmp/drivereport
echo /dev/$line >> /tmp/drivereport
+
echo /dev/sd"$i" >> /tmp/drivereport
smartctl -H /dev/$line >> /tmp/drivereport
+
smartctl -H /dev/sd$i >> /tmp/drivereport
smartctl -H /dev/$line > /tmp/assessmentstatus
+
smartctl -H /dev/sd$i > /tmp/assessmentstatus
 
if grep -q -i passed /tmp/assessmentstatus; then
 
if grep -q -i passed /tmp/assessmentstatus; then
echo "$line : GOOD"
+
echo "sd$i : GOOD"
 
elif grep -q -i failed /tmp/assessmentstatus; then
 
elif grep -q -i failed /tmp/assessmentstatus; then
echo "$line : $(tput setaf 1)REPLACE$(tput sgr 0)"
+
echo "sd$i : $(tput setaf 1)REPLACE$(tput sgr 0)"
 
else
 
else
echo "$line : UNKOWN STATUS"
+
echo "sd$i : UNKOWN STATUS"
 
fi
 
fi
 
rm -f /tmp/assessmentstatus
 
rm -f /tmp/assessmentstatus
 
echo "Last Smart Test Results" >> /tmp/drivereport
 
echo "Last Smart Test Results" >> /tmp/drivereport
smartctl -a /dev/$line > /tmp/smartoutput
+
smartctl -a /dev/sd$i > /tmp/smartoutput
 
grep offline /tmp/smartoutput >> /tmp/drivereport
 
grep offline /tmp/smartoutput >> /tmp/drivereport
 
rm -f /tmp/smartoutput
 
rm -f /tmp/smartoutput

Revision as of 13:11, 26 January 2017

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.

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 < /tmp/drivelist
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"