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

Joined
Feb 27, 2015
Messages
4
This is just what I was looking for. I am having a slight issue though, I'm able to see the individual drive information for the SMART script that you wrote, but it won't show me the collective table at the top of the script for some reason. It just comes up blank. I've tried using the dynamic drive name and the static drive names and it comes up the same way each time. Any ideas?
 

flyinfitz1

Explorer
Joined
Mar 29, 2013
Messages
91
This is great thanks!
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Can you post the result of cat -e the_script_name.sh please? The copy-paste from the forum to the script do some very weird things sometimes depending on how you're doing it, the terminal, the text editor, ...

BTW thank you both ;)
 
Joined
Feb 27, 2015
Messages
4
[root@Triforce] /mnt/Baldr/bin# cat -e Script1
#!/bin/sh$
$
### Parameters ###$
logfile="/tmp/smart_report.tmp"$
email="jh***@gmail.com"$
subject="SMART Status Report for FreeNAS"$
drives=`sysctl -n kern.disks | awk '{for (i=NF; i!=0 ; i--) print $i }'`$
tempWarn=40$
tempCrit=45$
sectorsCrit=10$
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 |Seek |Total |High |Command|"$
echo "| | | |On |Stop |Retry|Sectors|Pending|Uncorre c|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" sub str($10,7))} \$
/High_Fly_Writes/{hiFlyWr=$10} \$
/Command_Timeout/{cmdTimeout=$10} \$
END {$
if (temp > tempCrit || reAlloc > sectorsCrit || pending > sectorsCri t || 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, pendin g, 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}'`$
serial=`smartctl -i /dev/${drive} | grep "Serial Number" | awk '{print $3}'` $
($
echo ""$
echo "########## SMART status report for ${drive} drive (${brand}: ${ser ial}) ##########"$
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 ###$
sendmail -t < ${logfile}$
rm ${logfile}$
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Can you post it in between code tags or on a pastebin please? (don't copy the content of the post, re-copy it from the shell)
 

Bidule0hm

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

Ok, I see where the issue is: line 43 is cut at the end and is spread on two lines (43 and 44). If you fix this the script should work fine, I can't see any other problem.

If the line is too long and your editor doesn't like it you can cut it and put it on two lines properly like that:
Code:
awk -v device=${drive} -v tempWarn=${tempWarn} -v tempCrit=${tempCrit} -v sectorsCrit=${sectorsCrit} \
-v warnSymbol=${warnSymbol} -v critSymbol=${critSymbol} '\
 

Bidule0hm

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

I think I'll put the scripts on pastebin, that should avoid some copy-paste problems and as a bonus it'll reduce the first post length :) <-- edit: done :D
 
Last edited:

mjt5282

Contributor
Joined
Mar 19, 2013
Messages
139
nice set of scripts, thanks. What directory do you put them in by default, /usr/local/bin ?

Upon first run, I found a WD Green that had 120 reallocated sectors. I will be keeping an eye on that one!
 

Bidule0hm

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

As I use the root user they are in /root/ because it's the default directory when you connect and it's simple (KISS). And generally on my UNIX based systems I use /home/Myname/scripts/.

I don't recommend to use the */bin/ because you mix your scripts with those of the system. In theory it's not a problem but if you ever edit or delete one of the system script/bin inadvertently you'll don't like the result... And especially on FreeNAS I wouldn't use the */bin/ because of the updates, the /base/* thing, etc. A popular choice on FreeNAS is to use one of the data pool to put your scripts, it's not a bad idea but personally I prefer to separate scripts and data ;)
 
Last edited:

volckg

Dabbler
Joined
Oct 15, 2014
Messages
11
Thank you very much for sharing your scripts. Very useful indeed.
Since I started using Freenas half a year ago, I felt that this kind of reporting was missing from the out-of-the-box-toolkit.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
You're welcome ;)
 
Joined
Jan 21, 2015
Messages
18
Thanks a lot for the scripts! They're amazing and were just what I was searching for.
Very good work here!

I as a FreeNAS starter had some problems with the pastebin.
At first I copy and pasted the output on my Windows machine in a notepad file and sent it over to FreeNAS.
Using putty to ssh to my server then I thought with

chown root:wheel *.sh and
chmod 744 *.sh


I'd be on track.
But manually running the script with
sh smart_report.sh
I always got errors in the lines of the "for" loop.

After realizing that the logfile was not named "smart_report.tmp" but instead "smart_report.tmp^M^M", I guessed it's a DOS vs UNIX formatting problem.
Thus I downloaded the pastebin scripts as files and transferred them directly to the FreeNAS + changing ownership and permission again.
Same result - didn't work.

So I went to typing minimum examples at first and the whole SMART script later.
And it worked without an issue
At last - for the zpool script - I copy-pasted the pastebin output via Ctrl-C (on my windows machine) and inserted via right-clicking into putty/nano.
The script created in this way then worked as well. So that's the way to go via a windows machine...

I guess, it's a fairly noobish-error, but since this is the howto-section I thought my struggle might help someone...
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
Thanks! you're welcome ;)

I think I'll add some basic scripting things in the misc section to avoid this kind of problems ;)

At first I didn't want to add this because I think that if you want to put custom scripts on FreeNAS you should know what you're doing (you can easily damage the system with one command...), but these are pretty harmless scripts so I think even someone with little knowledge on UNIX systems should be able to use them :)

Edit: changes done, I've added a new section "Script basics" ;)
 
Last edited:

seesam

Cadet
Joined
Mar 1, 2015
Messages
1
Thanks for the scripts, they rock...

My take on the ups script.

http://pastebin.com/hWDyJazf
And the output
Code:
UPS report generated:
Time: 2015-03-19 22:26:44

Ups Info:
    UPS Status: OL
    UPS Temp: 25.0 C

Input Output:
    Input Frequency: 50.1 Hz
    Input Voltage: 231.4 V
    Output Voltage: 232.0 V
    Output Load: 22 %

Battery Status:
    Battery Runtime:  305s
    Battery Voltage: 13.60 V
    Battery Charge: 100 %
 
Last edited:

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
You're welcome ;)
 

hyram

Dabbler
Joined
Jan 16, 2015
Messages
10
Two thing I'd like to see added:

1. The last test completed (long, short, conveyance, etc).
2. Time stamp when the last test completed (or stated).

I've figured out how to do #1, but can't figure out any way to get #2.

Anybody got an idea?
 
Joined
Jan 9, 2015
Messages
430
2. Time stamp when the last test completed (or stated).
I don't believe I've every seen that data shown in the output of smartctl -a /dev/xxx. I think the best you will get is the Lifetime(hours) of when the test was run.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
1) It already is here (at the end of each drive blocks) :) (I added it a bit after I posted the first version of the script, use the last version if you have the old version)

2) There is no timestamp but there is already a mean to see when it has been executed: the power on hours at the time of the test is printed on the same line ;)

Example (directly from my last email):
Code:
Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error
Short offline       Completed without error       00%      5822         -
 
Top