Scripts to report SMART, ZPool and UPS status, HDD/CPU T°, HDD identification and backup the config

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Well, @ChiknNutz can you test this:

Replace (line 25):
Code:
echo "<pre style=\"font-size:14px\">" >> "$logfile"

with
Code:
echo "<div style=\"white-space: nowrap; overflow: auto;\"><pre style=\"font-size:14px\">" >> "$logfile"



And (line 103):
Code:
echo "</pre>" >> "$logfile"

with
Code:
echo "</pre></div>" >> "$logfile"
 
Last edited:

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
Perhaps I fat-fingered something, but I rec'd the following error after invoking the script. Note that up until now, it was working fine.

Code:
./smartemail.sh: cannot create  >> /tmp/smart_report.tmp

###### summary ######
(
    echo
    echo ##########: No such file or directory

+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+
|Device|Serial         |Temp|Power|Start|Spin |ReAlloc|Current|Offline |UDMA  |Seek  |High  |Command|Last|
|      |               |    |On   |Stop |Retry|Sectors|Pending|Uncorrec|CRC   |Errors|Fly   |Timeout|Test|
|      |               |    |Hours|Count|Count|       |Sectors|Sectors |Errors|      |Writes|Count  |Age |
+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+
./smartemail.sh: 40: Syntax error: ")" unexpected
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
I guess you copied the "(line xxx)" part. It was here to help you find the line, not to be copied :)

I'll edit to make my post a bit cleaner ;)
 
Last edited:

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
No I didn't copy those parts, just the code. In fact, I didn't copy it at all, I typed it all in (excluding the (line xxx) parts.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
If you're not sure of what you've done wrong just delete the script and re-copy it from the pastebin to be sure to have a clean base. Then do the changes ;)

The simplest method is to delete the old line and copy-paste the new line then check that everything is ok.
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
Here is the complete script with the latest edits.

Code:
#!/bin/sh

### Parameters ###
logfile="/tmp/smart_report.tmp"
email="MYEMAIL@gmail.com"
subject="SMART Status Report for FreeNAS"
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"
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 "<div style=\"white-space:nowrap; overflow: auto;\"">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></div>" >> "$logfile"

### Send report ###
sendmail -t < "$logfile"
rm "$logfile"
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Ok, found the error: you've put ...auto;\"">pre... instead of ...auto;\"><pre... ;)
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
Aw, what's an extra " between friends ;-)
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
Ok so it ran and sent the email but the formatting is no good. This is all that comes thru on my phone and on a PC it is all pushed off WAY WAY to the right (no line breaks).
 

Attachments

  • image.jpeg
    image.jpeg
    73.7 KB · Views: 336

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
In fact it's not an extra " but you replaced a > with a " and a < with a >. that's why the html is broken. Just copy-paste, it'll avoid any hand writing errors ;)
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
Although I've now lost all credibility due to my clear lack of how to properly navigate the shell editor (I used VI for this, which is a lot different that typical windows cut/paste/delete/insert), I did get this working. However, I see no appreciable difference. I did try changing the text size which helped a little (landed on 12px), but the table is still broken up. Perhaps this is partly due to Gmail, but I also sent the email to another email address on my phone with similar results. Honestly, it's not a big deal to view on a PC, was just hoping we could get this figured out for smartphones w/o too much effort.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Yeah vi isn't exactly the simplest editor, that's why I recommend nano ;)

It's broken in the exact same way?

I have some others things to try that might work but I'll see that tomorrow.
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
A couple screenshots from Gmail email app.

image.png image.png
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Ok, thanks, so it's in fact broken in the exact same way. Well, I'll see what I can do ;)
 

1nv1n

Cadet
Joined
Dec 9, 2015
Messages
5
@Bidule0hm thanks a lot for these, I now have my Cron Jobs setup for all of those reports!
I don't really have much scripting experience, but I modified your CPU Temp script to email the CPU & HDD Temperatures -> http://pastebin.com/tBKX6EMp
And I've set it up as a Cron Job to e-mail me once a day in the afternoon.
 
Last edited:

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Thanks ;)

@ChiknNutz Aww, sorry, I totally forgot to see what I can do today, just one (more) very busy day I guess.
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
No worries at all, this is totally a secondary function that would be fun to help get working and ultimately a useful tool I think for many.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Can you replace this line:
Code:
echo "<div style=\"white-space: nowrap; overflow: auto;\"><pre style=\"font-size:14px\">" >> "$logfile"


by this one:
Code:
echo "<div style=\"white-space: nowrap; overflow: auto; border: 1px solid red;\"><pre style=\"font-size:14px\">" >> "$logfile"


and redo a capture on the smartphone please?
 

ChiknNutz

Patron
Joined
Nov 6, 2015
Messages
217
Output from Gmail.

image.png
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Ok, so the div is here and is the right width, why there's no scroll? Ok, let's be a bit more agressive... replace this:
Code:
echo "<div style=\"white-space: nowrap; overflow: auto; border: 1px solid red;\"><pre style=\"font-size:14px\">" >> "$logfile"


by this:
Code:
echo "<div style=\"white-space: nowrap; overflow-x: scroll;\"><pre style=\"font-size:14px\">" >> "$logfile"
 
Top