#!/bin/sh # # chkconfig: 2345 30 90 # # 2002-02-07 Nigel Metheringham <Nigel.Metheringham@InTechnology.co.uk> # made ups.conf pre-eminant, added new upsdrvctl functions, targeted for RH7.2, should # work OK on RH 6.x, 7.x # 2001-10-24 Peter Bieringer <pb@bieringer.de> # enhancements for new style drivers and controls, tested on a RHL 7.1.93 system # # description: NUT upsd and its drivers directly monitor a ups and \ # make information from it available to other programs # processname: upsd # config: /etc/ups/upsd.conf # config: /etc/ups/ups.conf PATH=/sbin:/bin:/usr/sbin:/usr/bin export PATH # Source function library. . /etc/rc.d/init.d/functions # POWERDOWNFLAG *must* match that in upsmon.conf DRIVERPATH=/sbin POWERDOWNFLAG=/etc/killpower UPSDCONF=/etc/ups/upsd.conf UPSCONF=/etc/ups/ups.conf # if there is no config file, bail out [ -f $UPSDCONF ] || exit 0 runcmd() { gprintf "%s " "$1" shift if [ "$BOOTUP" = "color" ]; then $* && echo_success || echo_failure else $* fi echo } # See how we are called. case "$1" in start) NUM=0 if [ -f $UPSCONF ]; then # new style drivers uses 'upsdrvctl' gprintf "NUT starting UPS model drivers: " daemon upsdrvctl start echo if [ $? -eq 0 ]; then NUM=$[ $NUM + 1 ] fi else if [ -f $UPSDCONF ]; then # old style drivers for cmd_line in "`grep '^UPS' $UPSDCONF| cut -d' ' -f4-`" do if [ -n "$cmd_line" ]; then DAEMON=`echo $cmd_line | cut -d' ' -f1` ARGUMENTS=`echo $cmd_line | cut -d' ' -f2-` gprintf "NUT Starting model driver %s: " "$DAEMON" daemon $DRIVERPATH/$DAEMON $ARGUMENTS echo NUM=$[ $NUM + 1 ] fi done fi fi if [ $NUM -eq 0 ]; then failure "NUT No UPS drivers were configured" echo else gprintf "NUT Starting UPS daemon: " daemon upsd echo touch /var/lock/subsys/upsd fi ;; stop) gprintf "NUT Stopping UPS daemon: " killproc upsd echo if [ -f $UPSCONF ]; then # new style drivers uses 'upsdrvctl' action "NUT Stopping UPS model drivers" \ upsdrvctl stop else if [ -f $UPSDCONF ]; then # old style drivers for DAEMON in `awk '/^UPS/ {print $4}' $UPSDCONF` do gprintf "NUT Stopping model driver %s: " "$DAEMON" killproc $DAEMON echo done fi fi rm -f /var/lock/subsys/upsd ;; powerdown) if [ -f $UPSCONF ]; then # new style drivers runcmd "NUT powerdown of attached UPS(es)" upsdrvctl shutdown else if [ -f $UPSDCONF ]; then # old style drivers # what fun - cut is in /usr/bin, we'll use awk & sed # we also use runcmd - action needs things not available at this stage # the first part of the sed replaces tabs with spaces # - mind the raw tab in this script if [ -f $POWERDOWNFLAG ]; then for cmd_line in "`grep '^UPS' $UPSDCONF |\ sed -e 'y/ / /;s/^UPS *[^ ][^ ]* *[^ ][^ ]* *//'`" do if [ -n "$cmd_line" ]; then DAEMON=`echo $cmd_line | awk '{print $1}'` # Arguments are munged to insert -k before device name (last arg) ARGUMENTS=`echo $cmd_line | sed -e 's/^[^ ][^ ]* *//;s/\( *[^ ][^ ]* *\)$/ -k \1/'` runcmd "NUT Powerdown model driver $DAEMON" $DRIVERPATH/$DAEMON -d0 $ARGUMENTS fi done fi fi fi ;; restart) $0 stop $0 start ;; status) if [ -f $UPSCONF ]; then # new style drivers action "NUT: checking UPS model drivers" upsdrvctl status else if [ -f $UPSDCONF ]; then # old style drivers # ugh - this is painful - maybe it should all be made one function to do this parsing for DAEMON in `awk '/^UPS/ {print $4}' $UPSDCONF`; do status $DAEMON done fi fi status upsd ;; *) gprintf "Usage: upsd {start|stop|powerdown|restart|status}\n" exit 1 esac