File: rc.upsd

package info (click to toggle)
upsd 2.5-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 216 kB
  • ctags: 114
  • sloc: ansic: 1,037; sh: 332; makefile: 104
file content (297 lines) | stat: -rwxr-xr-x 7,334 bytes parent folder | download | duplicates (4)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/sh
#
# rc.upsd
#
  VERSION='2.10 5-25-98'	# Michael A. Robinton <michael@bzs.org>
#
# Initialize ups monitoring services enter.
#
# Call with -h or ? for HOWTO and help
# 
# see 'man upsd(8)' for further clarification
# of the parameters listed below

# Debug yes | YES, to print the 'upsd' command
# line to the console instead of excuting it.
DEBUG=no

##### ONLY define one HOST or DEVICE #########
#                                            #
# Serial device used for monitoring the ups  #
# leave blank or do not define if SLAVE      #
#                                            #
DEVICE=/dev/cua1
#                                            #
# Host name to query if SLAVE                #
# DEVICE (above) overides this setting       #
# if you really want it to work, use IP no.  #
# since 'gethostbyname' may not work when    #
# the file system is dismounted in -w mode   #
HOST=192.168.1.164
#                                            #
##############################################

# Do not define if STANDALONE
PORT=2043

# Poll time in seconds for UPS interrogations
# less than 10 seconds on the master, longer on slave
# COMMENT: do not put negative time here in this script.
# If POLL is negative, returns UPS status
POLL=10

# Delay # of poll times before shutdown to 
# avoid spurious glitches, set to 0 for SLAVES
COUNT=2

# Wait time in seconds between forground UPS
# status checks (-w option for RO file systems).
# Activated by -w option to this script
WAIT=10

# Verbose WAIT mode, Verbose=NO is overidden by command line '-v'
VERBOSE=no

# YES | yes, for automatic shutdown of UPS
KILL=yes

# location of upsd daemon
DAEMON=/sbin/upsd

# yes | YES, if support for enhanced init 'powerfailnow' 
# action is desired. Valid for sysvinit 2.52 and up. Don't use
# this for slaves unless they are on the same UPS as the master.
ENHANCED=yes

# Dummy test mode specifications here.
# local, look for file /tmp/upsds supplied by script 'upsdummy'
# master, serve status from file /tmp/upsds to the NET
DUMMY=
#DUMMY=master
#DUMMY=local

# Power wait message while looping
MESSAGE="..waiting, RO filesystem, OK to push RESET"

#########################################################
#### end of parameters, no changes beyond this point ####
#########################################################

####### Subroutine to Execute or Echo COMMAND ###########

GetStatusIfNotDebug () {
    case $DEBUG in

	y* | Y* )	# Preserve cosmetic appearance of redirect
	    echo "$COMMAND 2>&1 1> /dev/null"
	    ;;
	 * )		# EXECUTE if not debug mode
	    $COMMAND 2>&1 1> /dev/null
	    return $?	# return status
	    ;;
    esac
} # end subroutine IfDebug

############## Begin MAIN ###############################

# configure the upsd daemon

if [ -z $PORT ]; then		# port or none
  PORT=' -m'			# illegal with HOST
  HOST=
else
  PORT=" -p ${PORT}"
fi

if [ -z $DEVICE ]; then		# select source of data
  if [ -z "$HOST" ]; then
    echo "You must specify a serial DEVICE or HOST & PORT"
    exit 1
  fi
  DEVICE="-s ${HOST}"
				# no kill if slave
  KILL=
else
  HOST=
fi

if [ ! -z $COUNT ]; then
  COUNT=" -c ${COUNT}"
fi

case $VERBOSE in		# overides VERBOSE=no above

    y* | Y* )
	VERBOSE=' -v'
	;;
    * )
	VERBOSE=
	;;
esac

case $ENHANCED in

    y* | Y* )
	ENHANCED=' -e'
	;;
    * )
	ENHANCED=
	;;
esac

case $DUMMY in

    m* )
	DUMMY=' -d master'
	;;
    l* )
	DUMMY=' -d local'
	;;
    * )
	DUMMY=
	;;
esac

