net-nut: Add UPS notifications via sendmail
authorDaniel Dickinson <lede@cshore.thecshore.com>
Wed, 11 Jan 2017 23:54:40 +0000 (18:54 -0500)
committerDaniel Dickinson <lede@cshore.thecshore.com>
Wed, 11 Jan 2017 23:54:40 +0000 (18:54 -0500)
Add option to send email notification via sendmail
(or replacement).

Signed-off-by: Daniel Dickinson <lede@cshore.thecshore.com>
net/nut/Makefile
net/nut/files/nut-sendmail-notify [new file with mode: 0755]
net/nut/files/nut-sendmail-notify.default [new file with mode: 0644]

index 25d93c48dd29b2cce5db2b51ac1bd87618756eed..c2c38da984b4e05caf555d421c78e06ec42c0747 100644 (file)
@@ -152,6 +152,24 @@ define Package/nut-upsmon/install
        ln -sf /var/etc/nut/upsmon.conf $(1)/etc/nut/upsmon.conf
 endef
 
+define Package/nut-upsmon-sendmail-notify
+       $(call Package/nut/Default)
+       TITLE+= (upsmon with notifications via sendmail)
+       DEPENDS:=nut +nut-upsmon
+       CONFLICTS:=nut-upssched
+endef
+
+define Package/nut-upsmon-sendmail-notify/description
+$(call Package/nut/description/Default)
+upsmon with default notification via sendmail
+endef
+
+define Package/nut-upsmon-sendmail-notify/install
+       $(INSTALL_DIR) $(1)/usr/bin $(1)/etc/uci-defaults
+       $(INSTALL_BIN) ./files/nut-sendmail-notify $(1)/usr/bin/
+       $(INSTALL_DATA) ./files/nut-sendmail-notify.default $(1)/etc/uci-defaults/nut-sendmail-notify
+endef
+
 define Package/nut-upsc
        $(call Package/nut/Default)
        TITLE+= (upsc command)
@@ -504,6 +522,7 @@ $(eval $(call BuildPackage,nut))
 $(eval $(call BuildPackage,nut-common))
 $(eval $(call BuildPackage,nut-server))
 $(eval $(call BuildPackage,nut-upsmon))
+$(eval $(call BuildPackage,nut-upsmon-sendmail-notify))
 $(eval $(call BuildPackage,nut-upsc))
 $(eval $(call BuildPackage,nut-upscmd))
 $(eval $(call BuildPackage,nut-upslog))
diff --git a/net/nut/files/nut-sendmail-notify b/net/nut/files/nut-sendmail-notify
new file mode 100755 (executable)
index 0000000..749aa9b
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+/usr/sbin/sendmail root <<EOF
+From: root
+To: root
+Subject: UPS $NOTIFYTYPE Notification
+
+$1
+.
+EOF
diff --git a/net/nut/files/nut-sendmail-notify.default b/net/nut/files/nut-sendmail-notify.default
new file mode 100644 (file)
index 0000000..54ea909
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+. /lib/functions.sh
+
+REMOVEDEFAULTNOTIFY=0
+SKIPADDSYSLOG=0
+SKIPADDEXEC=0
+
+upsmon() {
+       local cfg="$1"
+       local val
+
+       config_get val "$cfg" defaultnotify
+       if [ -n "$val" ]; then
+               if echo "$val" |grep -q "IGNORE"; then
+                       REMOVEDEFAULTNOTIFY=1
+               else
+                       SKIPADDSYSLOG=1
+                       if echo "$val" |grep -q "EXEC"; then
+                               SKIPADDEXEC=1
+                       fi
+               fi
+       fi
+}
+
+config_load nut_monitor
+config_foreach upsmon upsmon
+
+uci set nut_monitor.@upsmon[-1]=upsmon
+uci set nut_monitor.@upsmon[-1].notifycmd=/usr/bin/nut-sendmail-notify
+
+if [ "$REMOVEDEFAULTNOTIFY" = "1" ]; then
+       uci delete nut_monitor.@upsmon[-1].defaultnotify || true
+fi
+
+if [ "$SKIPADDEXEC" != "1" ]; then
+       uci add_list nut_monitor.@upsmon[-1].defaultnotify="EXEC"
+fi
+
+if [ "$SKIPADDSYSLOG" != "1" ]; then
+       uci add_list nut_monitor.@upsmon[-1].defaultnotify="SYSLOG"
+fi
+
+uci commit nut_monitor