Imported nut changes from glotzi.

This commit is contained in:
Arne Fitzenreiter
2009-10-25 12:07:21 +01:00
parent c8119900f6
commit 4ad79fe6b5
6 changed files with 268 additions and 10 deletions

173
src/initscripts/init.d/nut Normal file
View File

@@ -0,0 +1,173 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: nut
# Required-Start: $local_fs $syslog $network
# Required-Stop: $local_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Network UPS Tools initscript
# Description: This script take care of starting and stopping the
# Network UPS Tools components. When needed, it also
# handle the UPS hardware shutdown.
### END INIT INFO
# Author: Arnaud Quette <aquette@debian.org>
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
NAME=nut
DESC="Network UPS Tools"
DEFAULT=/etc/sysconfig/nut
CONFIG=/etc/nut/nut.conf
. /etc/sysconfig/rc
. $rc_functions
# set upsd specific options. use "man upsd" for more info
UPSD_OPTIONS=""
# set upsmon specific options. use "man upsmon" for more info
UPSMON_OPTIONS=""
# Include defaults if available (transition period)
if [ -f $DEFAULT ] ; then
. $DEFAULT
fi
# Include NUT nut.conf
if [ -f $CONFIG ] ; then
. $CONFIG
fi
# Explicitly require the configuration to be done in /etc/nut/nut.conf
if [ "x$MODE" = "xnone" ] ; then
log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
log_failure_msg "and then set MODE to a suitable value in $CONFIG to enable it."
# exit success to avoid breaking the install process!
exit 0
fi
pid_dir=/var/run/nut
upsmon_pid=${pid_dir}/upsmon.pid
upsd_pid=${pid_dir}/upsd.pid
upsd=/usr/sbin/upsd
upsdrvctl=/usr/bin/upsdrvctl
upsmon=/usr/sbin/upsmon
log=">/dev/null 2>/dev/null"
# Check if /var/run/nut exists and has the correct perms
check_var_directory() {
[ ! -d ${pid_dir} ] && mkdir -p ${pid_dir} \
&& chown root:nut ${pid_dir} \
&& chmod 770 ${pid_dir}
}
start_stop_server () {
case "$MODE" in
standalone|netserver)
case "$1" in
start)
! $upsdrvctl start >/dev/null 2>&1 && boot_mesg "(upsdrvctl failed)"
$upsd $UPSD_OPTIONS >/dev/null 2>&1
;;
stop)
$upsd -c stop >/dev/null 2>&1
! $upsdrvctl stop >/dev/null 2>&1 && boot_mesg "(upsdrvctl failed)"
;;
esac
;;
none|netclient|*)
return 1
;;
esac
}
start_stop_client () {
case "$MODE" in
standalone|netserver|netclient)
case "$1" in
start)
$upsmon $UPSMON_OPTIONS >/dev/null 2>&1
;;
stop)
$upsmon -c stop >/dev/null 2>&1
;;
esac
;;
none|*)
return 1
;;
esac
}
case "$1" in
start)
boot_mesg "Starting $DESC"
check_var_directory
start_stop_server start
start_stop_client start
;;
stop)
boot_mesg "Stopping $DESC"
start_stop_server stop
start_stop_client stop
;;
reload)
$upsd -c reload >/dev/null 2>&1
$upsmon -c reload >/dev/null 2>&1
;;
restart|force-reload)
boot_mesg "Restarting $DESC"
start_stop_client stop
start_stop_server stop
sleep 5
check_var_directory
start_stop_server start
start_stop_client start
;;
poweroff)
flag=`sed -ne 's#^ *POWERDOWNFLAG *\(.*\)$#\1#p' /etc/nut/upsmon.conf`
wait_delay=`sed -ne 's#^ *POWEROFF_WAIT= *\(.*\)$#\1#p' $CONFIG`
if [ -f "$flag" ] ; then
if $upsmon -K >/dev/null 2>&1 ; then
boot_mesg "Shutting down the UPS ..."
sleep 1
if $upsdrvctl shutdown ; then
sleep 5
boot_mesg "Waiting for UPS to cut the power"
else
boot_mesg "Shutdown failed."
boot_mesg "Waiting for UPS batteries to run down"
fi
if [ "$wait_delay" ] ; then
boot_mesg " (will reboot after $wait_delay) ..."
sleep "$wait_delay"
/etc/init.d/reboot stop
fi
else
boot_mesg "Power down flag is not set (UPS shutdown not needed)"
fi
else
if [ -z "$flag" ] ; then
boot_mesg "##########################################################"
boot_mesg "## POWERDOWNFLAG is not defined in /etc/nut/upsmon.conf ##"
boot_mesg "## ##"
boot_mesg "## Please read the Manual page upsmon.conf(5) ##"
boot_mesg "##########################################################"
fi
fi
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|reload|restart|force-reload|poweroff}" >&2
exit 1
;;
esac
exit 0