Sorry we lost some posts because of database errors!

*New 12.1 series Release:
2020-09-01: XigmaNAS 12.1.0.4.7728 - released

*New 11.4 series Release:
2020-08-27: XigmaNAS 11.4.0.4.7718 - released!


We really need "Your" help on XigmaNAS https://translations.launchpad.net/xigmanas translations. Please help today!

Producing and hosting XigmaNAS costs money. Please consider donating for our project so that we can continue to offer you the best.
We need your support! eg: PAYPAL

Summary of 3 different Scripts for Standby WOL Shutdown ACPI

XigmaNAS Scripts and shell tips
Forum rules
Set-Up GuideFAQsForum Rules
Post Reply
Gleneisenbach
Starter
Starter
Posts: 37
Joined: 04 Apr 2015 10:36
Location: Neu-Anspach
Status: Offline

Summary of 3 different Scripts for Standby WOL Shutdown ACPI

#1

Post by Gleneisenbach »

Hi Folks,

after installing my first NAS4Free I was looking for a way to put it into ACPI S5 (Wake on LAN) when it is unused.
So here are the two options I found and the one I created:

1) The manageacpi.sh from Fritz:
viewtopic.php?f=70&t=2197
This script is started as Postinit script, it checks frequently if there is one of the defined IP addresses in the network.
It supports a mandatory up-time, APCI S3 and an email notification.
If you have sub-nets or using your NAS4FREE also as cloud via internet, this script won't identify the IPs and will shutdown your server.
If you use your NAS4Free with your mobile, add the IP of your mobile in the check list, this script won't shutdown your server until you turn off your mobile.

2) The nas_poweroff from siftu
viewtopic.php?f=70&t=4229&p=52497#p52497
It is not really a standby monitoring, but a conditional shutdown.
It is started as cron job(s).
It checks the I/O activity before a shutdown is initiated. It also supports APCI S3 and an email notification.
Unfortunately it didn't work with DHCP (I proposed a fix in the topic)

3) The GoToWOL.sh from me
This script is started as Postinit script, it checks frequently if there is i/o activiy on the server.
It supports a mandatory up-time, but neither APCI S3 nor an email notification.
Please note that there is some i/o traffic anytime, so the script needs a value to be tolerated and taken as inactivity.
If your server don't shutdown, check the log to find a good value.

Code: Select all

#!/bin/sh
# Script to send a NAS to WOL Status (APCI State 5) in case of inactivity
# 
# Author (c): Dirk Gasser 
# Version 1.0
# 
# Description: 
# The script must be started as Postinit script as a process
# It sleeps -c seconds, than it check the i/o packages of the ip-address line of the netstat command
# If the number of i/o packages is lower than the -t tolerance, the interval is counted as inactiv
# If the -i number of inactiv intervals is reached and there is no -u mandatory update, the server is shut down. 
# The log is written to /var/log/GoToWOL.log, not to a mounted data disk, so that the standby of the disks isnt't disrupted
# 
# Usage:
# GoToWOL.sh [-c Checkinterval] [-i inactivity] [-t tolerance] [-l] [-u upstart,upend] &
#
# Example:
# GoToWOL.sh  -c 900 -i 8 -t 50 -u 0700,2200 &
#
# Defaul:
# GoToWOL.sh  -c 900 -i 8 -t 400  &  <== check every 15 Minutes, Tolarate 400 i/o packages, shutdown after 2 hours inactivity
#
#-c: Check interval in seconds
#-i: Number of intervals without activity before standby
#-t: number of i/o packages which is taken as no activity
#-l: extented logging mode
#-u: mandatory up time hhmm,hhmm

#
#Constants:
#
LOGDAT="/var/log/GoToWOL.log"


#
#Default Values
#
CheckInterval="900"        # Check every 15 Minutes
IntervalbeforeWOL="8"      # WOL Status after two hours inactiv
Logmode="N"                # normal log mode
UpStart="0000"             # no mandatory up time 
UpEnd="0000"
Tolerate="400"		   # Tolerate 400 Pakages (per -c = 15 Minutes)

#
#Variables:
#
CountInactiv="0"

#
# Function
# readOpts: Read the parameters, check them
#
readOpts() {
while getopts ":c:i:t:lu:" opt
do
case $opt in
# [-c Checkinterval]
c) echo "$OPTARG" | grep -E "([0-9]+)" > /dev/null
    if [ "$?" -eq "0" ]
    then
     if [ "$OPTARG" -gt "120" ] 
     then 
       CheckInterval=$OPTARG
     else
        echo "Checkinterval minimum 120 is used"  >> $LOGDAT
        CheckInterval="120"
     fi
    else
       echo "Checkinterval must be numeric Defaul 600 is used" >> $LOGDAT
    fi;;
