File: upsdrvsvcctl

package info (click to toggle)
nut 2.8.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,356 kB
  • sloc: ansic: 123,904; sh: 14,718; cpp: 12,558; makefile: 5,212; python: 1,114; perl: 855; xml: 47
file content (344 lines) | stat: -rwxr-xr-x 14,013 bytes parent folder | download
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/sh
#
# Copyright (C) 2016-2018 Eaton
# Copyright (C) 2020-2024 Jim Klimov <jimklimov+nut@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#! \file    upsdrvsvcctl(.in)
#  \author  Jim Klimov <EvgenyKlimov@eaton.com>
#  \brief   Manage NUT devices registered as service-unit instances
#

if [ -z "${SERVICE_FRAMEWORK-}" ] ; then
    [ -x /usr/sbin/svcadm ] && [ -x /usr/sbin/svccfg ] && [ -x /usr/bin/svcs ] && \
        SERVICE_FRAMEWORK="smf"
    [ -z "${SERVICE_FRAMEWORK-}" ] && \
        [ -x /bin/systemctl ] && \
        SERVICE_FRAMEWORK="systemd"
fi

VERB=""
CMD=""
CMDARG=""
# Note: some frameworks separate the commands for acting and for reporting
CMDREPORT=""
CMDREPORTARG=""
case "$SERVICE_FRAMEWORK" in
    smf) CMD="/usr/sbin/svcadm"
        CMDREPORT="/usr/bin/svcs"
        [ -n "${ENUMERATOR-}" ] && export ENUMERATOR || ENUMERATOR="/usr/local/ups/libexec/nut-driver-enumerator.sh"
        ;;
    systemd) CMD="/bin/systemctl"
        CMDREPORT="${CMD}"
        [ -n "${ENUMERATOR-}" ] && export ENUMERATOR || ENUMERATOR="/usr/local/ups/libexec/nut-driver-enumerator.sh"
        ;;
    *)  echo "Unrecognized SERVICE_FRAMEWORK: $SERVICE_FRAMEWORK" >&2 ; exit ;;
esac
[ -n "${UPSDRVCTL-}" ] && export UPSDRVCTL || UPSDRVCTL='/usr/local/ups/sbin/upsdrvctl'
PACKAGE_VERSION='2.8.3'

# Should be populated by configure script during NUT build:
NUT_WEBSITE_BASE='https://www.networkupstools.org/historic/v2.8.3'
# Fallback, no trailing slash!
[ -n "${NUT_WEBSITE_BASE-}" ] || NUT_WEBSITE_BASE='https://www.networkupstools.org'

usage() {
    # Note: version header differs from UPS_VERSION in binaries that
    # might also have the git-version suffixed during build time
    cat << EOF
Network UPS Tools - UPS driver controller ${PACKAGE_VERSION}
Starts and stops UPS drivers via system service instances, see
the $ENUMERATOR
script for more details.

usage: $0 [OPTIONS] (start | stop | shutdown) [<ups>]

Options:
  -h            	display this help
  -V            	display upsdrvctl binary program version (should
                	not differ from script version above, but...)
  -t            	testing mode - prints actions without doing them
  -D            	raise debugging level
  --timeout-cmd <prog>	service management calls will be time-limited
  --timeout-args <arg>	by calling the specified program with its args.
                    	By default, if coreutils timeout is found, it
                    	would be used to limit service calls by 90 sec.
  start         	start all UPS drivers in ups.conf
  start <ups>   	only start driver for UPS <ups>
  stop          	stop all UPS drivers in ups.conf
  stop <ups>    	only stop driver for UPS <ups>

Note: the "shutdown" options from original upsdrvctl are not currently
directly supported by this service management framework wrapper; instead
they are passed to the native upsdrvctl binary (your current user account
should have sufficient permissions to do that all):
  shutdown      	shutdown all UPS drivers in ups.conf
  shutdown <ups>	only shutdown UPS <ups>

usage: $0 [OPTIONS] resync
  resync        	call $ENUMERATOR
                	to update the mapping of service instances for
                	NUT drivers to device sections in 'ups.conf'

usage: $0 [OPTIONS] reconfigure
  reconfigure   	call $ENUMERATOR
                	to remove and re-create the mapping of all service
                	instances for NUT drivers to device sections in
                	'ups.conf' e.g. after a NUT package upgrade

usage: $0 [OPTIONS] list [<ups>]
  list          	call $ENUMERATOR
                	to list the mapping of service instances to device sections
  list <ups>    	(optionally return the service instance name for one device)

usage: $0 [OPTIONS] status [<ups>]
  status        	report run-time status for each driver in ups.conf
                	and call $ENUMERATOR
                	to list the mapping of service instances to device sections
                	and report run-time status for each one that was matched
  status <ups>  	(optionally return the status info only for one device)
                	Fields: SVC_NAME SVC_STATE UPSNAME UPSDRV RUNNING PF_PID S_RESPONSIVE S_PID S_STATUS
                	(SVC_* according to service management framework; and
                	PF_* = according to PID file, if any; S_* = via socket
                	protocol -- as defined by upsdrvctl)
usage: $0 [OPTIONS] show-config [<ups>]
  show-config <ups>	output config section from ups.conf for device <ups>
  show-config   	...or all devices if no <ups> argument was passed

For more information please Read The Fine Manual ('man upsdrvsvcctl') and/or see
        ${NUT_WEBSITE_BASE}/docs/man/upsdrvsvcctl.html
Also check documentation of nut-driver-enumerator and plain old upsdrvctl tools
EOF
}

