Shutdown FreeNAS 7 Server when no ping returned from WHS2011 IP address

Status
Not open for further replies.

EditBear

Cadet
Joined
Sep 17, 2018
Messages
1
Hi,

Please forgive my lack of knowledge, I'm a very old "nooby" from the days of "vacuum tubes", trying my best to understand programming and failing miserably!

I have a FreeNas 7 server with all my DVD movies on it, and a Windows Home Server (2011) with all my Blu-rays and music.

The WHS2011 starts up when any client HTPC starts up and shuts down when all clients sleep.
I can get my WHS2011 server to start-up my FreeNas server (by a WOL script during the WHS startup), but for years, I cannot work out how to get the FreeNas to shutdown when my WHS shuts down?

I then came upon this script (posted originally by sac0dan) which seems to monitor a range of IP addresses and shuts the FreeNas 7 server down if they all are quiet.

https://forums.freenas.org/index.php?threads/green-power-scheduled-power-off.232/

I only need to check just one IP address - that of my WHS2011 which is 192.168.0.220
My FreeNas7 is at 192.168.0.250

I can see the program is looping round but my ability is too little to strip out the unnecessary steps:confused:

I would be very grateful if some kind person would simplify this to check only the one IP address please?

Code:
#!/bin/bash
{
lockfile=/tmp/autosuspend.lock

if [ ! -e $lockfile ]; then
   trap "rm -f $lockfile; exit" INT TERM EXIT
   touch $lockfile

	echo "autosuspend script started"
	#sleep 180	 # Sleep for 3 mins to make sure the server don't shut down right away
	class=192.168.0
	myIP=192.168.0.250   #$(/sbin/ifconfig | egrep -o "192.168.0.[0-9]{1,3}")
	failcount=0
	maxfail=3	   # Set the number of failure count before we do a shutdown
	SLEEP=600	   # Numbers of seconds between each check/loop

	LOGFILE=/tmp/autosuspend.log

	# Don't suspend if one of the following processes is active (separate by space):
	STOPPROCS='storeBackup wget'

	echo Logfile=$LOGFILE

	unset known_host

	_ping_last_host() {
	  # try to ping the last known active host
	  # return 0 on success, otherwise 1
	  echo "pinging last host $known_host" >> $LOGFILE
	  if [ $known_host ]; then
		echo -n "`date` - pinging last host $known_host - " >> $LOGFILE
		ping -c 1 $known_host >/dev/null;
		if [ $? -eq 0 ]; then 
		  echo "host is active" >> $LOGFILE
		  # Jepp! We're done
		  return 0
		else
		  echo "fail" >> $LOGFILE
		  unset known_host
		  return 1
		fi;
	  else
		return 1
	  fi
	 
	}

	_ping_range() {
	  # Ping an IP-range and look for a responding host.
	  # If there is one store it's IP in $known_host and return 0
	  # return 0 on success, otherwise 1
	  cnt=0
	  echo "`date` - pinging range...  " >> $LOGFILE
	  for i in {2..14}; # Set the range
	  do
		# Ignore my own IP
		if [ ${class}.${i} != $myIP ]; then
		  echo "pinging ${class}.${i}" >> $LOGFILE
		  ping -c 1 ${class}.${i} >/dev/null
		  if [ $? -eq 0 ]; then 
			echo -n "${class}.${i} - " >> $LOGFILE
			known_host=${class}.${i}
			return 0;
		  fi
		fi
	  done
	  return 1
	}

	_shutdown() { 
	  # Do a shutdown if we failed for $failcount rounds
	  # We need a script suspend.sh in the current directory
	  echo "deciding doing shutdown or not. failcount = $failcount" >> $LOGFILE
	  if [ $failcount -eq $maxfail ];then 
		echo "`date` - doing a shutdown" >> $LOGFILE
		more $LOGFILE | grep -F --line-buffered ''  # clear buffer
				rm $lockfile
				trap - INT TERM EXIT
		shutdown -p now >>$LOGFILE
		more $LOGFILE | grep -F --line-buffered ''  # clear buffer
		#we will never arrive here with a shutdown...
		echo "`date` - back from suspend" >> $LOGFILE
		failcount=0;
	  fi
	}

	while [ 1 ];
	do
	  proc_found=0
	  # look if uniterruptable jobs are running
	  for proc in $STOPPROCS
	  do
		if [ "`pgrep $proc`" != "" ];then 
		  echo "`date` - $proc is running." >> $LOGFILE
		  proc_found=1
		  break
		fi
	  done 
	  echo procfound=$proc_found 

	  if [ $proc_found -eq 0 ]; then
		# look for other hosts, that are alive in our subnet
		_ping_last_host
		if [ $? -ne 0 ];then
		  _ping_range
		  if [ $? -ne 0 ];then
			let failcount++;
			echo "failure No. $failcount" >> $LOGFILE
			_shutdown;
		  else
			echo "good." >> $LOGFILE
			failcount=0;
		  fi
		  else
			echo "good." >> $LOGFILE
			failcount=0;
		fi
	  fi
	  sleep $SLEEP;
	done
	
   rm $lockfile
   trap - INT TERM EXIT
else
   echo "autosuspend is already running. Exiting."
fi
} &

 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504

Ericloewe

Server Wrangler
Moderator
Joined
Feb 15, 2014
Messages
20,194
And WHS 2011 is itself rather old and crusty. You might want to look into upgrading both those servers to something modern.
 
Status
Not open for further replies.
Top