#  [-l]  
l) Logmode="E";;
#  [-i inactivity] 
i) echo "$OPTARG" | grep -E "([0-9]+)" > /dev/null
    if [ "$?" -eq "0" ]
    then
     if [ "$OPTARG" -gt "5" ] 
     then 
       IntervalbeforeWOL=$OPTARG
     else
        echo "IntervalbeforeWOL minimum 5 is used" >> $LOGDAT
        IntervalbeforeWOL="5"
     fi
    else
       echo "IntervalbeforeWOL must be numeric default 12 is used" >> $LOGDAT
    fi;;
#  [-t tolerance] 
t) echo "$OPTARG" | grep -E "([0-9]+)" > /dev/null
    if [ "$?" -eq "0" ]
    then
       Tolerate=$OPTARG
    else
       echo "Tolerate must be numeric 5 is used" >> $LOGDAT
    fi;;
#  [-u upstart,upend] 
u)  echo "$OPTARG" | grep -E "([0-2][0-9][0-5][0-9][,][0-2][0-9][0-5][0-9])" > /dev/null
    if [ "$?" -eq "0" ]
    then
        UpStart=`echo $OPTARG | cut -d "," -f1`
        UpEnd=`echo $OPTARG | cut -d "," -f2` 
    else
       echo "Uptime must be 9999 9999" $OPTARG >> $LOGDAT
    fi
esac
done
 echo "Option used: "  >> $LOGDAT
 echo "CheckInterval    :" $CheckInterval  >> $LOGDAT
 echo "IntervalbeforeWOL:" $IntervalbeforeWOL  >> $LOGDAT
 echo "Logmode          :" $Logmode   >> $LOGDAT            
 echo "UpStart          :" $UpStart  >> $LOGDAT
 echo "UpEnd            :" $UpEnd  >> $LOGDAT
 echo "Tolerate         :" $Tolerate  >> $LOGDAT   
}

#
# Function
# getTotalIO: Get the total I/O from IP Address line  
#
getTotalIO() {
Iplineofnetstat=`netstat -in | grep -E '[[:digit:]]{1,3}\.[1-9]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}'`
if [ $Logmode = "E" ]; then
     echo `netstat -in` >> $LOGDAT
fi
InPackages=`echo $Iplineofnetstat | cut -d " " -f5`
OutPackages=`echo $Iplineofnetstat | cut -d " " -f8`
TotalIO=`expr $InPackages + $OutPackages`
}

#
#Main Program
#

#Clear old logfile
echo "Start of GoToWOL" > $LOGDAT
currentTimestamp=`date`
echo $currentTimestamp  >> $LOGDAT

Options="$@"
readOpts $Options

getTotalIO
Finished="0"

while [ $Finished -ne "1" ]
do 
   oldTotalIO=$TotalIO
   sleep $CheckInterval
   getTotalIO
   IODiff=`expr $TotalIO - $oldTotalIO`
   currentTimestamp=`date`
   echo $currentTimestamp " / Total IO: " $TotalIO " / new IO " $IODiff  >> $LOGDAT 

   if [ $IODiff -le $Tolerate ]
   then
      CountInactiv=`expr $CountInactiv + "1"`
   else
      CountInactiv="0"
   fi
   if [ $Logmode = "E" ]; then
     echo "Number of Intervals inactiv: " $CountInactiv   >> $LOGDAT
   fi
   if [ $CountInactiv -ge $IntervalbeforeWOL ]
   then
	Zeit=`date`
	Time=`echo $Zeit | cut -d " " -f4`
   	Min=`echo $Time | cut -d : -f1`
  	Sek=`echo $Time | cut -d : -f2`
 	ActualTime=$Min$Sek 
 	if [ $ActualTime -ge $UpStart ] ; then
          if [ $ActualTime -le $UpEnd ] ; then
     	 # No Shutdown
     		 echo "Mandatory Up time " $UpStart ">" $ActualTime "<" $UpEnd  >> $LOGDAT
           else
		echo ".... Finished" >> $LOGDAT
      		Finished="1"
	   fi
  	 else
     		echo ".... Finished" >> $LOGDAT
      		Finished="1"
	fi
   fi
done
`shutdown -p +1`
Here is an example of the log, I use a less risky value -t to prevent an unintentionally shutdown.

Code: Select all

Start of GoToWOL
Mon Apr 6 09:20:05 CEST 2015
Option used: 
CheckInterval    : 900
IntervalbeforeWOL: 8
Logmode          : N
UpStart          : 0000
UpEnd            : 0000
Tolerate         : 400
Mon Apr 6 09:35:05 CEST 2015  / Total IO:  617  / new IO  533
Mon Apr 6 09:50:05 CEST 2015  / Total IO:  1875  / new IO  1258
Mon Apr 6 10:05:05 CEST 2015  / Total IO:  2350  / new IO  475
Mon Apr 6 10:20:05 CEST 2015  / Total IO:  2946  / new IO  596
Mon Apr 6 10:35:05 CEST 2015  / Total IO:  3412  / new IO  466
Mon Apr 6 10:50:06 CEST 2015  / Total IO:  3849  / new IO  437
Mon Apr 6 11:05:06 CEST 2015  / Total IO:  7320848  / new IO  7316999
Mon Apr 6 11:20:06 CEST 2015  / Total IO:  7905318  / new IO  584470
Mon Apr 6 11:35:06 CEST 2015  / Total IO:  8474705  / new IO  569387
Mon Apr 6 11:50:06 CEST 2015  / Total IO:  8853151  / new IO  378446
ASRock Q1900-ITX, be quiet! System Power S7 300 Watt bulk, Kingston 4GB DDR3L SO-DIMM 1600MHz, PC3-12800, 2 X Seagate ST3000VN000 NAS HDD 3TB, Cooler Master Elite 120 Advanced schwarz,Toshiba TransMemory mini Suruga 8GB schwarz
NAS4Free11.0.0.4 -(Sayyadina)

anders.malm
Starter
Starter
Posts: 17
Joined: 01 Mar 2015 10:03
Status: Offline

Re: Summary of 3 different Scripts for Standby WOL Shutdown ACPI

#2

Post by anders.malm »

Hello I have successfully used the nas_poweroff script in the past but since upgraded to 11.0.0.4 it doesn't work anymore. I suspect that it may be because off some changes in the system. Is someone running this script under 11.0.0.4?
ASUS P10-S, Intel i3 CPU, 16 GB ECC ram, 250 GB M2 SSD, RootOnZFS

Gleneisenbach
Starter
Starter
Posts: 37
Joined: 04 Apr 2015 10:36
Location: Neu-Anspach
Status: Offline

Re: Summary of 3 different Scripts for Standby WOL Shutdown ACPI

#3

Post by Gleneisenbach »

Hi Andreas,

I'm using my gotowol script but the problem seems to be the same.
I also upgrated from 9.0 to 11.0 and for any reason the number of i/o done if the NAS is not used increased significant.
Just check the log of the script and adjust the sleep_packets parameter.
For my script I had to increase the threshold parameter from 1.200 to 5.300 i/o done in 15 minutes.
ASRock Q1900-ITX, be quiet! System Power S7 300 Watt bulk, Kingston 4GB DDR3L SO-DIMM 1600MHz, PC3-12800, 2 X Seagate ST3000VN000 NAS HDD 3TB, Cooler Master Elite 120 Advanced schwarz,Toshiba TransMemory mini Suruga 8GB schwarz
NAS4Free11.0.0.4 -(Sayyadina)

Morrio
Starter
Starter
Posts: 28
Joined: 14 Dec 2013 20:13
Status: Offline

Re: Summary of 3 different Scripts for Standby WOL Shutdown ACPI

#4

Post by Morrio »

Hi,

i try to use your script but i did no log or something else.

Here is my Command.

Code: Select all

/mnt/pool1/Apps/GoToWOL.sh -c 900 -i 8 -t 2.000 &
The scipt owner is root with permission 0770. Is here maybe a mistake?

Maybe someone can help me.

Gleneisenbach
Starter
Starter
Posts: 37
Joined: 04 Apr 2015 10:36
Location: Neu-Anspach
Status: Offline

Re: Summary of 3 different Scripts for Standby WOL Shutdown ACPI

#5

Post by Gleneisenbach »

I tried the command on my NAS and the log was written in:

/var/log/GoToWOL.log


Start of GoToWOL
Mon Jan 29 20:22:43 CET 2018
Option used:
CheckInterval : 900
IntervalbeforeWOL: 8
Logmode : N
UpStart : 0000
UpEnd : 0000
Tolerate : 2.000
ASRock Q1900-ITX, be quiet! System Power S7 300 Watt bulk, Kingston 4GB DDR3L SO-DIMM 1600MHz, PC3-12800, 2 X Seagate ST3000VN000 NAS HDD 3TB, Cooler Master Elite 120 Advanced schwarz,Toshiba TransMemory mini Suruga 8GB schwarz
NAS4Free11.0.0.4 -(Sayyadina)

Post Reply

Return to “Scripts and shell tips”