ACTION=""
SVCINST=""
DRYRUN=""
DEBUG=0
# Note: DEBUG is UNUSED_PARAM so far

# Optionally use Coreutils timeout to limit the
# (potentially hanging) calls to systemd tools...
# Should not hurt with SMF too, if it ever misbehaves.
TIMEOUT_CMD=""
TIMEOUT_ARGS=""
if which timeout 2>/dev/null >/dev/null ; then
    # Systemd default timeout for unit start/stop
    TIMEOUT_CMD="timeout"
    TIMEOUT_ARGS="90s"
fi

while [ $# -gt 0 ]; do
    case "$1" in
        resync) eval $DRYRUN $ENUMERATOR ; exit $? ;;
        reconf|reconfigure) eval $DRYRUN $ENUMERATOR --reconfigure ; exit $? ;;
        list)
            if [ -n "$2" ] ; then
                eval $ENUMERATOR --get-service-for-device "$2" ; exit $?
            else
                eval $ENUMERATOR --list-services-for-devices ; exit $?
            fi
            ;;
        show-config)
            if [ -n "$2" ] ; then
                eval $ENUMERATOR --show-device-config "$2" ; exit $?
            else
                eval $ENUMERATOR --show-all-configs ; exit $?
            fi
            ;;
        status)
            RES=0
            STATUSES="`NUT_QUIET_INIT_BANNER=true "${UPSDRVCTL}" status $2`" || RES=$?
            if [ x"${STATUSES}" = x ]; then
                exit $RES
            fi

            SVCINSTS="`$0 list $2`" || RES=$?
            # Use a common approach below, even if we only got one instance name from list()
            if [ -n "$2" ] && [ -n "${SVCINSTS}" ] ; then SVCINSTS="${SVCINSTS}	$2" ; fi

            # Accomodate svc:/system/power/nut-driver:dummy (SMF)
            # or         nut-driver@dummy.service           (systemd)
            case "$SERVICE_FRAMEWORK" in
                smf) SVC_NAME_WIDTH="39" ; SVC_STATE_WIDTH="11" ;;
                systemd) SVC_NAME_WIDTH="30" ; SVC_STATE_WIDTH="23" ;;
            esac

            echo "$STATUSES" | ( COUNT=0; while IFS='' read LINE ; do
                COUNT="`expr $COUNT + 1`"
                if [ "$COUNT" = 1 ] ; then
                    # Header line
                    printf '%-'"${SVC_NAME_WIDTH}"'s\t%'"${SVC_STATE_WIDTH}"'s\t%s\n' "SVC_NAME" "SVC_STATE" "$LINE"
                    continue
                fi
                DEV_NAME="`echo "$LINE" | awk '{print $1}'`"
                SVC_NAME="`echo "$SVCINSTS" | awk '($NF == "'"${DEV_NAME}"'") {print $1}'`" && [ -n "$SVC_NAME" ] || SVC_NAME="N/A"
                if [ x"$SVC_NAME" = "N/A" ]; then
                    SVC_STATE="N/A"
                else
                    case "$SERVICE_FRAMEWORK" in
                        smf) SVC_STATE="`$CMDREPORT -Hostate ${SVC_NAME} 2>/dev/null`" \
                            && [ -n "$SVC_STATE" ] && [ 1 = `echo "$SVC_STATE" | wc -l` ] \
                            || SVC_STATE="N/A"
                            ;;
                        systemd) SVC_STATE=""
                            CMDRES=0
                            OUT="`$CMDREPORT is-enabled "${SVC_NAME}" 2>/dev/null`" || CMDRES=$?
                            if [ x"$OUT" != x ] ; then SVC_STATE="${OUT}" ; else
                                [ 0 = "$CMDRES" ] && SVC_STATE="enabled" || SVC_STATE="disabled"
                            fi
                            # At this point we should have some value, so others below
                            # may be or not be suffixed with a comma.

                            # The masked unit state has no separate query in some (all?)
                            # systemd versions and can be reported as part of the above
                            CMDRES=0
                            OUT="`$CMDREPORT is-masked "${SVC_NAME}" 2>/dev/null`" || CMDRES=$?
                            if [ x"$OUT" != x ] ; then
                                if [ x"${SVC_STATE}" != xmasked ] && [ x"${SVC_STATE}" != x"${OUT}" ] ; then
                                    SVC_STATE="${SVC_STATE},${OUT}"
                                fi
                            else
                                [ 0 = "$CMDRES" ] && SVC_STATE="${SVC_STATE},masked" || true
                            fi

                            CMDRES=0
                            OUT="`$CMDREPORT is-active "${SVC_NAME}" 2>/dev/null`" || CMDRES=$?
                            if [ x"$OUT" != x ] ; then
                                SVC_STATE="${SVC_STATE},${OUT}"
                            else
                                [ 0 = "$CMDRES" ] && SVC_STATE="${SVC_STATE},active" || true
                            fi

                            CMDRES=0
                            OUT="`$CMDREPORT is-failed "${SVC_NAME}" 2>/dev/null`" || CMDRES=$?
                            if [ x"$OUT" != x ] ; then
                                if [ x"${OUT}" != xactive ] && [ x"${OUT}" != xinactive ] ; then
                                    SVC_STATE="${SVC_STATE},${OUT}"
                                fi
                            else
                                [ 0 = "$CMDRES" ] && SVC_STATE="${SVC_STATE},failed" || true
                            fi
                            ;;
                    esac
                fi
                printf '%-'"${SVC_NAME_WIDTH}"'s\t%'"${SVC_STATE_WIDTH}"'s\t%s\n' "$SVC_NAME" "$SVC_STATE" "$LINE"
            done )
            exit $RES
            ;;
        start|stop)
            ACTION="$1"
            if [ -n "$2" ] ; then
                SVCINST="`$ENUMERATOR --get-service-for-device "$2"`" || exit
                shift
            fi
            ;;
        shutdown)
            echo "NOTE: Action '$1' is not implemented via services currently, will call upsdrvctl" >&2
            echo "Stopping the driver service instance(s) to release exclusive resources, if any..." >&2
            RES=0
            $0 stop $2
            "${UPSDRVCTL}" shutdown $2 || RES=$?
            echo "Starting the driver service instance(s) so they can reconnect when the UPS returns..." >&2
            $0 start $2
            exit $RES
            ;;
        --timeout-length|--timeout-len|--timeout-args)
            TIMEOUT_ARGS="$2"
            shift
            ;;
        --timeout-cmd)
            if [ -n "$2" ] ; then
                if [ -x "$2" ] || which "$2" 2>/dev/null >/dev/null ; then
                    TIMEOUT_CMD="$2"
                else
                    echo "ERROR: Received a '$2' argument for $1, and can not find such program; clearing the option so the basic functionality can proceed" >&2
                    TIMEOUT_CMD=""
                    TIMEOUT_ARGS=""
                fi
            else
                echo "INFO: Received an empty argument for $1, clearing the option" >&2
                TIMEOUT_CMD=""
                TIMEOUT_ARGS=""
            fi
            shift
            ;;
        -t) DRYRUN="echo" ;;
        -h) usage; exit 0 ;;
        -V) "${UPSDRVCTL}" -V ; exit ;;
        -D) DEBUG="`expr $DEBUG + 1`" ;;
        -r|-u) echo "Option '$1 $2' is not implemented via services currently" >&2 ; shift;;
        *)  echo "Unrecognized argument: $1" >&2 ; exit ;;
    esac
    shift
