egregius.be

Urban Exploration, PHP and others…

Synology Diskstation DSM 7 auto-shutdown

Synology Diskstation DMS 7 auto-shutdown
Synology Diskstation DMS 7 auto-shutdown

Just like the previous time I updated my nas to a new major release of DSM , that’ll be DSM 7, and it broke the Advanced Power Management package. because I want my nas to go to sleep when I don’t need it I dug up an old script from the archives.
I altered it a bit, made it better…
First thing it does is checking wether my Nvidia Shield media player is online. Of course I don’t want the nas to poweroff when I’m just pausing a movie.

If the media player isn’t online I check the read- and write blocks of the disks, together with the network traffic.
If both are below a treshold a poweroff is initiated.

Numbers are logged in a logfile so it’s easy to analyze what numbers are good for your situation. My network treshold is quite big because of a proxmox server that’s continuously polling the NFS share.

Execute the script as root with the Synology task sheduler every 5 minutes.

#!/bin/bash
sudo ping -q -c1 192.168.2.7
if [[ $? -eq 1 ]];then
	DISK=1
	NETWORK=15000

	mkdir -p /tmp/trytosleep
	NOW=$(date +%s)
	NOWTIME=$(date +"%Y/%m/%d %H:%M:%S")
	LAST=`cat /tmp/trytosleep/TIMESTAMP`
	if [[ $? -eq 1 ]];then
		echo $NOW > /tmp/trytosleep/TIMESTAMP
		LAST=$NOW
	fi
	NETRXLAST=`cat /tmp/trytosleep/NETRX`
	if [[ $? -eq 1 ]];then
		echo '0' > /tmp/trytosleep/NETRX
		NETRXLAST=0
	fi
	NETTXLAST=`cat /tmp/trytosleep/NETTX`
	if [[ $? -eq 1 ]];then
		echo '0' > /tmp/trytosleep/NETTX
		NETTXLAST=0
	fi
	DISKREADLAST=`cat /tmp/trytosleep/DISKREAD`
	if [[ $? -eq 1 ]];then
		echo '0' > /tmp/trytosleep/DISKREAD
		DISKREADLAST=0
	fi
	DISKWRITELAST=`cat /tmp/trytosleep/DISKWRITE`
	if [[ $? -eq 1 ]];then
		echo '0' > /tmp/trytosleep/DISKWRITE
		DISKWRITELAST=0
	fi

	let PERIOD=$NOW-$LAST

	NETRX=`netstat -s | grep "InOctets" | cut -d: -f2 | awk '{ print $1 }'`
	NETTX=`netstat -s | grep "OutOctets" | cut -d: -f2 | awk '{ print $1 }'`
	let RX=$NETRX-$NETRXLAST
	let TX=$NETTX-$NETTXLAST
	let TOTAL=$RX+$TX
	let NETPERTIME=$TOTAL/$PERIOD
	echo $NETRX > /tmp/trytosleep/NETRX
	echo $NETTX > /tmp/trytosleep/NETTX
	echo --- NET	RX =	$NETRX TX =	$NETTX TOTAL =	$TOTAL PER SECOND =	$NETPERTIME | tee -a /tmp/trytosleep/trytosleep.log

	DISKREAD=`cat /sys/block/sda/stat | tr -s ' ' | cut -d ' ' -f 2`
	DISKWRITE=`cat /sys/block/sda/stat | tr -s ' ' | cut -d ' ' -f 7`
	let READ=$DISKREAD-$DISKREADLAST
	let WRITE=$DISKWRITE-$DISKWRITELAST
	let TOTALBLOCKS=$READ+$WRITE
	let BLOCKSPERTIME=$TOTALBLOCKS/$PERIOD
	echo $DISKREAD > /tmp/trytosleep/DISKREAD
	echo $DISKWRITE > /tmp/trytosleep/DISKWRITE
	echo --- DISK READ =	$READ WRITE =	$WRITE TOTAL=	$TOTALBLOCKS PER SECOND =	$BLOCKSPERTIME | tee -a /tmp/trytosleep/trytosleep.log

	echo --- $NOWTIME : Period =	$PERIOD DISK =	$BLOCKSPERTIME/$DISK NET =	$NETPERTIME/$NETWORK | tee -a /tmp/trytosleep/trytosleep.log
	echo $NOW > /tmp/trytosleep/TIMESTAMP

	if [[ $PERIOD -ge 250 ]];then
		if [[ $NETPERTIME -le $NETWORK ]];then
			if [[ $BLOCKSPERTIME -le $DISK ]];then
				echo --- $NOWTIME : Shutting down now | tee -a /tmp/trytosleep/trytosleep.log
				/volume1/Guy/scripts/atshutdown.sh
				/sbin/poweroff
				exit
			else
				echo --- $NOWTIME : To much disk activity | tee -a /tmp/trytosleep/trytosleep.log
			fi
		else
			echo --- $NOWTIME : To much network activity | tee -a /tmp/trytosleep/trytosleep.log
		fi
		echo --- | tee -a /tmp/trytosleep/trytosleep.log
	fi
else
	echo --- $NOWTIME : Shield online | tee -a /tmp/trytosleep/trytosleep.log
fi
exit