# Get the command line arguments
RECALL=
FOREGROUND=
while [ $# -gt 0 ]; do
  case $1 in			# if powerfail wait

    -w* ) FOREGROUND=' -w'	# forground flag, check below
	;;			# for script or daemon
    -r* ) RECALL=' -r'
	;;
    -v* ) VERBOSE=' -v'		# verbose if foreground -WAIT present
	;;
      * )	# anything else, display this help and exit
    more <<EOF

        HELP for $0 version $VERSION

  See:	man upsd (8) for daemon options

  These are script file options which are similar

  -h ? 	Display this help

  -r*	Recall previous UPS status from /etc/upstatus at startup.
	Do NOT specify at system boot, that will cause SIGPWR
	'init' failures dependent on your inittab.

  -v*	Verbose forground reporting (ignored unless -w specified)
	Continuously report daemon return codes:
	status CODE, UPS status message, user message. May also 
	be specified in the configuration section of this script.

  -w*	Quietly wait in forground unless -v specified, do not 
	become a daemon do not issue SIGPWR, do not return
	unless power is restored.

   Parameters that must be set at the beginning of this script

   DEBUG yes,YES | no,NO - Print the 'upsd' command line
	to the console instead of executing. Useful to
	determine that all setup and calling parameters are OK

   Specify only one:
	DEVICE if master / HOST IP/name if slave.
	** note: host 'name' not recommended, only work if name
	resolution returns a valid IP number in runlevels 0,1,6.

   PORT number if master or slave, none if master mode disabled.

   POLL time in seconds for UPS interrogations.
	Less than 10 seconds for master, longer for slave.

   COUNT number of POLL times before reporting outage to
	avoid spurious power glitches. Set to '0' for slave.

   WAIT time in seconds for UPS interrogations in forground mode.

   Report VERBOSE while WAITing. This mode is NOT debounced as is
   silent WAIT. i.e. Count == 0, recommend only for DEBUG or slaves.

   KILL yes,YES | no,NO - Attempt to kill UPS in WAIT mode.

   ENHANCED yes,YES | no,NO - Support init 'powerfailnow'. Don't
   use this for a slave unless it on the same UPS as the master.

   DUMMY master, local, (none) - See the README for 'upsdummy'

   DAEMON, location of upsd. Usually /sbin/upsd.

   MESSAGE to echo in 'verbose - wait' mode.   
EOF
	exit 0
	;;
  esac
  shift
done

if [ ! -z "$FOREGROUND" ]; then
    if [ -z $WAIT ] || [ $WAIT -le 0 ]; then	# MUST be +non-zero
	WAIT=1
    fi
    if [ ! -z "$VERBOSE" ]; then
	POLL=-1
        FOREGROUND=
    else
        FOREGROUND=" -w ${WAIT}"
    fi
fi

if [ $POLL -lt 0 ] || [ ! -z "$FOREGROUND" ]; then
  case $KILL in

    y* | Y* )
	KILL=' -k'
	;;
    * )			# no kill
	KILL=
	;;
  esac
else				# no kill in detection mode
	KILL=
fi

if [ ! -z $KILL ]; then				# first try to kill
  COMMAND="${DAEMON}${RECALL}${PORT}${ENHANCED}${DUMMY} -i -1 -k ${DEVICE}"
  GetStatusIfNotDebug	# Execute COMMAND
fi

COMMAND="${DAEMON}${RECALL}${PORT}${ENHANCED}${DUMMY} \
-i ${POLL}${COUNT}${FOREGROUND} ${DEVICE}"

if [ $POLL -ge 0 ]; then		# normal
  GetStatusIfNotDebug    # Execute COMMAND
  exit $?
else					# verbose powerfail wait
  UPSTATUS=0
  while [ ${UPSTATUS} -ne 100 ]; do	# then wait for power to return
					# or we die here with low battery
    GetStatusIfNotDebug    # Execute COMMAND
    UPSTATUS=$?
    echo -n "$UPSTATUS "
    case $UPSTATUS in
        100 ) echo "Power restored ${HOST}"
                ;;
        101 ) echo "On battery ${HOST}"
                ;;
        102 ) echo "Low battery ${HOST}"
                ;;
        103 ) echo "Connect failed ${HOST}"
                ;;
         99 ) if [ ! -z $HOST ]; then
                echo " Unable to connect to ${HOST}"
              else
                echo " No status from ${DEVICE}"
              fi
                ;;

    esac
    echo "     ${MESSAGE} $(date +%H:%M:%S)"
    sleep $WAIT
  done
  exit 0
fi