done

if [ -z "$ENUMERATOR" ] || [ ! -s "$ENUMERATOR" ] || [ ! -x "$ENUMERATOR" ] ; then
    echo "ENUMERATOR script (nut-driver-enumerator.sh) not found!" >&2
    exit 1
fi

if [ -z "$ACTION" ]; then
    echo "No action was requested!" >&2
    exit 1
fi

if [ -n "$TIMEOUT_CMD" ]; then
    echo "INFO: Detected presence of $TIMEOUT_CMD so will limit service management calls by passing the args: $TIMEOUT_ARGS" >&2
fi

if [ -z "$SVCINST" ]; then
    SVCINST="`$ENUMERATOR --list-services`" || exit
fi

# TODO: Support shutdown of one or all UPSes by stopping its service
# and then calling the original upsdrvctl on it?
case "$ACTION" in
    start)
        VERB="Starting"
        case "$SERVICE_FRAMEWORK" in
            smf) CMDARG="enable -ts" ;;
            systemd) CMDARG="start" ;;
        esac
        ;;
    stop)
        VERB="Stopping"
        case "$SERVICE_FRAMEWORK" in
            smf) CMDARG="disable -ts" ;;
            systemd) CMDARG="stop" ;;
        esac
        ;;
    *)  echo "Unrecognized ACTION: $ACTION" >&2 ; exit ;;
esac

for INST in $SVCINST ; do
    echo "$VERB $INST ..." >&2
    $DRYRUN $TIMEOUT_CMD $TIMEOUT_ARGS $CMD $CMDARG "$INST" &
done
wait

case "$SERVICE_FRAMEWORK" in
    smf)
        sleep 1
        echo "Post-process clearing services that failed early..." >&2
        for INST in $SVCINST ; do
            echo "Clearing $INST (if it got broken) ..." >&2
            $DRYRUN $TIMEOUT_CMD $TIMEOUT_ARGS $CMD clear "$INST" &
        done
        ;;
esac

wait