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

StorageEater

Dabbler
Joined
Sep 17, 2016
Messages
10
I should also mention that I tried using the version of the script that was said to be adaptive earlier on in the thread but the results were the same in that case as well. My iXsystem lists the drives as adax..etc
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Hello and thanks for your reply.

The funny thing is that I don't see a report file showing up at all in the /tmp directory which is probably part of the issue but I wasn't certain if that gets removed immediately or not when the script runs.

I can try changing the shebang and see how it goes.

It all good, I figured I would be asked about the email functionality due to me forgetting to mention that the standard "test" option works fine within the email setup page.

Is the expected behavior of running the script via the ./xyz.sh from within the terminal that an output will appear within that window or will it do nothing and just send a message via email?
The very last line of the script you posted is an 'rm' command that would delete the report file -- but it's commented out. So you should see the report file in the /tmp directory: you can enter ls /tmp to see the contents of the /tmp directory, and cat /tmp/smart_report.tmp should list the contents of the file on your terminal.

If your script isn't creating the report file (/tmp/smart_report.tmp) then something is very wrong...
 

srit

Dabbler
Joined
Nov 29, 2011
Messages
23
@Bidule0hm - Thank you for your work on these... simply awesome effort.

I am having a minor formatting issue with the smart script. my SSD reports POH in xxxh+00m+00.00s. As a result, the emailed table formatting is a bit off:
Code:
########## SMART status report summary for all drives ##########

+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+
|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 |
+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+
|ada0 ?|163603456807  | 25 |161h+00m+00.000s|  |  |  |  |  |  |  N/A|  N/A|  0|  6|
|ada1 ?|WD-WXG1A259PP95| 18 |  83|  4|  0|  0|  0|  0|  0|  N/A|  N/A|  N/A|  3|
|ada2  |WD-WCC4N4KY2PFX| 19 |  115|  9|  0|  0|  0|  0|  0|  N/A|  N/A|  N/A|  1|
|ada3  |WD-WCC4N2KNSYVF| 20 |  115|  9|  0|  0|  0|  0|  0|  N/A|  N/A|  N/A|  1|
+------+---------------+----+-----+-----+-----+-------+-------+--------+------+------+------+-------+----+



########## SMART status report for ada0 drive (SandForce Driven SSDs: 163603456807) ##########
smartctl 6.5 2016-05-07 r4318 [FreeBSD 10.3-STABLE amd64] (local build)

SMART overall-health self-assessment test result: PASSED

ID# ATTRIBUTE_NAME  FLAG  VALUE WORST THRESH TYPE  UPDATED  WHEN_FAILED RAW_VALUE
  5 Retired_Block_Count  0x0032  100  100  000  Old_age  Always  -  0
  9 Power_On_Hours_and_Msec 0x0032  000  100  000  Old_age  Always  -  161h+00m+00.000s
12 Power_Cycle_Count  0x0032  100  100  000  Old_age  Always  -  10
165 Unknown_Attribute  0x0032  100  100  000  Old_age  Always   -  65536
166 Unknown_Attribute  0x0032  100  100  000  Old_age  Always  -  0
167 Unknown_Attribute  0x0032  100  100  000  Old_age  Always  -  0
168 Unknown_Attribute  0x0032  100  100  000  Old_age  Always  -  0
169 Unknown_Attribute  0x0032  100  100  000  Old_age  Always  -  0
170 Reserve_Block_Count  0x0032  100  100  000  Old_age  Always  -  0
171 Program_Fail_Count  0x0032  100  100   000  Old_age  Always  -  0
172 Erase_Fail_Count  0x0032  100  100  000  Old_age  Always  -  0
173 Unknown_SandForce_Attr  0x0032  100  100  000  Old_age  Always  -  0
174 Unexpect_Power_Loss_Ct  0x0032  100  100  000  Old_age  Always  -  1
187 Reported_Uncorrect  0x0032  100  100  000  Old_age  Always  -  0
188 Command_Timeout  0x0032  100  100  000  Old_age  Always  -  0
194 Temperature_Celsius   0x0022  075  057  000  Old_age  Always  -  25 (Min/Max 0/43)
199 SATA_CRC_Error_Count  0x0032  100  100  000  Old_age  Always  -  0
230 Life_Curve_Status  0x0032  100  100  000  Old_age  Always  -  0
232 Available_Reservd_Space 0x0033  100  100  004  Pre-fail  Always  -  100
233 SandForce_Internal  0x0032  100  100  000  Old_age  Always  -  0
234 SandForce_Internal  0x0032  100  100  000  Old_age  Always   -  2
241 Lifetime_Writes_GiB  0x0030  253  253  000  Old_age  Offline  -  1
242 Lifetime_Reads_GiB  0x0030  253  253  000  Old_age  Offline  -  1
244 Unknown_Attribute  0x0032  000  100  000  Old_age  Always  -  0

