43f7092c227365d2a3722f995addbbdc4210e80a
[people/pmueller/ipfire-2.x.git] / src / initscripts / init.d / nut
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: nut
4 # Required-Start: $local_fs $syslog $network
5 # Required-Stop: $local_fs $syslog $network
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Short-Description: Network UPS Tools initscript
9 # Description: This script take care of starting and stopping the
10 # Network UPS Tools components. When needed, it also
11 # handle the UPS hardware shutdown.
12 ### END INIT INFO
13
14 # Author: Arnaud Quette <aquette@debian.org>
15
16 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
17
18 NAME=nut
19 DESC="Network UPS Tools"
20 DEFAULT=/etc/sysconfig/nut
21 CONFIG=/etc/nut/nut.conf
22
23 . /etc/sysconfig/rc
24 . $rc_functions
25
26 # set upsd specific options. use "man upsd" for more info
27 UPSD_OPTIONS=""
28
29 # set upsmon specific options. use "man upsmon" for more info
30 UPSMON_OPTIONS=""
31
32 # Include defaults if available (transition period)
33 if [ -f $DEFAULT ] ; then
34 . $DEFAULT
35 fi
36
37 # Include NUT nut.conf
38 if [ -f $CONFIG ] ; then
39 . $CONFIG
40 fi
41
42 # Explicitly require the configuration to be done in /etc/nut/nut.conf
43 if [ "x$MODE" = "xnone" ] ; then
44 log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
45 log_failure_msg "and then set MODE to a suitable value in $CONFIG to enable it."
46 # exit success to avoid breaking the install process!
47 exit 0
48 fi
49
50 pid_dir=/var/run/nut
51 upsmon_pid=${pid_dir}/upsmon.pid
52 upsd_pid=${pid_dir}/upsd.pid
53 upsd=/usr/sbin/upsd
54 upsdrvctl=/usr/bin/upsdrvctl
55 upsmon=/usr/sbin/upsmon
56 log=">/dev/null 2>/dev/null"
57
58 # Check if /var/run/nut exists and has the correct perms
59 check_var_directory() {
60 [ ! -d ${pid_dir} ] && mkdir -p ${pid_dir} \
61 && chown root:nut ${pid_dir} \
62 && chmod 770 ${pid_dir}
63 }
64
65 start_stop_server () {
66 case "$MODE" in
67 standalone|netserver)
68 case "$1" in
69 start)
70 ! $upsdrvctl start >/dev/null 2>&1 && boot_mesg "(upsdrvctl failed)"
71 $upsd $UPSD_OPTIONS >/dev/null 2>&1
72 ;;
73 stop)
74 $upsd -c stop >/dev/null 2>&1
75 ! $upsdrvctl stop >/dev/null 2>&1 && boot_mesg "(upsdrvctl failed)"
76 ;;
77 esac
78 ;;
79 none|netclient|*)
80 return 1
81 ;;
82 esac
83 }
84
85 start_stop_client () {
86 case "$MODE" in
87 standalone|netserver|netclient)
88 case "$1" in
89 start)
90 $upsmon $UPSMON_OPTIONS >/dev/null 2>&1
91 ;;
92 stop)
93 $upsmon -c stop >/dev/null 2>&1
94 ;;
95 esac
96 ;;
97 none|*)
98 return 1
99 ;;
100 esac
101 }
102
103 case "$1" in
104
105 start)
106 boot_mesg "Starting $DESC"
107 check_var_directory
108 start_stop_server start
109 start_stop_client start
110 ;;
111
112 stop)
113 boot_mesg "Stopping $DESC"
114 start_stop_server stop
115 start_stop_client stop
116 ;;
117
118 reload)
119 $upsd -c reload >/dev/null 2>&1
120 $upsmon -c reload >/dev/null 2>&1
121 ;;
122
123 restart|force-reload)
124 boot_mesg "Restarting $DESC"
125 start_stop_client stop
126 start_stop_server stop
127 sleep 5
128 check_var_directory
129 start_stop_server start
130 start_stop_client start
131 ;;
132
133 poweroff)
134 flag=`sed -ne 's#^ *POWERDOWNFLAG *\(.*\)$#\1#p' /etc/nut/upsmon.conf`
135 wait_delay=`sed -ne 's#^ *POWEROFF_WAIT= *\(.*\)$#\1#p' $CONFIG`
136 if [ -f "$flag" ] ; then
137 if $upsmon -K >/dev/null 2>&1 ; then
138 boot_mesg "Shutting down the UPS ..."
139 sleep 1
140 if $upsdrvctl shutdown ; then
141 sleep 5
142 boot_mesg "Waiting for UPS to cut the power"
143 else
144 boot_mesg "Shutdown failed."
145 boot_mesg "Waiting for UPS batteries to run down"
146 fi
147 if [ "$wait_delay" ] ; then
148 boot_mesg " (will reboot after $wait_delay) ..."
149 sleep "$wait_delay"
150 /etc/init.d/reboot stop
151 fi
152 else
153 boot_mesg "Power down flag is not set (UPS shutdown not needed)"
154 fi
155 else
156 if [ -z "$flag" ] ; then
157 boot_mesg "##########################################################"
158 boot_mesg "## POWERDOWNFLAG is not defined in /etc/nut/upsmon.conf ##"
159 boot_mesg "## ##"
160 boot_mesg "## Please read the Manual page upsmon.conf(5) ##"
161 boot_mesg "##########################################################"
162 fi
163 fi
164 ;;
165
166 *)
167 N=/etc/init.d/$NAME
168 echo "Usage: $N {start|stop|reload|restart|force-reload|poweroff}" >&2
169 exit 1
170 ;;
171 esac
172
173 exit 0