packages/utils/nut/files/upsmon.init
juhosg 787b5c61cb nut: add initscripts for upsd and upsmon, create config from UCI
There's more to be done here, but this is a good start and covers most
things that people will need to configure.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
Signed-off-by: Gabor Juhos <juhosg@openwrt.org>

git-svn-id: svn://svn.openwrt.org/openwrt/packages@34828 3c298f89-4303-0410-b956-a3cf2f4a3e73
2012-12-21 18:25:32 +00:00

145 lines
4.3 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
# Copyright © 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
START=51
DEFAULT=/etc/default/nut
RUN_D=/var/run
PID_F=$RUN_D/upsmon.pid
UPSMON_C=$RUN_D/upsmon.conf
nut_upsmon_conf() {
local cfg="$1"
echo "# Config file automatically generated from UCI config" > $UPSMON_C
config_get val "$cfg" runas
[ -n "$val" ] && echo "RUN_AS_USER $val" >> $PSMON_C
config_get val "$cfg" minsupplies 1
echo "MINSUPPLIES $val" >> $UPSMON_C
config_get val "$cfg" shutdowncmd "/sbin/halt"
echo "SHUTDOWNCMD \"$val\"" >> $UPSMON_C
config_get val "$cfg" notifycmd
[ -n "$val" ] && echo "NOTIFYCMD \"$val\"" >> $UPSMON_C
config_get val "$cfg" pollfreq 5
echo "POLLFREQ $val" >> $UPSMON_C
config_get val "$cfg" pollfreqalert 5
echo "POLLFREQALERT $val" >> $UPSMON_C
config_get val "$cfg" hostsync 15
echo "HOSTSYNC $val" >> $UPSMON_C
config_get val "$cfg" deadtime 15
echo "DEADTIME $val" >> $UPSMON_C
config_get val "$cfg" powerdownflag /var/run/killpower
echo "POWERDOWNFLAG $val" >> $UPSMON_C
config_get val "$cfg" onlinemsg
[ -n "$val" ] && echo "NOTIFYMSG ONLINE \"$val\"" >> $UPSMON_C
config_get val "$cfg" onbattmsg
[ -n "$val" ] && echo "NOTIFYMSG ONBATT \"$val\"" >> $UPSMON_C
config_get val "$cfg" lowbattmsg
[ -n "$val" ] && echo "NOTIFYMSG LOWBATT \"$val\"" >> $UPSMON_C
config_get val "$cfg" fsdmsg
[ -n "$val" ] && echo "NOTIFYMSG FSD \"$val\"" >> $UPSMON_C
config_get val "$cfg" commokmsg
[ -n "$val" ] && echo "NOTIFYMSG COMMOK \"$val\"" >> $UPSMON_C
config_get val "$cfg" commbadmsg
[ -n "$val" ] && echo "NOTIFYMSG COMMBAD \"$val\"" >> $UPSMON_C
config_get val "$cfg" shutdownmsg
[ -n "$val" ] && echo "NOTIFYMSG SHUTDOWN \"$val\"" >> $UPSMON_C
config_get val "$cfg" replbattmsg
[ -n "$val" ] && echo "NOTIFYMSG REPLBATT \"$val\"" >> $UPSMON_C
config_get val "$cfg" nocommmsg
[ -n "$val" ] && echo "NOTIFYMSG NOCOMM \"$val\"" >> $UPSMON_C
config_get val "$cfg" noparentmsg
[ -n "$val" ] && echo "NOTIFYMSG NOPARENT \"$val\"" >> $UPSMON_C
local default
config_get default "$cfg" defaultnotify SYSLOG
config_get val "$cfg" onlinenotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG ONLINE $val" >> $UPSMON_C
config_get val "$cfg" onbattnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG ONBATT $val" >> $UPSMON_C
config_get val "$cfg" lowbattnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG LOWBATT $val" >> $UPSMON_C
config_get val "$cfg" fsdnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG FSD $val" >> $UPSMON_C
config_get val "$cfg" commoknotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG COMMOK $val" >> $UPSMON_C
config_get val "$cfg" commbadnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG COMMBAD $val" >> $UPSMON_C
config_get val "$cfg" shutdownnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG SHUTDOWN $val" >> $UPSMON_C
config_get val "$cfg" replbattnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG REPLBATT $val" >> $UPSMON_C
config_get val "$cfg" nocommnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG NOCOMM $val" >> $UPSMON_C
config_get val "$cfg" noparentnotify "$default"
[ -n "$val" ] && echo "NOTIFYFLAG NOPARENT $val" >> $UPSMON_C
config_get val "$cfg" rbwarntime 43200
echo "RBWARNTIME $val" >> $UPSMON_C
config_get val "$cfg" nocommwarntime 300
echo "NOCOMMWARNTIME $val" >> $UPSMON_C
config_get val "$cfg" finaldelay 5
echo "FINALDELAY $val" >> $UPSMON_C
}
nut_upsmon_add() {
local cfg="$1"
local type="$2"
local upsname
local hostname
local port
local powervalue
local user
local pass
local system
# If no core config, use defaults
[ -r $UPSMON_C ] || nut_upsmon_conf ""
config_get upsname "$cfg" upsname
config_get hostname "$cfg" hostname localhost
config_get port "$cfg" port
config_get powervalue "$cfg" powervalue 1
config_get user "$cfg" user
config_get pass "$cfg" password
system="$upsname@$hostname"
if [ -n "$port" ]; then
system="$system:$port";
fi
echo "MONITOR $system $powervalue $user $pass $type" >> $UPSMON_C
}
start() {
[ -d $RUN_D ] || mkdir -p $RUN_D
rm -f $UPSMON_C
config_load upsmon
config_foreach nut_upsmon_conf upsmon
config_foreach nut_upsmon_add master master
config_foreach nut_upsmon_add slave slave
[ -f $DEFAULT ] && . $DEFAULT
$DEBUG /usr/sbin/upsmon $UPSMON_OPTIONS
}
stop() {
[ -r $PID_F ] && /usr/sbin/upsmon -c stop
rm -f $UPSMON_C
}