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

Ruff.Hi

Patron
Joined
Apr 21, 2015
Messages
271
thx for the code for changing onHours to Y/M/D/H. I ended up just displaying y and m.
 

melp

Explorer
Joined
Apr 4, 2014
Messages
55
View attachment 19759

67: fi
64: drives=$(for drive in $(sysctl -n kern.disks); do \
Very weird, I can't figure out why it's not matching up the if's and fi's. Honestly, I don't know a whole lot about shell scripting and it's possible I messed something up, but I can't figure out what it might be. If you want to hop on IRC, I can help you troubleshoot it on there.

Paste your scripts into http://www.shellcheck.net. It's a fantastic 'lint' analyzer for shell scripts that I've found invaluable.
I ran my whole script through there and I'm working on correcting stuff, but it didn't come up with anything major.
 

redlancer

Cadet
Joined
Aug 8, 2017
Messages
3
Updated the script: https://github.com/edgarsuit/FreeNAS-Report

Changes:
  • Config backup now attached to report email
  • Added option to turn off config backup
  • Added option to save backup configs in a specified directory
  • Power-on hours in SMART summary table now listed as YY-MM-DD-HH
  • Changed filename of config backup to exclude timestamp (just uses datestamp now)
  • Config backup and checksum files now zipped (was just .tar before; now .tar.gz)
  • Fixed degrees symbol in SMART table (rendered weird for a lot of people); replaced with a *
  • Added switch to enable or disable SSDs in SMART table (SSD reporting still needs work)
  • Added most recent Extended & Short SMART tests in drive details section (only listed one before, whichever was more recent)
  • Reformatted user-definable parameters section
  • Added more general comments to code
TODO:
  • Improve SSD reporting, going to have to add them in a new table
  • Run through shellcheck for general improvements
  • Add support for conveyance SMART tests

Thanks guys, I've been learning a lot running through these scripts and have just about everything working the way I want.

The current version of melp's script I'm getting some errors when I run it in the shell:

Code:
awk: syntax error at source line 19																								 
context is																														 
					switch (powerTimeFormat) >>>  { <<<																			 
awk: illegal statement at source line 20																							
awk: break illegal outside of loops at source line 23


and repeated 6 times (I'm assuming for each of my 6 drives)

The formatted SMART report summary is empty in the emailed output file (but the detail text output is there). Tried a couple different copy/paste (using notepad++) and the script works for the most part, but I can't find where it looks like any syntax errors.

-SJ
 

ArgaWoW

Patron
Joined
Jul 4, 2015
Messages
444
Thanks guys, I've been learning a lot running through these scripts and have just about everything working the way I want.

The current version of melp's script I'm getting some errors when I run it in the shell:

Code:
awk: syntax error at source line 19																								
context is																														
					switch (powerTimeFormat) >>>  { <<<																			
awk: illegal statement at source line 20																							
awk: break illegal outside of loops at source line 23


and repeated 6 times (I'm assuming for each of my 6 drives)

The formatted SMART report summary is empty in the emailed output file (but the detail text output is there). Tried a couple different copy/paste (using notepad++) and the script works for the most part, but I can't find where it looks like any syntax errors.

-SJ
Hello,
I have exactly the same issues. Do you have found a fix ?
 

redlancer

Cadet
Joined
Aug 8, 2017
Messages
3
I'm guessing that your version of awk doesn't seem to like switch statements. I'll update the script to use if/elseif statements instead.

Interesting, had never even heard of awk before now (had to do some quick wikipedia reading :) )

Would it have to do with the FreeNAS version? currently running 9.10.2-U4. Though something as basic as a switch statement you would think wouldn't be a problem.
 

ArgaWoW

Patron
Joined
Jul 4, 2015
Messages
444
Interesting, had never even heard of awk before now (had to do some quick wikipedia reading :) )

Would it have to do with the FreeNAS version? currently running 9.10.2-U4. Though something as basic as a switch statement you would think wouldn't be a problem.
I am on the latest release, so I don't think this will be the reason
 

melp

Explorer
Joined
Apr 4, 2014
Messages
55
Unbeknownst to me before starting all this, awk has to be compiled with a --support-switch flag to include support for switch statements. I'm guessing the awk binary included with FreeNAS didn't have this flag when it was compiled (or something else is going on).

Regardless, I switched it to use if/else statements instead. Updates are on github (including a new flag to change the format of the Power On Hours column based on the comment from Ruff-Hi.

https://github.com/edgarsuit/FreeNAS-Report
 

diedrichg

Wizard
Joined
Dec 4, 2012
Messages
1,319
Regardless, I switched it to use if/else statements instead. Updates are on github (including a new flag to change the format of the Power On Hours column based on the comment from Ruff-Hi.

https://github.com/edgarsuit/FreeNAS-Report
Sweet. I'll give it a go tonight. Btw, do you have the ability to load the versions on github? I didn't keep rv01 which worked well and couldn't revert when rv02 failed to run. Looking forward to running rv03!
 

melp

Explorer
Joined
Apr 4, 2014
Messages
55
You might be able to get to it in the .sh file's history, but I'm not 100% sure. I'm pretty new to git and github. If you're still having issues with that one if statement, you can replace it with a space-separated list of your drives and it should work again (i.e., drives="da0 da1 da2 da3").
 

diedrichg

Wizard
Joined
Dec 4, 2012
Messages
1,319
@melp it's still choking on this section under Auto-generated parameters
Code:
if ([ "$includeSSD" == "true" ]); then
#71  drives=$(for drive in $(sysctl -n kern.disks); do \
  if ([ "$(smartctl -i /dev/"${drive}" | grep "SMART support is: Enabled")" ]); then
  printf "%s " "${drive}";
  fi
#75 done | awk '{for (i=NF; i!=0 ; i--) print $i }')

Code:
FreeNASReport.sh: 75: Syntax error: "done" unexpected (expecting "then")		
FreeNASReport.sh: 71: Syntax error: Error in command substitution

Shellcheck.net says:
Code:
if ([ "$(smartctl -i /dev/"${drive}" | grep "SMART support is: Enabled")" ]); then
..... ^-- SC2143: Use grep -q instead of comparing output with [ -n .. ].
 

melp

Explorer
Joined
Apr 4, 2014
Messages
55
Still no idea what's going on with it. The suggestion shellcheck is giving doesn't have anything to do with the error (it wants you to use if smartctl -i /dev/"${drive}" | grep -q "SMART support is: Enabled"; then for the if statement line, but similar syntax doesn't work for the section below it because of the pipe). If you move the "then" on line 72 to a new line, does that resolve it? That "then" is what it's looking and not finding for some dumb reason.
 

diedrichg

Wizard
Joined
Dec 4, 2012
Messages
1,319
Still no idea what's going on with it.
I got it to pass that section with
Code:
drives="da0 da1 da2 da3";
  if smartctl -i /dev/"${drive}" | grep -q "SMART support is: Enabled"
	 then
  printf "%s " "${drive}";
  fi
  awk '{for (i=NF; i!=0 ; i--) print $i }'

but now it's getting hung up on the "for" line saying "Syntax error: word unexpected"
Code:
poolNum=0
for pool in $pools; do
  # zpool health summary
 

melp

Explorer
Joined
Apr 4, 2014
Messages
55
Glad it's working, but that whole section under drives="da0 da1 da2 da3"; isn't doing anything any more. The then on the same line of the if shouldn't cause issues, so I don't know what the fuck. I'm thinking maybe it was the / after the do?

I'm guessing the next error is because you took out the pools=$(zpool list -H -o name) line at the bottom of that first section as well. Try the following code for the ###### Auto-generated Parameters section. If it works, I'll update the git repo so you can use the main branch again:

Code:
###### Auto-generated Parameters
host=$(hostname -s)
logfile="/tmp/smart_report.tmp"
subject="Status Report and Configuration Backup for ${host}"
boundary="gc0p4Jq0M2Yt08jU534c0p"
if [ "$includeSSD" == "true" ]; then
	drives=$(for drive in $(sysctl -n kern.disks); do
		if [ "$(smartctl -i /dev/"${drive}" | grep "SMART support is: Enabled")" ]; then
			printf "%s " "${drive}"
		fi
	done | awk '{for (i=NF; i!=0 ; i--) print $i }')
else
	drives=$(for drive in $(sysctl -n kern.disks); do
		if [ "$(smartctl -i /dev/"${drive}" | grep "SMART support is: Enabled")" ] && ! [ "$(smartctl -i /dev/"${drive}" | grep "Solid State Device")" ]; then
			printf "%s " "${drive}"
		fi
	done | awk '{for (i=NF; i!=0 ; i--) print $i }')
fi
pools=$(zpool list -H -o name)
 
Last edited:

diedrichg

Wizard
Joined
Dec 4, 2012
Messages
1,319
Okay everybody. Sometimes you just have to stand up and admit your mistakes. I've had all these issues because I was editing the script in Notepad++ and then copying it to FN. That didn't work, something was screwing up between dos and unix. I've since directly copied the report.sh to FN and edited in nano and all is well, the script works perfectly fine. I'm sorry...
 

melp

Explorer
Joined
Apr 4, 2014
Messages
55
Last edited:

Hazimil

Contributor
Joined
May 26, 2014
Messages
172
Okay everybody. Sometimes you just have to stand up and admit your mistakes. I've had all these issues because I was editing the script in Notepad++ and then copying it to FN. That didn't work, something was screwing up between dos and unix. I've since directly copied the report.sh to FN and edited in nano and all is well, the script works perfectly fine. I'm sorry...

This can happen because of the differences in the way Windows/DOS, UNIX/Linux/FreeBSD, and Mac's treat "carriage returns" at the end each line, thankfully Notepad++ is aware of this, so if editing a file which you plan to put on to Linux/FreeBSD, make sure you select the Edit menu, then select EOL Conversion, and then select Unix (LF) before you save it.

More details can be found at: https://forums.freenas.org/index.ph...pc-or-mac-cause-issues-when-copied-to-nas.67/

Then it should work fine.

Jonathan
 
Last edited:

Ruff.Hi

Patron
Joined
Apr 21, 2015
Messages
271
Regardless, I switched it to use if/else statements instead. Updates are on github (including a new flag to change the format of the Power On Hours column based on the comment from Ruff-Hi.

HA. Didn't expect that. And now you find that I have actually changed my report to show Y:MM: DD in the heading and 2:02:15 in the table. Shows the info and remains a relatively thin column.
 
Top