No Errors Logged

Test_Description  Status  Remaining  LifeTime(hours)  LBA_of_first_error
Short offline  Completed without error  00%  8  -


I'm no Bash expert (barely know enough to be dangerous)... Any suggestions on how best to modify the following line to achieve the desired POH output for my SSD:

/Power_On_Hours/{onHours=$10} \

Thank you in advance.

I had the same formatting issue but am terrible at writing scripts. here is my uneducated way of fixing it manually.
i changed the below to extend the space for the serial numbers temp and Power on hours so things fit in nicely. Temp, just because. the print f line to match the upper and lower groups
Code:
	echo "+------+------------------------+----+-----------------------+-----+-----+-------+-------+--------+------+------+------+-------+----+"
	echo "|Device|Serial				  |Temp|Power On Hours		 |Start|Spin |ReAlloc|Current|Offline |UDMA  |Seek  |High  |Command|Last|"
	echo "|	  |						|	|					   |Stop |Retry|Sectors|Pending|Uncorrec|CRC   |Errors|Fly   |Timeout|Test|"
	echo "|	  |						|	|					   |Count|Count|	   |Sectors|Sectors |Errors|	  |Writes|Count  |Age |"
	echo "+------+------------------------+----+-----------------------+-----+-----+-------+-------+--------+------+------+------+-------+----+"

			printf "|%-6s|%-24s|%4s|%23s|%5s|%5s|%7s|%7s|%8s|%6s|%6s|%6s|%7s|%4s|\n",
	echo "+------+------------------------+----+-----------------------+-----+-----+-------+-------+--------+------+------+------+-------+----+"

i also noticed for my sas drives it was not outputting any info in the drive auto find because the Serial Number field had the n in number set to lower case so i modified the below lines to look for Serial Number or Serial number. this allows it to add all thats needed.

#The Below line adds or to the grep
#if [ "$(smartctl -i /dev/${drive} | grep "Serial number" | awk '{print $3}')" ]
		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 }')

Hope this helps.
 
Last edited:

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
The script should be POSIX compliant so it can be run with any shell but sh is the recommended one.

Is the expected behavior of running the script via the ./xyz.sh from within the terminal that an output will appear within that window or will it do nothing and just send a message via email?

It should not output anything but just send the mail. You can comment out the tmp file rm so you can check it in /tmp/.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,996
When I run ./myscript.sh there is a brief pause as if its doing something but no data is returned either on the screen on via an email.
Then it is likely working fine. If there is an error in the script you will see some messages on the screen. My first thought would be to ensure you have the email setup on FreeNAS properly. You cannot recieve an email if this is not setup properly.
 

Bidule0hm

Server Electronics Sorcerer
Joined
Aug 5, 2013
Messages
3,710
And be sure the email isn't in your spam directory too.
 

StorageEater

Dabbler
Joined
Sep 17, 2016
Messages
10
Thanks all..I found the issue and things are working as expected now.

Embarrassingly, the issue was due to me sending the reports to an email address I hadn't yet setup forwarding on to my primary email. Once I logged into the Inbox of the address I was sending the messages to I saw all the reports staring right at me; all 20 of them! :rolleyes::D
 

Ryan Allen

Explorer
Joined
Oct 11, 2016
Messages
93
Hello all! I have been studying all these threads for a little while now trying to make all this work for me. First off... THANK YOU GUYS! I'm new to FreeNAS and I have been trying it out on a very old rig. I should be getting my Supermicro X11 rig put together by the end of next month! (I'll post that later in another thread)

My question: how to do i change the file name that is emailed to me to show the version

Like this code does:
cp /data/freenas-v1.db /mnt/tank/.scripts/ConfigBackups/`date +%Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4`.db
which looks like this: "20150125_9.3_201501241715"?

I tried to modify the script by adding the BOLD and underlined section like this....
### Parameters ###
logfile="/tmp/config_backup_error.tmp"
tarfile="/tmp/config_backup.tar"
filename="$(date "+FreeNAS_Config_%Y-%m-%d_%H-%M-%S_cat /etc/version | cut -d'-' -f2_cat /etc/version | cut -d'-' -f4")"
..... just part of the script.....

......but it didn't work out.

I'm following the following threads to try and help me out:
The thread your reading from now..
Nightly check of FreeNAS database!
Backup config file every night automatically!

Any help would be great! Thanks for all the help guys!
 

Spearfoot

He of the long foot
Moderator
Joined
May 13, 2015
Messages
2,478
Hello all! I have been studying all these threads for a little while now trying to make all this work for me. First off... THANK YOU GUYS! I'm new to FreeNAS and I have been trying it out on a very old rig. I should be getting my Supermicro X11 rig put together by the end of next month! (I'll post that later in another thread)

My question: how to do i change the file name that is emailed to me to show the version

Like this code does:
cp /data/freenas-v1.db /mnt/tank/.scripts/ConfigBackups/`date +%Y%m%d`_`cat /etc/version | cut -d'-' -f2`_`cat /etc/version | cut -d'-' -f4`.db
which looks like this: "20150125_9.3_201501241715"?

I tried to modify the script by adding the BOLD and underlined section like this....
### Parameters ###
logfile="/tmp/config_backup_error.tmp"
tarfile="/tmp/config_backup.tar"
filename="$(date "+FreeNAS_Config_%Y-%m-%d_%H-%M-%S_cat /etc/version | cut -d'-' -f2_cat /etc/version | cut -d'-' -f4")"
..... just part of the script.....

......but it didn't work out.

I'm following the following threads to try and help me out:
The thread your reading from now..
Nightly check of FreeNAS database!
Backup config file every night automatically!

Any help would be great! Thanks for all the help guys!
There are a zillion different ways to do this stuff... and the Unix Script Magicians seem to take pleasure in writing complex and obscure code. :)

This is the script I use to backup my configuration file, derived in great part from earlier posts here on this thread. It includes the hostname, complete FreeNAS version, and date. For host 'wilbur', the files end up named like this: wilbur-FreeNAS-9.10.2-U1-86c7ef5-20170228234500.db
Code:
#!/bin/bash

# The source FreeNAS configuration database file:

SRC_FILE="/data/freenas-v1.db"

# Form the destination filename:

DEST_DIR="/mnt/tank/sysadmin/config"
DEST_HOST=$(hostname -s)
DEST_VERSION=$(cat /etc/version | sed -e 's/ /-/' | sed -e 's/(//' | sed -e 's/)//')
DEST_DATE=$(date +%Y%m%d%H%M%S)
DEST_FILE="$DEST_HOST"-"$DEST_VERSION"-"$DEST_DATE".db

echo "Backup configuration database file: $DEST_FILE"

# Copy the source to the destination:

cp "$SRC_FILE" "$DEST_DIR"/"$DEST_FILE"
 

Ryan Allen

Explorer
Joined
Oct 11, 2016
Messages
93
Thanks guys for the quick responses!
I see thoses scripts but don't know how to integrate them into the script I'm using now (it checks the integrity of the confit file, makes a .tar file, then emails it to me). I just wanted to change the file name to include the complete version and build.

Sorry if I'm just missing what you guys are trying to show me.
 

Ryan Allen

Explorer
Joined
Oct 11, 2016
Messages
93
There are a zillion different ways to do this stuff... and the Unix Script Magicians seem to take pleasure in writing complex and obscure code. :)

This is the script I use to backup my configuration file, derived in great part from earlier posts here on this thread. It includes the hostname, complete FreeNAS version, and date. For host 'wilbur', the files end up named like this: wilbur-FreeNAS-9.10.2-U1-86c7ef5-20170228234500.db
Code:
#!/bin/bash

# The source FreeNAS configuration database file:

SRC_FILE="/data/freenas-v1.db"

# Form the destination filename:

DEST_DIR="/mnt/tank/sysadmin/config"
DEST_HOST=$(hostname -s)
DEST_VERSION=$(cat /etc/version | sed -e 's/ /-/' | sed -e 's/(//' | sed -e 's/)//')
DEST_DATE=$(date +%Y%m%d%H%M%S)
DEST_FILE="$DEST_HOST"-"$DEST_VERSION"-"$DEST_DATE".db

echo "Backup configuration database file: $DEST_FILE"

# Copy the source to the destination:

cp "$SRC_FILE" "$DEST_DIR"/"$DEST_FILE"

By the way, I like your script layout! VERY easy to understand and very descriptive! Kind of like back in high school where I had to SHOW how I came up with my answers in Math!! LOL :D
 

Ruff.Hi

Patron
Joined
Apr 21, 2015
Messages
271
Am I right in thinking that creating a config file doesn't grab your scripts? And if you need to reinstall FreeNAS you can use your config file to get your config back ... but it won't include your scripts?
 

Ericloewe

Server Wrangler
Moderator
Joined
Feb 15, 2014
Messages
20,194
Am I right in thinking that creating a config file doesn't grab your scripts? And if you need to reinstall FreeNAS you can use your config file to get your config back ... but it won't include your scripts?
The scripts should be on the data pool, so it's a non-issue.
 

Ruff.Hi

Patron
Joined
Apr 21, 2015
Messages
271
errrr ... unless I've stored them on the OS drive. Which I might have done. I'll check.
 

Ryan Allen

Explorer
Joined
Oct 11, 2016
Messages
93
Would this script work? I want it to verify that the config file it will email me is good before it sends it on its way!
I want to make sure it sends it with the filename like this:
MY HOST-FreeNAS-9.10.2-U1-86c7ef5-20170228234500.db

