Skip to content

Instantly share code, notes, and snippets.

@remyj38
Forked from cfazendin/emailStatusReport.sh
Last active June 1, 2018 17:17
Show Gist options
  • Save remyj38/1ed0e03c7bdddb299f0a5ccb35bb413e to your computer and use it in GitHub Desktop.
Save remyj38/1ed0e03c7bdddb299f0a5ccb35bb413e to your computer and use it in GitHub Desktop.
Email reporting for disk health (SMART)
#!/bin/sh
### Parameters ###
smartctl="/usr/sbin/smartctl"
logfile="/tmp/smart_report.tmp"
email="your_email@your_domain.com"
subject="SMART Status Report for `hostname -f`"
drives="sda sdb"
tempWarn=40
tempCrit=45
sectorsCrit=10
warnSymbol="?"
critSymbol="!"
### Set email body ###
echo "<h2>$subject</h2>"
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 |Seek |Total |High |Command|"
echo "| | | |On |Stop |Retry|Sectors|Pending|Uncorrec|Errors|Seeks |Fly |Timeout|"
echo "| | | |Hours|Count|Count| |Sectors|Sectors | | |Writes|Count |"
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 warnSymbol=$warnSymbol -v critSymbol=$critSymbol '\
/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} \
/Seek_Error_Rate/{seekErrors=("0x" substr($10,3,4));totalSeeks=("0x" substr($10,7))} \
/High_Fly_Writes/{hiFlyWr=$10} \
/Command_Timeout/{cmdTimeout=$10} \
END {
if (temp > tempCrit || reAlloc > sectorsCrit || pending > sectorsCrit || offlineUnc > sectorsCrit)
device=device " " critSymbol;
else if (temp > tempWarn || reAlloc > 0 || pending > 0 || offlineUnc > 0)
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|%10s|%6s|%7s|\n",
device, serial, temp, onHours, startStop, spinRetry, reAlloc, pending, offlineUnc, \
seekErrors, totalSeeks, hiFlyWr, cmdTimeout;
}'
) >> $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}'`
model=`$smartctl -i /dev/$drive | grep "Device Model" | cut -d':' -f2 | sed -e 's/^[ \t]*//g'`
serial=`$smartctl -i /dev/$drive | grep "Serial Number" | awk '{print $3}'`
(
echo ""
echo "########## SMART status report for $drive drive ($brand $model: $serial) ##########"
$smartctl -n never -H -A -l error /dev/$drive
$smartctl -n never -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 ###
cat $logfile | mail -h "Content-Type: text/html" -s "$subject" $email
rm $logfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment