|
Post by noname1112 on Jun 14, 2008 8:08:41 GMT 7
this is a howto for the automatic shutdown of the thecus n5200 (pro): 1. install the modules "sshd", "sysuser" and "shutdown" from onbeat.dk. 2. ssh login: a) set a password for the user "sys" on the "sysuser" module config page: i.e. "http://ip_address_of_n5200/adm/getform.html?Module=SYSUSER" b) login via ssh on your thecus as "sys": "ssh sys@ip_address_of_n5200" 3. create script: a) type "vi /raid/data/powersaver.sh" b) type "i" and copy&paste the following script: #!/bin/sh # # (cc:by-sa) 2007 Marco Gabriel, http://www.marcogabriel.com/ # # Powersaver.sh
### # CONFIG ###
# Filename of the statusfile STATUSFILE="/tmp/powersaver-status"
# which ip range should be checked? # this example looks in "192.168.1.10 to 192.168.1.30" # example: # a=168.168.1. # b1=10 # b2=30 a=192.168.1. b1=10 b2=30
# how many clients are always on (other servers, routers, etc) MINCLIENTS=0
# shutdown after how many retries? RETRIES=2
### # END CONFIG ###
NUMCLIENTS=0
for((x=$b1;x<=$b2;x++)); do ping -c 1 $a$x if [ $? -eq 0 ]; then let NUMCLIENTS=$NUMCLIENTS+1 fi done
if [ $NUMCLIENTS -le $MINCLIENTS ]; then if [ ! -f "$STATUSFILE" ]; then echo "COUNT=$RETRIES" > $STATUSFILE fi . $STATUSFILE if [ $COUNT -lt 0 ]; then /raid/data/module/SHUTDOWN/system/doshutdown; else let COUNT=$COUNT-1 echo "COUNT=$COUNT" > $STATUSFILE fi; else if [ -f "$STATUSFILE" ]; then rm $STATUSFILE fi fi
c) modify the ip range, minclients and retries to your needs. d) save the "powersaver.sh" by pushing the ESC button and typing ":wq" or quit without saving by pushing the ESC button and typing ":q!" e) make the "powersaver.sh" executable with: "chmod +x /raid/data/powersaver.sh" 4. modify cron: a) make a backup: "cp /app/cfg/crond.conf /app/cfg/crond.conf.backup" b) "vi /app/cfg/crond.conf" c) "i" d) copy&paste: "*/10 * * * * /raid/data/powersaver.sh" in a new line (the number behind the "/" specifies the number of minutes cron has to wait before executing "powersaver.sh" again) e) save: ":wq" 5. restart your n5200. 6. now, your n5200 should execute the "powersaver.sh" script every 10 minutes and shutdown after the specified amount of failed attempts using "/raid/data/module/SHUTDOWN/system/doshutdown"
|
|
|
Post by omega on Jun 16, 2008 20:15:40 GMT 7
Hi noname1112,
thanks a lot for this howto....
One line that I don't understand is
if [ $COUNT -le $MINCLIENTS ]; then
in my opinion it shoudl read
if [ $NUMCLIENTS -le $MINCLIENTS ]; then
isn't it?
Anyway, in my opinion it would be better to use the META module and to write a script for it. By this way you don't need to modify the system crontab file (which might be destroyed when updating the firmware, etc.).
I've (re-) written a script which should do the same as yours and which can be used as a META script. You might want to try it and if so, please give some feedback. Hope the script is OK so far...
Of course you need to adjust the config vars, especially the WAIT_TIME, CYCLE_TIME and the CHECK_HOST_LIST!
Andreas
P.S. The formatting in HTML is terrible, I could supply this script by E-Mail or maybe the better way is to establish a META script section at the Thecus Wiki....
#!/bin/sh ############################################################################# ## ## autoshutdown - shutdown Thecus N5200 when other hosts are not alive ## ## This shell script is inteded to run on a Thecus N5200 hosts under control of ## the META module but can be started and stopped manually too. ## ## This script checks a number of defined hosts being alive (ping test) and ## shutdown the N5200 if none of the hosts in the list are alive for a given time. ## ## By design this script can be used for other tasks to be done whenever ## the defined hosts are not alive. Just adjust the ACTION_COMMAND variable. ## ## 16.06.2008 (C) Andreas Vogel (omega) ## #############################################################################
##--------------------------------------------------------------------------- ## Set this variable to the hosts to be checked being alive. ## You may specify several hosts or host ranges separated by blanks. ## A host range is in the form 192.168.1.30-50 which denotes all hosts starting ## from 192.168.1.30 up to 192.168.1.50. ## ## Examples: ## CHECK_HOST_LIST="192.168.1.30" ## CHECK_HOST_LIST="192.168.1.10-30" ## CHECK_HOST_LIST="192.168.1.30 192.168.1.40" ## CHECK_HOST_LIST="192.168.1.30-35 192.168.1.40 192.168.1.50-90" ##--------------------------------------------------------------------------- CHECK_HOST_LIST="192.168.1.10"
##--------------------------------------------------------------------------- ## The number of hosts which are always active and which are part of ## CHECK_HOST_LIST. This number of hosts are ignored when calculating the ## total number of active hosts. ##--------------------------------------------------------------------------- ALWAYS_ACTIVE_HOSTS=0
##--------------------------------------------------------------------------- ## This variable specifies the time in seconds for which no hosts in ## CHECK_HOST_LIST must be pingable in order to start the action. ##--------------------------------------------------------------------------- WAIT_TIME=120
##--------------------------------------------------------------------------- ## This variable specifies the cycle time in seconds for doing the alive checks. ##--------------------------------------------------------------------------- CYCLE_TIME=5
##--------------------------------------------------------------------------- ## This script/executable will be called whenever the defined hosts are not ## alive for at least WAIT_TIME seconds. ##--------------------------------------------------------------------------- ACTION_COMMAND=/raid/data/module/SHUTDOWN/system/doshutdown
############################################################################# ## ## END of configuration section!! Do not change anything below this comment!! ## #############################################################################
############################################################################# ## Debug ############################################################################# Debug () { $DEBUG && echo $* }
############################################################################# ## DaemonProcess ############################################################################# DaemonProcess () { while [ ! -e $PIDFILE ] ; do sleep 1 done Debug "Started DaemonProcess() with PID $(<$PIDFILE)"
## ## In any case: delete the PID file which indicated that we're actually running... ## trap "rm -f $PIDFILE ; trap - EXIT ; exit 0" EXIT INT ABRT QUIT TERM
HOST_IP_LIST="" for HOST_SPEC in $CHECK_HOST_LIST ; do FIRST_IP=$(echo $HOST_SPEC | cut -s -f1 -d-) FIRST_IP=${FIRST_IP:-$HOST_SPEC}
NET=$(echo $FIRST_IP | cut -s -f1-3 -d.)
FIRST_HOST=$(echo $FIRST_IP | cut -s -f4 -d.)
LAST_HOST=$(echo $HOST_SPEC | cut -s -f2 -d-) LAST_HOST=${LAST_HOST:-$FIRST_HOST}
for ((HOST = $FIRST_HOST; HOST <= $LAST_HOST; HOST++)) ; do HOST_IP_LIST="$HOST_IP_LIST $NET.$HOST" done done
NOW=$(date +'%s') ACTION_TIME=$((NOW + WAIT_TIME))
while true ; do ## ## Check how many hosts are alive (pingable) ## ACTIVE_HOSTS=0
SCAN_START=$(date +'%s') if [ -x "$FPING" ] ; then Debug "Checking hosts: $HOST_IP_LIST" ACTIVE_HOSTS=$($FPING -a -r 1 -t 100 $HOST_IP_LIST 2>/dev/null | wc -l) else for HOST_IP in $HOST_IP_LIST ; do Debug "Checking host $HOST_IP" ping -c 1 $HOST_IP &>/dev/null && ((ACTIVE_HOSTS++)) done fi SCAN_END=$(date +'%s') SCAN_TIME=$((SCAN_END - SCAN_START))
NOW=$(date +'%s')
if [ $ACTIVE_HOSTS -le $ALWAYS_ACTIVE_HOSTS ] ; then if [ $NOW -gt $((ACTION_TIME += SCAN_TIME)) ] ; then ## We've reached the computed action time: call the action now! Debug "Action time reached: calling $ACTION_COMMAND" $ACTION_COMMAND
## re-schedule the action NOW=$(date +'%s') ACTION_TIME=$((NOW + WAIT_TIME)) else Debug "No active hosts but need to wait another $((ACTION_TIME - NOW)) seconds until action..." fi else ## There are still/again some hosts active - reset the action logic. ACTION_TIME=$((NOW + WAIT_TIME + CYCLE_TIME)) Debug "Active hosts ($ACTIVE_HOSTS) exceeds always-active hosts ($ALWAYS_ACTIVE_HOSTS) - resetting." fi
Debug "Sleeping for $CYCLE_TIME secs..." sleep $CYCLE_TIME done }
############################################################################# ## ## MAIN ## #############################################################################
SCRIPTNAME=$(basename $0) PIDFILE=/var/tmp/$SCRIPTNAME.pid
## ## We definitely do like fping: check if we can find a fping executable somewhere. ## If fping isn't available just use the "normal" ping command. ## if [ -x /raid/data/module/UTILS/system/bin/fping ] ; then FPING=/raid/data/module/UTILS/system/bin/fping else FPING=$(type -p fping) fi [ ! -x "$FPING" ] && FPING=""
## ## Parse the command line... ## DEBUG=false action="" while [ -n "$1" ] ; do case "$1" in -d) DEBUG=true ;; -*) echo "Illegal option '$1'!" ; exit 1 ;; *) action=$1 ;; esac shift done
## ## Now do what needs to be done... ## case "$action" in start) if [ ! -x "$ACTION_COMMAND" ] ; then echo "ERROR: action script/executable '$ACTION_COMMAND' not available or not executable!" exit 1 fi if [ -e $PIDFILE ] ; then echo "Another instance of $0 with PID $(<$PIDFILE) is running - exiting..." exit 1 else DaemonProcess & echo $! >$PIDFILE fi ;;
stop) if [ -r $PIDFILE ] ; then PID=$(<$PIDFILE) if kill $PID ; then echo "Killed $SCRIPTNAME with PID $PID" else echo "Cannot kill $SCRIPTNAME with PID $PID" fi else echo "Cannot stop $SCRIPTNAME: not running" fi rm -f $PIDFILE ;;
status) if [ -r $PIDFILE ] ; then PID=$(<$PIDFILE) if [ $(ps w | awk "\$1 == $PID { print \"OK\" }") = OK ] ; then echo "$SCRIPTNAME is running with PID $PID!" exit 0 fi fi echo "$SCRIPTNAME is not running!" exit 1 ;;
*) echo "ERROR: Illegal action '$action'!" echo "Usage: $0 [-d] start | stop | status" exit 1 ;; esac
exit 0
|
|
|
Post by noname1112 on Jun 18, 2008 3:51:05 GMT 7
thanks for the reply. you are right. the line should be
if "powersaver.sh" doesn't find "/tmp/powersaver-status", it creates the file with the content "COUNT=$RETRIES" (in the example "COUNT=2"). as long as "COUNT" isn't negative, "powersaver.sh" will count down: first attempt: create "/tmp/powersaver-status" with "COUNT=2" and not enough clients => "COUNT=1" first retry: not enough clients => "COUNT=0" second retry: not enough clients => "COUNT=-1" => shutdown
i changed the line of code and added 4.a) to make a backup of the cron file before modifying it. i will use my solution, because it is already working. i didn't check omegas solution. but, if it is working without any problems, everybody else who doesn't want to take risks should use his solution.
|
|
|
Post by noname1112 on Sept 15, 2008 8:51:46 GMT 7
hi. i wrote now my own shutdown script and didn't just modify another ones. it is more reliable than the first shutdown script, because it uses netstat to look for connections between the nas and its clients. if you connect to the thecus n5200, netstat will know . the other script just uses ping to search for devices in the lan. it doesn't differentiate between real clients and devices that are just connected to the lan like printers, routers... . but, the new script does. you just need to follow my instructions in the first post using the new script. have fun. noname1112 my script: #!/bin/bash
# shutdown after how many failed attempts? ATTEMPTS=4
STATUSFILE="/tmp/powersaver-status" IP=$(ip route | head -n 1 | awk '{print $5}') LAST=$(echo $IP | awk -F . '{print $4}') SEARCH=$(echo $IP | sed "s/${LAST}//") CONNECTIONS=$(netstat -a | grep $SEARCH | wc -l | sed 's/ //g')
if [ ! -f "$STATUSFILE" ]; then echo 1 > $STATUSFILE fi
if [ $CONNECTIONS -eq 0 ]; then if [ $(cat $STATUSFILE) -eq $ATTEMPTS ]; then /raid/data/module/SHUTDOWN/system/doshutdown else echo $(expr $(cat $STATUSFILE) + 1) > $STATUSFILE fi else echo 1 > $STATUSFILE fi
|
|
|
Post by netaddict on Sept 21, 2008 13:24:31 GMT 7
For me, omega's script worked better. For me, netstat detects extraneous connections from the upstream router.
I also modified omega's script with these changes, which 1) hides the active debug code; 2) checks for MLDonkey and RSNAPSHOT activity so shutdown does not occur if they're active: 1. Orig:
Debug () { $DEBUG && echo $* } New:
Debug () { $DEBUG && echo $* >&2 } 2. Orig:
if [ $ACTIVE_HOSTS -le $ALWAYS_ACTIVE_HOSTS ] ; then New:
# Check for RSNAPSHOT and MLDonkey activity RSNAPSHOT=`ps ww|grep -c RSNAPSHOT` MLDONKEY=0 if [ -e /raid0/data/mldonkey/temp ]; then MLDONKEY=`find /raid0/data/mldonkey/temp -type f | wc -l` fi
if [ $ACTIVE_HOSTS -le $ALWAYS_ACTIVE_HOSTS \ -a $RSNAPSHOT -eq 0 \ -a $MLDONKEY -eq 0 ] ; then
|
|
|
Post by noname1112 on Sept 23, 2008 19:38:00 GMT 7
hi. i don't know what your network configuration is like. therefore, i can't help u. i had to change my script a little bit, because my n5200 gets its ip address with dhcp now and that caused some problems. my dd-wrt router checks the mac address of my n5200 and gives him always the ip address 168.192.1.22 using dhcp. that way i can use the n5200 hostname instead of its ip address. netaddict: perhaps this script solves your problem too #!/bin/bash
# shutdown after how many failed attempts? ATTEMPTS=4 # what are the first 3 ip numbers of your n5200? # i.e.: ip=192.168.1.5 => SEARCH=192.168.1. SEARCH=
STATUSFILE="/tmp/powersaver-status" CONNECTIONS=$(netstat -an | grep $SEARCH | wc -l | sed 's/ //g')
if [ ! -f "$STATUSFILE" ]; then echo 1 > $STATUSFILE fi
if [ $CONNECTIONS -eq 0 ]; then if [ $(cat $STATUSFILE) -eq $ATTEMPTS ]; then /raid/data/module/SHUTDOWN/system/doshutdown else echo $(expr $(cat $STATUSFILE) + 1) > $STATUSFILE fi else echo 1 > $STATUSFILE fi
|
|
|
Post by noname1112 on Oct 3, 2008 7:57:19 GMT 7
hi. netaddict: you can exclude ip adresses (like that of your router, printer etc.) from the search with this script i recommend this script. don't use my other netstat scripts. #!/bin/bash
##########variables########## # shutdown after how many failed attempts? ATTEMPTS=4 # what are the first 3 ip numbers of your n5200? # i.e.: ip=192.168.1.5 => SEARCH=192.168.1. SEARCH= # What are the ip adresses of the devices you want to exclude from the search? # Don't exclude the ip adress of your n5200! # (i.e. EXCLUDE="192.168.1.1 192.168.1.26 192.168.1.127 192.168.1.28") EXCLUDE=""
##########Don't modify########## STATUSFILE="/tmp/powersaver-status" CONNECTIONS="/tmp/powersaver-connections"
netstat -an | grep ESTABLISHED | grep $SEARCH > $CONNECTIONS
for i in $EXCLUDE; do cat $CONNECTIONS | grep -v $i > $CONNECTIONS done
if [ ! -f "$STATUSFILE" ]; then echo 1 > $STATUSFILE fi
if [ "$(cat $CONNECTIONS)" == "" ]; then if [ $(cat $STATUSFILE) -eq $ATTEMPTS ]; then /raid/data/module/SHUTDOWN/system/doshutdown else echo $(expr $(cat $STATUSFILE) + 1) > $STATUSFILE fi else echo 1 > $STATUSFILE fi
|
|
|
Post by streal on Oct 11, 2008 17:08:13 GMT 7
Firstly thanks to both noname1112 and Omega for the work on their scripts.
Both solutions were easy to implement, with minimal further research, for a complete novice like me, but I have had issues with the particular job I need them to do.
I am trying to detect when my Netgear Eva8000s (media players) are turned off. The problem with Omegas script is that the media players still remain on the network awaiting commands, and reply to pings (this enables them to allow you to move the video / music etc to another room without you having to turn them on first).
It also means that the Omega script fails in this specialist case.
The script from noname1112 works unless I give it an ip exception, at which time it appears to ignore all connections and just turns off the N5200 pro after the set number of retrys. I wondered if there was a formating error that has appeared on the code when noname112 pasted it into this site, but as it is a foreign language to me I can't tell.
Any help would greatly appreciated.
|
|
|
Post by jim2035 on Oct 15, 2008 17:49:11 GMT 7
I've recently got my N5200PRO up and running and have followed the steps outlined by noname1112 to perform the automatic shutdown. Everything works, up to the point where the N5200 is meant to actually shutdown ... it just doesn't I've seen in another thread, the same problem ... the /raid/data/module/SHUTDOWN/system/doshutdown script is looking for the shutdown command and the N5200 doesn't have one. So it fails to shutdown. But that thread didn't have a solution. Does anyone know what the command line is to gracefully shutdown the N5200?
|
|
|
Post by noname1112 on Oct 16, 2008 0:24:28 GMT 7
try this script. i'll test this myself this weekend, when i am back home. if the script still doesn't work, i need your output of "netstat -an". noname1112
#!/bin/bash
##########variables########## # shutdown after how many failed attempts? ATTEMPTS=4 # what are the first 3 ip numbers of your n5200? # i.e.: ip=192.168.1.5 => SEARCH=192.168.1. SEARCH= # What are the ip adresses of the devices you want to exclude from the search? # Don't exclude the ip adress of your n5200! # (i.e. EXCLUDE="192.168.1.1 192.168.1.26 192.168.1.127 192.168.1.28") EXCLUDE=""
##########Don't modify########## STATUSFILE="/tmp/powersaver-status" CONNECTIONS="/tmp/powersaver-connections" CONNECTIONS2="/tmp/powersaver-connections2"
netstat -an | grep ESTABLISHED | grep $SEARCH > $CONNECTIONS
if [ "$EXCLUDE" != "" ]; then for i in $EXCLUDE; do cat $CONNECTIONS | grep -v $i > $CONNECTIONS2 cp $CONNECTIONS2 $CONNECTIONS done fi
if [ ! -f "$STATUSFILE" ]; then echo 1 > $STATUSFILE fi
if [ "$(cat $CONNECTIONS)" == "" ]; then if [ $(cat $STATUSFILE) -eq $ATTEMPTS ]; then /raid/data/module/SHUTDOWN/system/doshutdown else echo $(expr $(cat $STATUSFILE) + 1) > $STATUSFILE fi else echo 1 > $STATUSFILE fi
|
|
|
Post by jim2035 on Oct 16, 2008 17:46:40 GMT 7
Thanks for the reply noname1112.
If you look at what your script does, it determines that it should shutdown and then executes the command /raid/data/module/SHUTDOWN/system/doshutdown to do it. The doshutdown script in turn executes the command shutdown to shutdown the N5200PRO.
However, this is where it falls apart, the shutdown command doesn't exist on the N5200PRO so a shutdown can't happen.
Can you run the command "which shutdown" on your N5200 and tell me what it comes back with, please? Alternatively, "find / -name shutdown -print". Either of these commands should identify where the shutdown command is on your NAS.
Thanks, jim2035
|
|
|
Post by noname1112 on Oct 16, 2008 23:57:36 GMT 7
jim2035hi. k. i'll do this, when i am back home this weekend. did u follow the steps in my first post of this thread and use my last script? noname1112
|
|
|
Post by jim2035 on Oct 18, 2008 7:40:34 GMT 7
noname1112Hi back, I've followed the steps from your first post. To which I've added my own mods, but haven't used your last script. That said, I've debugged the script and tracked it through to the execution of the /raid/data/module/SHUTDOWN/system/doshutdown command and the attempt to execute the shutdown command, where it fails. jim2035
|
|
|
Post by noname1112 on Oct 19, 2008 4:31:17 GMT 7
i have no problems with the script. here is the output: root@127.0.0.1:/raid0/data/module/SHUTDOWN# find / -name shutdown -print /raid0/data/module/META/system/etc/shutdown
|
|
|
Post by jim2035 on Oct 19, 2008 10:47:35 GMT 7
noname1112If that was the full result of the find command, then I'm surprised that your N5200 is able to shutdown. The file path returned is in fact a directory (which is due to your having installed the META module) ... not the actual shutdown command itself. If you execute the command 'which shutdown', I expect that it will not return anything; that is, there is no shutdown command on your system. So I'm confused how the doshutdown script can work on your N5200. If you're able to shed some light on this, that'd be great. On another tack, I've gotten around trying to use the shutdown command. I think I understand how the N5200 shuts down when you hit the POWER button; there's a process (script) that monitors it. I've used the guts of that script (3 lines) to perform the shutdown ... and it seems to work !! jim2035
|
|