TrueNAS - Drives - List Drives Smart Status

This script will send the email with the SMART status of the hard drives.

NOTE: Adjust amount of the hard drives and the email address.

#!/bin/sh
#
### Parameters ###
###
### Script  based on Bruce Allen, Christian Franke, www.smartmontools.org
# Adam Truszkowski Oct 23 2016
### Checking SMART Status and email it. Most important infor at the begining of the email.
#
logfile="/tmp/smart_report.tmp"
#email="$reciepients"
reciepients="my@email1.com.my@email2.com"
email="$reciepients"
##email="my@email.com"
subject="My Storinator Status"
## TO- DO - Loop through HDD's - Listed now
##
##
##
#drives=$(for drive in $(sysctl -n kern.disks); do \
#if [ "$(smartctl -i /dev/${drive} | grep "Serial Number" | awk '{print $3}')" ]
#then printf ${drive}" "; fi done | awk '{for (i=NF; i!=0 ; i--) print $i }')
#
drives="da0 da1 da2 da3 da4 da5 da6 da7 da8 da9 da10 da11 da12 da12 da13 da14 da15 da16 da17 da18 da19 da20 da21 da22 da23 da24 da25 da26 da27 da28 da29 da30 da31 da32 da33 da34 da35 da36 da37 da38 da39 da40 da41 da42 da43 da44 da45 da46 da46 da47 da48 da49 da50 da51 da52 da53 da54 da55 da56 da57 da58 da59"
tempWarn=40
tempCrit=45
sectorsCrit=10
testAgeWarn=1
warnSymbol="?"
critSymbol="!"
 
### Set email headers ###
(
    echo "To: ${email}"
    echo "Subject: ${subject}"
    echo "Content-Type: text/html"
    echo "MIME-Version: 1.0"
    echo -e "\r\n"
) > "$logfile"
 
### Set email body ###
echo "<pre style=\"font-size:14px\">" >> "$logfile"
 
###### summary ######
(
    echo ""
    echo "########## SMART status report summary for all drives ##########"
    echo ""
    echo "+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+"
    echo "|Device|Serial         |Temp|Power|Start|Spin |ReAlloc|Current|Offline |UDMA  |Seek  |High  |Command|Last|"
    echo "|      |               |    |On   |Stop |Retry|Sectors|Pending|Uncorrec|CRC   |Errors|Fly   |Timeout|Test|"
    echo "|      |               |    |Hours|Count|Count|       |Sectors|Sectors |Errors|      |Writes|Count  |Age |"
    echo "+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+"
) >> "$logfile"
for drive in $drives
do
    (
        smartctl -A -i -v 7,hex48 /dev/"$drive" | \
        awk -v device="$drive" -v tempWarn="$tempWarn" -v tempCrit="$tempCrit" -v sectorsCrit="$sectorsCrit" \
        -v testAgeWarn="$testAgeWarn" -v warnSymbol="$warnSymbol" -v critSymbol="$critSymbol" \
        -v lastTestHours="$(smartctl -l selftest /dev/"$drive" | grep "# 1" | awk '{print $9}')" '\
        /Serial Number:/{serial=$3} \
        /Temperature_Celsius/{temp=$10} \
        /Power_On_Hours/{onHours=$10} \
        /Start_Stop_Count/{startStop=$10} \
        /Spin_Retry_Count/{spinRetry=$10} \
        /Reallocated_Sector/{reAlloc=$10} \
        /Current_Pending_Sector/{pending=$10} \
        /Offline_Uncorrectable/{offlineUnc=$10} \
        /UDMA_CRC_Error_Count/{crcErrors=$10} \
        /Seek_Error_Rate/{seekErrors=("0x" substr($10,3,4));totalSeeks=("0x" substr($10,7))} \
        /High_Fly_Writes/{hiFlyWr=$10} \
        /Command_Timeout/{cmdTimeout=$10} \
        END {
            testAge=sprintf("%.0f", (onHours - lastTestHours) / 24);
            if (temp > tempCrit || reAlloc > sectorsCrit || pending > sectorsCrit || offlineUnc > sectorsCrit)
                device=device " " critSymbol;
            else if (temp > tempWarn || reAlloc > 0 || pending > 0 || offlineUnc > 0 || testAge > testAgeWarn)
                device=device " " warnSymbol;
            seekErrors=sprintf("%d", seekErrors);
            totalSeeks=sprintf("%d", totalSeeks);
            if (totalSeeks == "0") {
                seekErrors="N/A";
                totalSeeks="N/A";
            }
            if (hiFlyWr == "") hiFlyWr="N/A";
            if (cmdTimeout == "") cmdTimeout="N/A";
            printf "|%-6s|%-15s| %s |%5s|%5s|%5s|%7s|%7s|%8s|%6s|%6s|%6s|%7s|%4s|\n",
            device, serial, temp, onHours, startStop, spinRetry, reAlloc, pending, offlineUnc, \
            crcErrors, seekErrors, hiFlyWr, cmdTimeout, testAge;
        }'
    ) >> "$logfile"
done
(
    echo "+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+"
    echo ""
    echo ""
) >> "$logfile"
 
###### for each drive ######
for drive in $drives
do
    brand="$(smartctl -i /dev/"$drive" | grep "Model Family" | awk '{print $3, $4, $5}')"
    serial="$(smartctl -i /dev/"$drive" | grep "Serial Number" | awk '{print $3}')"
    (
        echo ""
        echo "########## SMART status report for ${drive} drive (${brand}: ${serial}) ##########"
        smartctl -H -A -l error /dev/"$drive"
        smartctl -l selftest /dev/"$drive" | grep "# 1 \|Num" | cut -c6-
        echo ""
        echo ""
    ) >> "$logfile"
done
sed -i '' -e '/smartctl 6.3/d' "$logfile"
#sed -i '' -e '/Copyright/d' "$logfile"
sed -i '' -e '/=== START OF READ/d' "$logfile"
sed -i '' -e '/SMART Attributes Data/d' "$logfile"
sed -i '' -e '/Vendor Specific SMART/d' "$logfile"
sed -i '' -e '/SMART Error Log Version/d' "$logfile"
echo "</pre>" >> "$logfile"
 
### Send report ###
sendmail -t < "$logfile"
#rm "$logfile"