nut: add initscripts for upsd and upsmon, create config from UCI
authorjuhosg <juhosg@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Fri, 21 Dec 2012 18:25:32 +0000 (18:25 +0000)
committerjuhosg <juhosg@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Fri, 21 Dec 2012 18:25:32 +0000 (18:25 +0000)
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

utils/nut/Makefile
utils/nut/files/ups.init [new file with mode: 0755]
utils/nut/files/upsmon.init [new file with mode: 0755]

index 69043fe..9d9ffd1 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=nut
 PKG_VERSION:=2.6.5
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
 PKG_SOURCE_URL:=http://www.networkupstools.org/source/2.6/
@@ -72,12 +72,11 @@ define Package/nut/install
        $(INSTALL_DIR) $(1)/etc/nut
        $(CP) $(PKG_INSTALL_DIR)/usr/lib/libupsclient.so* $(1)/usr/lib/
        $(INSTALL_CONF) $(PKG_INSTALL_DIR)/etc/nut/nut.conf.sample $(1)/etc/nut/nut.conf
-       $(INSTALL_CONF) $(PKG_INSTALL_DIR)/etc/nut/ups.conf.sample $(1)/etc/nut/ups.conf
+       ln -sf /var/run/ups.conf $(1)/etc/nut/ups.conf
 endef
 
 define Package/nut/conffiles
 /etc/nut/nut.conf
-/etc/nut/ups.conf
 endef
 
 define Package/nut-client/install
@@ -92,17 +91,17 @@ define Package/nut-server/install
        $(INSTALL_DIR) $(1)/usr/sbin
        $(INSTALL_DIR) $(1)/lib/nut
        $(INSTALL_DIR) $(1)/usr/share/nut
-
+       $(INSTALL_DIR) $(1)/etc/init.d
+       $(INSTALL_BIN) ./files/ups.init $(1)/etc/init.d/ups
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upsd $(1)/usr/sbin
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/lib/nut/upsdrvctl $(1)/lib/nut
        $(INSTALL_CONF) $(PKG_INSTALL_DIR)/etc/nut/upsd.conf.sample $(1)/etc/nut/upsd.conf
-       $(INSTALL_CONF) $(PKG_INSTALL_DIR)/etc/nut/upsd.users.sample $(1)/etc/nut/upsd.users
+       ln -sf /var/run/upsd.users $(1)/etc/nut/upsd.users
        $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/nut/cmdvartab $(1)/usr/share/nut/cmdvartab
        $(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/nut/driver.list $(1)/usr/share/nut/driver.list
 endef
 
 define Package/nut-server/conffiles
-/etc/nut/upsd.users
 /etc/nut/upsd.conf
 endef
 
@@ -114,14 +113,15 @@ endef
 define Package/nut-monitor/install
        $(INSTALL_DIR) $(1)/usr/sbin
        $(INSTALL_DIR) $(1)/etc/nut
+       $(INSTALL_DIR) $(1)/etc/init.d
+       $(INSTALL_BIN) ./files/upsmon.init $(1)/etc/init.d/upsmon
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upsmon $(1)/usr/sbin
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upssched $(1)/usr/sbin
-       $(INSTALL_CONF) $(PKG_INSTALL_DIR)/etc/nut/upsmon.conf.sample $(1)/etc/nut/upsmon.conf
+       ln -sf /var/run/upsmon.conf $(1)/etc/nut/upsmon.conf
        $(INSTALL_CONF) $(PKG_INSTALL_DIR)/etc/nut/upssched.conf.sample $(1)/etc/nut/upssched.conf
 endef
 
 define Package/nut-monitor/conffiles
-/etc/nut/upsmon.conf
 /etc/nut/upssched.conf
 endef
 
diff --git a/utils/nut/files/ups.init b/utils/nut/files/ups.init
new file mode 100755 (executable)
index 0000000..90410a3
--- /dev/null
@@ -0,0 +1,87 @@
+#!/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=50
+
+DEFAULT=/etc/default/nut
+RUN_D=/var/run
+PID_F=$RUN_D/upsd.pid
+UPS_C=$RUN_D/ups.conf
+USERS_C=$RUN_D/upsd.users
+
+nut_user_add() {
+       local cfg="$1"
+       local a
+       local val
+
+       config_get val "$cfg" username "$1"
+       echo "[$val]" >> $USERS_C
+
+       config_get val "$cfg" password
+       echo "  password = $val" >> $USERS_C
+
+       config_get val "$cfg" actions
+       for a in $val; do
+           echo "  actions = $a" >> $USERS_C
+       done
+
+       config_get val "$cfg" instcmds
+       for a in $val; do
+           echo "  instcmds = $a" >> $USERS_C
+       done
+
+       config_get val "$cfg" upsmon
+       if [ -n "$val" ]; then
+           echo "  upsmon $val" >> $USERS_C
+       fi
+}
+
+start() {
+        local in_driver have_drivers
+       config_cb() {
+           if [ "$1" != "driver" ]; then
+               in_driver=
+           else
+               echo "[$2]" >> $UPS_C
+               in_driver=true
+               have_drivers=true
+           fi
+       }
+       option_cb() {
+           if [ "$in_driver" = "true" ]; then
+               echo " $1 = $2" >> $UPS_C
+           fi
+       }
+
+       [ -d $RUN_D ] || mkdir -p $RUN_D
+       echo "# Config file automatically generated from UCI config" > $UPS_C
+       echo "# Config file automatically generated from UCI config" > $USERS_C
+
+       config_load ups
+
+       config_foreach nut_user_add user
+
+       [ -f $DEFAULT ] && . $DEFAULT
+       if [ "$have_drivers" = "true" ]; then
+           $DEBUG /usr/sbin/upsd $OPTIONS
+           $DEBUG /lib/nut/upsdrvctl start
+       fi
+}
+
+nut_driver_stop() {
+       local cfg="$1"
+       local driver
+
+       config_get driver "$cfg" driver
+
+       [ -r /var/run/$driver-$cfg ] && /lib/nut/upsdrvctl stop $cfg
+}
+
+stop() {
+       [ -r $PID_F ] && /usr/sbin/upsd -c stop
+       config_load ups
+       config_foreach nut_driver_stop driver
+}
diff --git a/utils/nut/files/upsmon.init b/utils/nut/files/upsmon.init
new file mode 100755 (executable)
index 0000000..8d6c09e
--- /dev/null
@@ -0,0 +1,144 @@
+#!/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
+}