Power down proxy server and wake it up via TomatoUSB router

For a home proxy server, there is no need to have the server running 24/7 if there is no incoming traffics.  Having the server run that much will just increase kwh usage and therefore increase energy cost.

On the proxy server we need to write a script that will put the server to sleep if there is no unicast traffic on eth0 for at least 10 minutes.  The script should run on the background as the server starts up.

1) ssh to proxy server.

2) change the user to root

     su -

3) create the following script monitorTraffic.sh in the root home directory:

#!/bin/bash

#Sleep for each stat pool

SLEEP=600

rx=0

tx=0

#get net stats - rx received packets - tx transmitted packets

function stats()

{
  orx=$rx

  otx=$tx

  tx=`ethtool -S eth0 | grep tx_unicast | cut -d: -f2 | tr -d ' '`

  rx=`ethtool -S eth0 | grep rx_unicast | cut -d: -f2 | tr -d ' '`
}

# Initialize net stats rx and tx

stats

echo  `date +'%Y/%m/%d %H:%M:%S'` "Monitoring started - rx: " $rx

#loop until network has no activity for a certain period of time then suspend

while [ 1 ];

do

  sleep $SLEEP

  stats

  if [ $orx = $rx ]; then

    echo `date +'%Y/%m/%d %H:%M:%S'` " going to suspend - old rx: " $orx " new rx: " $rx

    /usr/sbin/pm-suspend

  else

    echo  `date +'%Y/%m/%d %H:%M:%S'` "rx: " $rx

  fi

done

For the script above to work you will need pm-utils installed in your server.
pacman -S pm-utils

Add the following line to /etc/rc.local

/root/monitorTraffic.sh > /var/log/monitorTraffic.log &

Now going on to setup the router side so that we can wake up our server. Assuming that we have an ip table rule that will log the word WakeMe in /var/log/messages when there is a need to wake up the proxy server in the network.

.i.e.

      iptables -t mangle -A PREROUTING -m limit --limit 1/min -j LOG --log-prefix 'WakeMe' -p tcp --dport 80

This example above will log the message WakeMe once every minute that there is an tcp request on port 80.

This next script will be used to wake up our proxy server when a request is being redirect to it.  The script will be called /jffs/WakeupServer.sh and it will run in the router as a background process.  The file should be created with the following contents:


#!/bin/sh

INTERVAL=5

NUMP=3

OLD=""

PORT=80

WOLPORT=9

MAC=A1:DB:A6:9C:78:6E

TARGET=192.168.0.YY

WOL=/usr/bin/ether-wake

LOGFILE="/tmp/var/log/wol.log"

LOCKFILE="/var/run/wakeup.pid"

if [ -f "${LOCKFILE}" ]; then

    # The file exists so read the PID

    # to see if it is still running

    MYPID=`head -n 1 "${LOCKFILE}"`

    TEST_RUNNING=`ps | egrep "$MYPID.*WakeupServer.sh" | grep -v "grep"`

    if [ -z "${TEST_RUNNING}" ]; then

       # The process is not running

       # Echo current PID into lock file

       echo $$ > "${LOCKFILE}"

    else

       exit 0

    fi

else

  echo $$ > "${LOCKFILE}"

fi

echo "AUTO WOL PID $$ Script started at" `date` >> $LOGFILE

while sleep $INTERVAL;do

NEW=`cat /var/log/messages | egrep 'WakeMe.*DPT=(80|8080|22)' | tail -1 | awk '{ print $1 $2 $3 }'`

if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then

   if ping -qc $NUMP $TARGET >/dev/null; then

      echo "NOWAKE $TARGET was accessed and is already alive at" `date` >> $LOGFILE

   else

      echo "wake on lan at" `date` >> $LOGFILE

      $WOL $MAC

      sleep 600

   fi

   OLD=$NEW

fi

done

The script above will search the messages log file for the WakeMe message that has a destination port of either 80,8080 or 22. This script will check for this message every 5 seconds.  Once the message is found it will issue the WOL command to the target server and then it will go to sleep for 10 minutes.
 
We now need to go back to our router admin page and setup this scripts to run as the router initializes. Go the the script admin page on your router (http://192.168.0.1/admin-scripts.asp) and do the following:

1) go to Wan UP tab and enter the following line:

    /jffs/WakeupServer.sh &

3) save

 

Rate: 

0
No votes yet