Shutdown hosts remotely on power failure – A Christmas project!

January 13, 2011 · Posted in Gentoo, Linux · 4 Comments 

The electricity network in my island is by far the worst in the entire planet. Seriously. In order to minimize the hardware failures, I bought two UPS units (Mustek & Trust). Sadly, the Trust one does not have a serial/usb port interface so I can’t control it in any way. Therefore, I came up with a neat solution. It would be cool to shutdown both computers when the battery of the Mustek UPS goes low. But how can I do that?

Legend:

Phoenix -> Host attached to Mustek UPS unit via usb

Mystical -> Host connected to Trust UPS but there is no usb/serial port to control it

Step 1: Install NUT on the Phoenix host

echo "sys-power/nut usb" >> /etc/portage/package.use/nut
emerge -av nut

Step 2: Allow user on Mystical host to shutdown the host using sudo

Mystical ~ # grep -v ^# /etc/sudoers|grep -v '^$'
root ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
%users ALL= NOPASSWD: /sbin/shutdown -h now, /sbin/shutdown -r now

Step 3:  Configure NUT

Nex@Phoenix ~ $ grep -v ^# /etc/nut/ups.conf

[mustek]
driver = blazer_usb
port = auto
desc = “UPS”
Nex@Phoenix ~ $ grep -v ^# /etc/nut/upsmon.conf
RUN_AS_USER Nex
MONITOR mustek@localhost 1 server CHANGEME master
MINSUPPLIES 1
SHUTDOWNCMD "ssh -l user -p XXXXX Mystical "sudo /sbin/shutdown -h now"
&& /sbin/shutdown -h +0"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5
Now, every time the Mustek’s battery goes low, the Phoenix host will shutdown the Mystical host remotely, by sending a shutdown command via ssh.  I may have failed when I chose a UPS unit without a usb/serial port but I am quite happy that I managed to sort this out.
ps: The tutorial assumes that you have a working ssh public-key configuration between the two hosts.

GreekBloggers.com
Patras Wireless Metropolitan Network
Planet Hellug
iloog
forum hellug