BASH script to inventory UNRAID drives for including in other scripts


Recommended Posts

I created a bash script that will loop over the "Tools > Vars" UNRAID web page and build an array list of every drive assigned to UNRAID. You are welcome to use/modify this code as you see fit as long as the credit given at top is included.

 

Parity drives will be listed first, followed by diskx assignments, followed by cache drive(s).

 

# Get a list of UNRAID variables which contains drive information
# Base code by John Bartlett (Support @ Lime-Tech: http://goo.gl/k533BE)
wget --quiet --output-document=/tmp/diskspeedvars.txt http://localhost/Tools/Vars
# Init variables
FoundArray=0
InDiskInfo=0
EndScan=0
i=-1
# List of drive attributes to extract, pipe delimited with leading & trailing pipe
# Array variables will be named "Disk_[attrib]" - for example, "Disk_name" and "Disk_id"
# Remove/Add attributes you want, "device" is required below
Attribs="|id|idx|device|name|size|status|temp|numReads|numWrites|numErrors|format|type|fsSize|fsFree|"

# Loop over the Vars HTML
while read line
do
# Check to see if we're past the drive section
if [[ ${line:0:9} == "[display]" ]]; then
	EndScan=1
fi
if [[ $EndScan == 0 ]]; then
	# Check to see if we've found the start of the drive information
	if [[ $FoundArray -eq 0 ]]; then
		if [[ $line == "[disks] => Array" ]]; then
			FoundArray=1
		fi
	else
		# Check to see if we're at the start of a new drive
		if [[ $InDiskInfo -eq 0 ]]; then
			IsParity2=0
			if [[ ${line:0:7} == "[parity" ]] || [[ ${line:0:5} == "[disk" ]] || [[ ${line:0:6} == "[cache" ]]; then
				InDiskInfo=1
				let i++
			fi
			if [[ ${line:0:7} == "[disk1]" ]]; then
				i=2
			fi
			if [[ ${line:0:9} == "[parity2]" ]]; then
				IsParity2=1
			fi
		fi
		# Check to see if we're at the end of a drive's information
		if [[ $InDiskInfo -eq 1 ]] && [[ $line == ")" ]]; then
			InDiskInfo=0
		fi
		# If we're in the middle of a drive's information, log the data
		if [[ $InDiskInfo -eq 1 ]]; then
			tmp=($line)
			CurrVar=${tmp[0]//[\[\]]/}
			CurrVal=${tmp[2]}
			# If temp attribute, check for strange length and override if the temp is reported as "*" and bash evalutes it strange
			if [[ $CurrVar == "temp" ]] && [[ ${#CurrVal} -gt 3 ]]; then
				CurrVal="N/A"
			fi
			if [[ $Attribs =~ "|$CurrVar|" ]]; then
				if [[ $IsParity2 -eq 0 ]]; then
					eval "tmpDisk_$CurrVar[i]=$CurrVal"
				else
					eval "tmpDisk_$CurrVar[1]=$CurrVal"
				fi
			fi
		fi
	fi
fi
done < /tmp/diskspeedvars.txt
rm /tmp/diskspeedvars.txt

# Rebuild Arrays to remove empty drive assignments
AttribArray=(${Attribs//|/ })
q=-1
for (( d=0; d <= $i; d++ ))
do
if [[ ${tmpDisk_device[d]} != "" ]]; then
	let q++
	for f in "${AttribArray[@]}"
	do
		eval "Disk_$f[$q]=\${tmpDisk_$f[$d]}"
	done
fi
done

UNRAIDDrives=$q

# Example report
for (( d=0; d <= $UNRAIDDrives; d++ ))
do
echo "${Disk_name[d]} (${Disk_device[d]}) ${Disk_id[d]}  Temp: ${Disk_temp[d]}"
done

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.