Influx/Grafana script after 6.8


Recommended Posts

I've been running a simple script in the custom directory to export Unraid stats to an Influx DB for Grafana.  Upgrading to 6.8 and I knew it would break.  I wanted to find a better way of running the same script and it turned out to be pretty simple.  

 

In Settings and User Scripts, add a new script and include the following:

 

#!/bin/bash
#
# Set Vars
#
DBURL=http://x.x.x.x:8086
DBNAME=dashboard
DEVICE="UNRAID"
CURDATE=`date +%s`


# Current array assignment.
# I could pull the automatically from /var/local/emhttp/disks.ini
# Parsing it wouldnt be that easy though.
DISK_ARRAY=( sdf sde sdj sdq sdn sdg sdh sdp sdl sdm sdd sdb sdc sdi sdk sdo nvme0n1 )
DESCRIPTION=( parity disk1 disk2 disk3 disk4 disk5 disk6 disk7 disk8 disk9 disk10 disk11 disk12 disk13 disk14 disk15 cache )
#
# Added -n standby to the check so smartctl is not spinning up my drives
#
i=0
for DISK in "${DISK_ARRAY[@]}"
do
	smartctl -n standby -A /dev/$DISK | grep "Temperature_Celsius" | awk '{print $10}' | while read TEMP 
	do
		curl -is -XPOST "$DBURL/write?db=$DBNAME" --data-binary "DiskTempStats,DEVICE=${DEVICE},DISK=${DESCRIPTION[$i]} Temperature=${TEMP} ${CURDATE}000000000" >/dev/null 2>&1
	done
	((i++))
done

# Had to increase to 10 samples because I was getting a spike each time I read it.  This seems to smooth it out more
top -b -n 10 -d.2 | grep "Cpu" |  tail -n 1 | awk '{print $2,$4,$6,$8,$10,$12,$14,$16}' | while read CPUusr CPUsys CPUnic CPUidle CPUio CPUirq CPUsirq CPUst
do
	top -bn1 | head -3 | sed -e 's/.*load//g' | awk '/average/ {print $2" "$3" "$4}' | sed -e 's/,//g' | while read LAVG1 LAVG5 LAVG15
	do
		curl -is -XPOST "$DBURL/write?db=$DBNAME" --data-binary "cpuStats,Device=${DEVICE} CPUusr=${CPUusr},CPUsys=${CPUsys},CPUnic=${CPUnic},CPUidle=${CPUidle},CPUio=${CPUio},CPUirq=${CPUirq},CPUsirq=${CPUsirq},CPUst=${CPUst},CPULoadAvg1m=${LAVG1},CPULoadAvg5m=${LAVG5},CPULoadAvg15m=${LAVG15} ${CURDATE}000000000" >/dev/null 2>&1
	done
done

if [[ -f byteCount.tmp ]] ; then

	# Read the last values from the tmpfile - Line "eth0"
	grep "eth0" byteCount.tmp | while read dev lastBytesIn lastBytesOut
	do
		cat /proc/net/dev | grep "eth0" | grep -v "veth" | awk '{print $2, $10}' | while read currentBytesIn currentBytesOut 
		do
			# Write out the current stats to the temp file for the next read
			echo "eth0" ${currentBytesIn} ${currentBytesOut} > byteCount.tmp

			totalBytesIn=`expr ${currentBytesIn} - ${lastBytesIn}`
			totalBytesOut=`expr ${currentBytesOut} - ${lastBytesOut}`

			curl -is -XPOST "$DBURL/write?db=$DBNAME" --data-binary "interfaceStats,Interface=eth0,Device=${DEVICE} bytesIn=${totalBytesIn},bytesOut=${totalBytesOut} ${CURDATE}000000000" >/dev/null 2>&1

		done
	done 

else
    # Write out blank file
	echo "eth0 0 0" > byteCount.tmp
fi

# Gets the stats for boot, disk#, cache, user
#
df | grep "mnt/\|/boot\|docker" | grep -v "user0\|containers" | sed 's/\/mnt\///g' | sed 's/%//g' | sed 's/\/var\/lib\///g'| sed 's/\///g' | while read MOUNT TOTAL USED FREE UTILIZATION DISK
do
	if [ "${DISK}" = "user" ]; then
		DISK="array_total"
	fi
	curl -is -XPOST "$DBURL/write?db=$DBNAME" --data-binary "drive_spaceStats,Device=${DEVICE},Drive=${DISK} Free=${FREE},Used=${USED},Utilization=${UTILIZATION} ${CURDATE}000000000" >/dev/null 2>&1	
done

Make sure to adjust the top couple of variables.  You can then set the timer to whatever you like.  I'm tracking CPU and network so I like to do it every two minutes.  Set the schedule for the script to Custom and use */2 * * * * for the timer (cron).  Make sure to remove any added carriage returns when you copy and paste the script.  

 

I'm running Influx and Grafana on a separate system.  I believe there are integrated ways to do this if you're running in a docker on Unraid.

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.