When using a system to host your important (or critical) operations, being able to get informed of issues while you’re away is a very handy function. Email is good for notifications, but it isn’t always fast enough, and perhaps you don’t want to waste all that battery power to check for emails on-the-go for just that one alert you’ll maybe, perhaps, but definitely receive when you least expect it.
SMS on the other hand, is an almost instant alert method and although length-limited, should do the job just fine for those very important, must have alerts.
For these mission critical instant alerts you will need (in lack of fancier solutions) a working, configured and test 3G USB stick with a functioning SIM in it (nobody said this will not cost you…) connected to the machine, NUT and gammu.
Configuring gammu will be covered by a separate tutorial to be published later.
We assume NUT is already installed, configured and running correctly.
NUT should also be configured to hibernate the system on power failure / low battery, not shut it down. To do this, edit /etc/ups/upsmon.conf, search for
SHUTDOWNCMD "/sbin/shutdown -h +0"
comment out that line and add /or replace it with
That file, /usr/sbin/hibernate does not exist by default. You need create it with the following content, then chmod it 755:
echo disk >/sys/power/state
This procedure is also described in this post.
Edit /etc/ups/upssched.conf and define your timers/events:
AT ONBATT * START-TIMER upssms 30
AT ONLINE * CANCEL-TIMER upssms
AT ONBATT * START-TIMER pwrgone 150
AT ONLINE * CANCEL-TIMER pwrgone
AT ONLINE * EXECUTE ups-back-on-power
upssms is set to execute 30 second after power fails and upsgone at 150 seconds (two and a half minutes) after failure.
Make sure /var/run/nut/upssched exists and is owned by the nut user.
Next, edit the command script /usr/bin/upssched-cmd and add the following:
case $1 in
logger -t upssched-cmd "The UPS has been gone for 30 secs. Warn by sms..."
/bin/echo "Power failed. System will hibernate soon..." | /usr/bin/gammu --sendsms TEXT +1234567890
logger -t upssched-cmd "The UPS has been gone for 2 and a half minutes. Hibernating..."
#/usr/sbin/hibernate # direct call to hibernate is not allowed by non-root
/usr/sbin/upsmon -c fsd # call a shutdown in NUT instead
logger -t upssched-cmd "The UPS power has been restored."
/bin/echo "Power restored." | /usr/bin/gammu --sendsms TEXT +1234567890
logger -t upssched-cmd "Unrecognized command: $1"
This script uses gammu to connect to the GSM device and send the SMS.
The upssms event (which triggers 30 seconds after power failure) will send an SMS alert informing of the situation. The program then sleeps until the next event, upsgone (two minutes and a half after failure) is triggered. It is now that the hibernation script is called and the system will go to sleep.
However, if power is restored before upsgone, the ups-back-on-power event is triggered which send another SMS informing of power restore and cancels all other timers/actions.
You should make sure the configured timers (here 30 and 150 seconds) are appropriate for the backup power duration. If battery reaches critical before the timers are triggered the system will enter hibernation before alerts are sent.
All that’s left to do now is test out things (or simply wait for the next power failure to see how things work… NUT should still hibernate the system on low battery if things fail). Also make sure hibernation works on your system before changing NUT’s configuration.
All of the trigger events are logged in the system log. You can monitor /var/log/messages for output from the upssched program.