1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#! /bin/sh
#
# powerfail This script is run when the UPS tells the system the power has
# gone. Tell everybody and start the shutdown based on the failure
# type. This script will also being run when the power comes up
# again.
#
# Version: /etc/init.d/powerfail (v1.1)
#
# Author: Tom Webster <webster@kaiwan.com>
# Modified-By: Brian White <bcwhite@verisim.com>
#
failtime=+5 # shutdown delay from initial power failure
btrytime=now # shutdown delay from low-battery warning
failmsg="LINE POWER FAILURE -- SWITCHED TO BATTERY BACKUP"
btrymsg="BACKUP BATTERY LOW -- EMERGENCY SHUTDOWN"
cablmsg="BAD UPS CABLE -- SYSTEM MAY BE UNRELIABLE"
othrmsg="LINE POWER FAILURE -- SHUTTING DOWN SYSTEM"
okaymsg="LINE POWER RESTORED -- RESUMING NORMAL OPERATION"
# Set the path.
PATH=/sbin:/etc:/bin:/usr/bin
# Set location of upsstatus file
statpath="/etc/upsstatus"
# Set location of file containing PID of running shutdowns
spidpath="/var/run/shutdown.pid"
# See what happened.
case "$1" in
start)
# Called with a powerfail event, check to see if a shutdown is running
if [ -f $spidpath ]
then
# Shutdown is running, kill it to process the new event
shutdown -c >/dev/null 2>&1
fi
# Get power problem and act on it
if [ -r $statpath ]
then
stats=`head -1 $statpath`
case "$stats" in
FAIL) # Power is down
shutdown -h $failtime "$failmsg" &
# switch off backlight, and force disks to spin down.
# fblevel in pmud-utils now!
fblevel 0
[ "`cat /proc/ide/hde/model`" != "(none)" ] && hdparm -f -S 1 -Y /dev/hde
hdparm -f -S 1 -Y /dev/hda
;;
SCRAM) # Battery is low
shutdown -h $btrytime "$btrymsg" &
fblevel 0
[ "`cat /proc/ide/hde/model`" != "(none)" ] && hdparm -f -S 1 -Y /dev/hde
hdparm -f -S 1 -Y /dev/hda
;;
CABLE) # Possible bad cable
echo "$cablmsg" | wall
;;
*) # Unknown message, assume power is down
shutdown -h $btrytime "$othrmsg" &
fblevel 0
[ "`cat /proc/ide/hde/model`" != "(none)" ] && hdparm -f -S 1 -Y /dev/hde
hdparm -f -S 1 -Y /dev/hda
;;
esac
else
# powerfail called, and upsstatus dosen't exist.
# Assume user is using powerd, and shutdown.
shutdown -h $failtime "$othrmsg" &
fblevel 0
[ "`cat /proc/ide/hde/model`" != "(none)" ] && hdparm -f -S 1 -Y /dev/hde
hdparm -f -S 1 -Y /dev/hda
fi
;;
now)
# Called with a powerfail event, check to see if a shutdown is running
if [ -f $spidpath ]
then
# Shutdown is running, kill it to process the new event
shutdown -c >/dev/null 2>&1
fi
# Power is going down _now_
shutdown -h $btrytime "$btrymsg" &
;;
stop)
# switch backlight back on
fblevel 12
# Ok, power is good again. Say so on the console.
if [ -f $spidpath ]
then
# Only cancel if shutdown is running (system boot will call this)
shutdown -c "$okaymsg"
fi
;;
*)
echo "Usage: /etc/init.d/powerfail {start|now|stop}" >&2
exit 1
;;
esac
exit 0
|