Code:
#!/bin/sh
### Parameters ###
logfile="/tmp/config_backup_error.tmp"
tarfile="/tmp/config_backup.tar"
DEST_HOST=$(hostname -s)
DEST_VERSION=$(cat /etc/version | sed -e 's/ /-/' | sed -e 's/(//' | sed -e 's/)//')
DEST_DATE=$(date +%Y%m%d%H%M%S)
filename="$DEST_HOST"-"$DEST_VERSION"-"$DEST_DATE"
email="MY EMAIL ADDRESS HERE@gmail.com"
subject="Config Backup for FreeNAS"
if [ "$(sqlite3 /data/freenas-v1.db "pragma integrity_check;")" == "ok" ]
then
### Send config backup ###
	cp /data/freenas-v1.db "/tmp/${filename}.db"
	md5 "/tmp/${filename}.db" > /tmp/config_backup.md5
	sha256 "/tmp/${filename}.db" > /tmp/config_backup.sha256
	cd "/tmp/"; tar -cf "${tarfile}" "./${filename}.db" ./config_backup.md5 ./config_backup.sha256; cd -
	uuencode "${tarfile}" "${filename}.tar" | mail -s "${subject}" "${email}"
	rm "/tmp/${filename}.db"
	rm /tmp/config_backup.md5
	rm /tmp/config_backup.sha256
	rm "${tarfile}"
else
### Send error message ###
	(
		echo "To: ${email}"
		echo "Subject: ${subject}"
		echo "Content-Type: text/html"
		echo "MIME-Version: 1.0"
		echo -e "\r\n"
		echo "<pre style=\"font-size:14px\">"
		echo ""
		echo "Automatic backup of FreeNAS config failed."
		echo ""
		echo "The config file is corrupted!"
		echo ""
		echo "You should correct this problem as soon as possible."
		echo ""
		echo "</pre>"
	) >> "${logfile}"
	sendmail -t < "${logfile}"
	rm "${logfile}"
fi
 

Ruff.Hi

Patron
Joined
Apr 21, 2015
Messages
271
I'm in the process of moving my scripts to my pool. It means that my CRON command has to be something like ...

Code:
./../mnt/<pool>/FreeNAS/Scripts/blah.sh

I've also included a 'cd' at the top of my scripts to change the 'active' directory to that scripts directory - then the tmp and Data directories hang off the script directory.

However, that 'cd' means that it throws an error if I execute the script from the CLI. I got around that by wrapping the 'cd' command in an IF statement that checks if that directory (../mnt/<pool>/FreeNAS/Scripts) exists.

Further fun and games (including some comments on some posts over the last few pages) ... the script that reports the GPTID didn't like one of the geom ...

glabel: No such geom: ada1p2.

That was because there wasn't a 2nd partition for that particular drive (the p2 part). Changing it to P1 (ie ada1p1) resulted in no error.

I fudged a test to see if I should be using adaXp1 or adaXp2 ... but it isn't very generic. Is there a way to tell if the 2nd partition exists?

Finally, SSDs do report temps via SMART ... but the grep part (grep "Temperature_Celsius") might not use those exact letters. For example, my Samsung SSD reports temp as something like "Air Flow Temp" ... so you need to modify the grep part.

Second finally, is there a way of being able to tell a SSD from a HDD?

Edit: See here ... http://unix.stackexchange.com/quest...-disk-is-compact-flash-or-standard-hard-drive

Code:
Rotational=`smartctl -a /dev/adaX | grep "Rotation" | awk '{print $3}'`
if [ $Rotational == "Solid" ]
then
   isSSD=1
   isHDD=0
else
   isSSD=0
   isHDD=1
fi
 
Last edited:

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,996
Second finally, is there a way of being able to tell a SSD from a HDD?
I'm not aware of a way to use smartctl or camcontrol to return any definitive value since I have not been able to find a value that specifically can identify a device as a SSD or HDD.

My suggestion is for the script to be customized to your situation by using either "smartctl" or "camcontrol identify" and then you search for a unique piece of text to identify the drive as SSD. For example on one of my SSDs it have a field called "Rotation Rate: Solid State Drive" so I can use the folowing command smartctl -x /dev/ada0 | grep "Rotation" to get a result back on my SSD, however my WD Reds do not have this value at all and returns a nothing. You can use anything to identify the difference so long as it's unique to the SSD and if you have several different SSDs in the system, then you might find a common item or you might need to find something for each if they are different models/brands.
 

Ruff.Hi

Patron
Joined
Apr 21, 2015
Messages
271
Thx Joe. That 'rotation' grep was what I ended up using (see edit on my post). My WD Reds reported 5400 rpm when I grep'd them for rotation.
 
Last edited:

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,996
No problem. I'm sure there is someway to identify the identity of any SSD, I just don't know it yet.
 
Top