Biduleohm

SMART report (SATA)

Mar 4th, 2015
10,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.27 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ### Parameters ###
  4. logfile="/tmp/smart_report.tmp"
  5. subject="SMART Status Report for FreeNAS"
  6. drives="da0 da1 da2 da3 da4 da5 da6 da7"
  7. tempWarn=40
  8. tempCrit=45
  9. sectorsCrit=10
  10. testAgeWarn=1
  11. warnSymbol="?"
  12. critSymbol="!"
  13.  
  14. ### Set email headers ###
  15. (
  16.     echo "To: ${email}"
  17.     echo "Subject: ${subject}"
  18.     echo "Content-Type: text/html"
  19.     echo "MIME-Version: 1.0"
  20.     echo -e "\r\n"
  21. ) > "$logfile"
  22.  
  23. ### Set email body ###
  24. echo "<pre style=\"font-size:14px\">" >> "$logfile"
  25.  
  26. ###### summary ######
  27. (
  28.     echo ""
  29.     echo "########## SMART status report summary for all drives ##########"
  30.     echo ""
  31.     echo "+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+"
  32.     echo "|Device|Serial         |Temp|Power|Start|Spin |ReAlloc|Current|Offline |UDMA  |Seek  |High  |Command|Last|"
  33.     echo "|      |               |    |On   |Stop |Retry|Sectors|Pending|Uncorrec|CRC   |Errors|Fly   |Timeout|Test|"
  34.     echo "|      |               |    |Hours|Count|Count|       |Sectors|Sectors |Errors|      |Writes|Count  |Age |"
  35.     echo "+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+"
  36. ) >> "$logfile"
  37. for drive in $drives
  38. do
  39.     (
  40.         smartctl -A -i -v 7,hex48 /dev/"$drive" | \
  41.         awk -v device="$drive" -v tempWarn="$tempWarn" -v tempCrit="$tempCrit" -v sectorsCrit="$sectorsCrit" \
  42.         -v testAgeWarn="$testAgeWarn" -v warnSymbol="$warnSymbol" -v critSymbol="$critSymbol" \
  43.         -v lastTestHours="$(smartctl -l selftest /dev/"$drive" | grep "# 1" | awk '{print $9}')" '\
  44.        /Serial Number:/{serial=$3} \
  45.        /Temperature_Celsius/{temp=$10} \
  46.        /Power_On_Hours/{onHours=$10} \
  47.        /Start_Stop_Count/{startStop=$10} \
  48.        /Spin_Retry_Count/{spinRetry=$10} \
  49.        /Reallocated_Sector/{reAlloc=$10} \
  50.        /Current_Pending_Sector/{pending=$10} \
  51.        /Offline_Uncorrectable/{offlineUnc=$10} \
  52.        /UDMA_CRC_Error_Count/{crcErrors=$10} \
  53.        /Seek_Error_Rate/{seekErrors=("0x" substr($10,3,4));totalSeeks=("0x" substr($10,7))} \
  54.        /High_Fly_Writes/{hiFlyWr=$10} \
  55.        /Command_Timeout/{cmdTimeout=$10} \
  56.        END {
  57.            testAge=sprintf("%.0f", (onHours - lastTestHours) / 24);
  58.            if (temp > tempCrit || reAlloc > sectorsCrit || pending > sectorsCrit || offlineUnc > sectorsCrit)
  59.                device=device " " critSymbol;
  60.            else if (temp > tempWarn || reAlloc > 0 || pending > 0 || offlineUnc > 0 || testAge > testAgeWarn)
  61.                device=device " " warnSymbol;
  62.            seekErrors=sprintf("%d", seekErrors);
  63.            totalSeeks=sprintf("%d", totalSeeks);
  64.            if (totalSeeks == "0") {
  65.                seekErrors="N/A";
  66.                totalSeeks="N/A";
  67.            }
  68.            if (hiFlyWr == "") hiFlyWr="N/A";
  69.            if (cmdTimeout == "") cmdTimeout="N/A";
  70.            printf "|%-6s|%-15s| %s |%5s|%5s|%5s|%7s|%7s|%8s|%6s|%6s|%6s|%7s|%4s|\n",
  71.            device, serial, temp, onHours, startStop, spinRetry, reAlloc, pending, offlineUnc, \
  72.            crcErrors, seekErrors, hiFlyWr, cmdTimeout, testAge;
  73.        }'
  74.     ) >> "$logfile"
  75. done
  76. (
  77.     echo "+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+"
  78.     echo ""
  79.     echo ""
  80. ) >> "$logfile"
  81.  
  82. ###### for each drive ######
  83. for drive in $drives
  84. do
  85.     brand="$(smartctl -i /dev/"$drive" | grep "Model Family" | awk '{print $3, $4, $5}')"
  86.     serial="$(smartctl -i /dev/"$drive" | grep "Serial Number" | awk '{print $3}')"
  87.     (
  88.         echo ""
  89.         echo "########## SMART status report for ${drive} drive (${brand}: ${serial}) ##########"
  90.         smartctl -H -A -l error /dev/"$drive"
  91.         smartctl -l selftest /dev/"$drive" | grep "# 1 \|Num" | cut -c6-
  92.         echo ""
  93.         echo ""
  94.     ) >> "$logfile"
  95. done
  96. sed -i '' -e '/smartctl 6.3/d' "$logfile"
  97. sed -i '' -e '/Copyright/d' "$logfile"
  98. sed -i '' -e '/=== START OF READ/d' "$logfile"
  99. sed -i '' -e '/SMART Attributes Data/d' "$logfile"
  100. sed -i '' -e '/Vendor Specific SMART/d' "$logfile"
  101. sed -i '' -e '/SMART Error Log Version/d' "$logfile"
  102. echo "</pre>" >> "$logfile"
  103.  
  104. ### Send report ###
  105. sendmail -t < "$logfile"
  106. rm "$logfile"
Advertisement
Add Comment
Please, Sign In to add comment