Mon, 14 May 2007

Powering down machines, wakeonlan

I used to do everything on my laptop, but now I work from home so much, I have a server machine as well. It is my test box for lguest and also the web proxy, but I don't really need it to run all the time. So I wrote a cron job, which runs this every minute:

#! /bin/sh

date >> /tmp/halter.log

# So, has everyone on been idle for over 10 minutes?
PEOPLE=`who -u | awk '{print $2}'`
if [ -n "$PEOPLE" ]; then
        if [ -n "$(cd /dev; find $PEOPLE -mmin -10)" ]; then
                echo People still active: $PEOPLE >> /tmp/halter.log
                exit 0
        fi
fi

# Has squid proxy been unused for over 30 minutes?
if [ -n "`find /var/log/squid/access.log -mmin -30`" ]; then
        echo Squid log `ls -l /var/log/squid/access.log` >> /tmp/halter.log
        exit 0
fi

# Now, is load average <= .10?
if awk '{ if ($1 < 0.1) exit 1; }' < /proc/loadavg; then
        echo Loadavg too high: `cat /proc/loadavg` >> /tmp/halter.log
        exit 0
fi

/sbin/shutdown -P +1 "Automatic shutdown due to idle" >> /tmp/halter.log < /dev/null 2>&1

I ran "sudo ethtool -s eth0 wol g" once (it seems to be sticky), and then had to go into the BIOS and turn it on there. Now I can wake up the machine with "wakeonlan 00:16:76:E3:60:57" (I put that in a script for Alli).

The final ingredient is only half-done. There is a way to get Firefox to do direct if the proxy isn't answering, but it's not as simple as a checkbox. I put the line "function FindProxyForURL(url, host) { return "PROXY 192.168.5.133:3128; DIRECT"; }" in /etc/proxy.pac and put "/etc/proxy.pac" in Preferences->Connection Settings->Automatic Proxy Configuration URL. This causes very low-delay failover when the proxy is off.

Ideally, I'd want the wake-on-lan packet sent out when I try to access the machine, and then go direct while it's booting. I was initially thinking a firefox extension, but actually a little libnetfilter_log program makes more sense: the machine would then wake up on any traffic.

My other issue is that I run experimental kernels which don't have suspend set up, so I'm booting every time. Ubuntu boots quite quickly, but I did have to tune2fs the filesystems from fsck-every-100-mounts to every 10 weeks.

Wake-on-lan seems under-developed and under-utilized, but it seems like a really easy way to be environmentally conscious.


[/tech] permanent link