From 0540297384612812c15ffbba4ea35839b3b459a8 Mon Sep 17 00:00:00 2001 From: dmacias72 Date: Fri, 28 Apr 2017 20:14:39 -0600 Subject: [PATCH] fork unRAID NUT plugin --- source/nut/etc/rc.d/rc.nut | 69 +++++++ source/nut/etc/ups/nut.conf | 1 + source/nut/etc/ups/ups.conf | 3 + source/nut/etc/ups/upsd.conf | 1 + source/nut/etc/ups/upsd.users | 11 ++ source/nut/etc/ups/upsmon.conf | 6 + source/nut/etc/ups/upssched.conf | 1 + .../local/emhttp/plugins/nut/NUTdetails.page | 36 ++++ .../local/emhttp/plugins/nut/NUTsettings.page | 185 ++++++++++++++++++ .../usr/local/emhttp/plugins/nut/README.md | 3 + .../usr/local/emhttp/plugins/nut/default.cfg | 10 + .../emhttp/plugins/nut/event/disks_mounted | 11 ++ .../emhttp/plugins/nut/icons/nutdetails.png | Bin 0 -> 952 bytes .../emhttp/plugins/nut/icons/nutsettings.png | Bin 0 -> 421 bytes .../local/emhttp/plugins/nut/images/nut.png | Bin 0 -> 2162 bytes .../emhttp/plugins/nut/include/nut_status.php | 6 + .../emhttp/plugins/nut/include/update.nut.php | 32 +++ .../nut/scripts/notifycmd_batterylevel | 30 +++ .../plugins/nut/scripts/notifycmd_seconds | 30 +++ .../plugins/nut/scripts/notifycmd_timeout | 30 +++ .../plugins/nut/scripts/nut_kill_inverter | 14 ++ .../plugins/nut/scripts/nut_restart_udev | 11 ++ .../local/emhttp/plugins/nut/scripts/rc.nut | 69 +++++++ .../local/emhttp/plugins/nut/scripts/start | 2 + .../usr/local/emhttp/plugins/nut/scripts/stop | 2 + .../emhttp/plugins/nut/scripts/txt/kill.txt | 4 + .../emhttp/plugins/nut/scripts/txt/udev.txt | 4 + .../plugins/nut/scripts/write_config.sh | 99 ++++++++++ 28 files changed, 670 insertions(+) create mode 100755 source/nut/etc/rc.d/rc.nut create mode 100755 source/nut/etc/ups/nut.conf create mode 100755 source/nut/etc/ups/ups.conf create mode 100755 source/nut/etc/ups/upsd.conf create mode 100755 source/nut/etc/ups/upsd.users create mode 100755 source/nut/etc/ups/upsmon.conf create mode 100755 source/nut/etc/ups/upssched.conf create mode 100644 source/nut/usr/local/emhttp/plugins/nut/NUTdetails.page create mode 100755 source/nut/usr/local/emhttp/plugins/nut/NUTsettings.page create mode 100755 source/nut/usr/local/emhttp/plugins/nut/README.md create mode 100755 source/nut/usr/local/emhttp/plugins/nut/default.cfg create mode 100755 source/nut/usr/local/emhttp/plugins/nut/event/disks_mounted create mode 100644 source/nut/usr/local/emhttp/plugins/nut/icons/nutdetails.png create mode 100755 source/nut/usr/local/emhttp/plugins/nut/icons/nutsettings.png create mode 100755 source/nut/usr/local/emhttp/plugins/nut/images/nut.png create mode 100755 source/nut/usr/local/emhttp/plugins/nut/include/nut_status.php create mode 100755 source/nut/usr/local/emhttp/plugins/nut/include/update.nut.php create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_batterylevel create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_seconds create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_timeout create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/nut_restart_udev create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/rc.nut create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/start create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/stop create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/txt/kill.txt create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/txt/udev.txt create mode 100755 source/nut/usr/local/emhttp/plugins/nut/scripts/write_config.sh diff --git a/source/nut/etc/rc.d/rc.nut b/source/nut/etc/rc.d/rc.nut new file mode 100755 index 00000000..92323dc5 --- /dev/null +++ b/source/nut/etc/rc.d/rc.nut @@ -0,0 +1,69 @@ +#!/bin/sh +# Slackware startup script for Network UPS Tools +# Copyright 2010 V'yacheslav Stetskevych +# Edited for unRAID by macester macecapri@gmail.com + +DPATH=/usr/bin +FILE=/boot/config/plugins/nut/nut.cfg +export PATH=$DPATH:$PATH + + + +start_driver() { + /usr/sbin/upsdrvctl -u root start || exit 1 +} + +start_upsd() { + /usr/sbin/upsd -u root || exit 1 +} + +start_upsmon() { + /usr/sbin/upsmon -u root || exit 1 +} + +stop() { + echo "Stopping the UPS services... " + if pgrep upsd 2>&1 >/dev/null; then + /usr/sbin/upsd -c stop; fi + if pgrep upsmon 2>&1 >/dev/null; then + /usr/sbin/upsmon -c stop; fi + /usr/sbin/upsdrvctl stop + sleep 2 + if [ -f /var/run/upsmon.pid ]; then + rm /var/run/upsmon.pid; fi +} + +write_config() { + /usr/local/emhttp/plugins/nut/scripts/write_config.sh +} + +case "$1" in + start) # starts everything (for a ups server box) + sleep 1 + write_config + sleep 3 + start_driver + start_upsd + start_upsmon + ;; + start_upsmon) # starts upsmon only (for a ups client box) + start_upsmon + ;; + stop) # stops all UPS-related daemons + stop + ;; + shutdown) # shuts down the UPS + echo "Killing inverter..." + /usr/sbin/upsdrvctl shutdown + ;; + reload) + write_config + /usr/sbin/upsd -c reload + /usr/sbin/upsmon -c reload + ;; + write_config) + write_config + ;; + *) + echo "Usage: $0 {start|start_upsmon|stop|shutdown|reload|restart|write_config}" +esac diff --git a/source/nut/etc/ups/nut.conf b/source/nut/etc/ups/nut.conf new file mode 100755 index 00000000..6529f5ed --- /dev/null +++ b/source/nut/etc/ups/nut.conf @@ -0,0 +1 @@ +MODE=standalone diff --git a/source/nut/etc/ups/ups.conf b/source/nut/etc/ups/ups.conf new file mode 100755 index 00000000..c2d29b0e --- /dev/null +++ b/source/nut/etc/ups/ups.conf @@ -0,0 +1,3 @@ +[ups] +driver=none +port=auto diff --git a/source/nut/etc/ups/upsd.conf b/source/nut/etc/ups/upsd.conf new file mode 100755 index 00000000..a32659e5 --- /dev/null +++ b/source/nut/etc/ups/upsd.conf @@ -0,0 +1 @@ +LISTEN 0.0.0.0 diff --git a/source/nut/etc/ups/upsd.users b/source/nut/etc/ups/upsd.users new file mode 100755 index 00000000..80c04e82 --- /dev/null +++ b/source/nut/etc/ups/upsd.users @@ -0,0 +1,11 @@ +[admin] +password=adminpass +actions=set +actions=fsd +instcmds=all +[monuser] +password=monpass +upsmon master +[slaveuser] +password=slavepass +upsmon slave diff --git a/source/nut/etc/ups/upsmon.conf b/source/nut/etc/ups/upsmon.conf new file mode 100755 index 00000000..8991b938 --- /dev/null +++ b/source/nut/etc/ups/upsmon.conf @@ -0,0 +1,6 @@ +MONITOR ups@localhost 1 monuser monpass master +SHUTDOWNCMD "/usr/local/sbin/powerdown" +POWERDOWNFLAG /etc/ups/flag/killpower +NOTIFYFLAG ONBATT SYSLOG+EXEC +NOTIFYFLAG ONLINE SYSLOG+EXEC +NOTIFYCMD "/etc/ups/notifycmd" diff --git a/source/nut/etc/ups/upssched.conf b/source/nut/etc/ups/upssched.conf new file mode 100755 index 00000000..8b137891 --- /dev/null +++ b/source/nut/etc/ups/upssched.conf @@ -0,0 +1 @@ + diff --git a/source/nut/usr/local/emhttp/plugins/nut/NUTdetails.page b/source/nut/usr/local/emhttp/plugins/nut/NUTdetails.page new file mode 100644 index 00000000..2867e4f3 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/NUTdetails.page @@ -0,0 +1,36 @@ +Menu="NUTsettings" +Title="NUT Details" +--- + + + + + + +
KeyValueKeyValue
Please wait, retrieving UPS information...
diff --git a/source/nut/usr/local/emhttp/plugins/nut/NUTsettings.page b/source/nut/usr/local/emhttp/plugins/nut/NUTsettings.page new file mode 100755 index 00000000..198b2a6f --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/NUTsettings.page @@ -0,0 +1,185 @@ +Menu="OtherSettings" +Type="xmenu" +Title="NUT Settings" +Icon="nut.png" +--- + + + + + + +
+ + + Online Manual + +Start NUT UPS service: +: + +> Set to 'Yes' to enable NUT and start the service, set to 'No' to disable NUT and stop the service. +> Setting 'Yes' will also enable NUT on unRAID boot. + +UPS Driver: +: + +> **Check the link below to find the driver matching your UPS** +> +> + http://www.networkupstools.org/stable-hcl.html +> +> For other devices set **Custom** and enter the driver manually below. +> Dont forget to match the port with your driver + + +Custom Driver: +: + +> Use the *Link* above to find which Driver to choose. +> Dont forget to match the port with your driver + + +UPS Port: +: + +> Enter the *port* which your UPS connects to. +> +> + **auto** - Set to "auto" if the ups is connected by usb. +> + **/dev/tty0** - Example, for seriall devices** + + +UPS Mode: +: + +> Select the *mode* which mode your UPS will run in. +> +> + **standalone** - When the ups is only used by the local host** +> + **netserver** - When nut clients/slaves need to connect to the ups** + + +UPS Shutdown Mode: +: + +> Select the *Shutdown mode* for NUT, this mode determines which "timer" to use for when the UPS will shutdown the system. +> +> + **Battery Level** - Shuts down the system when there is X percentage left on the battery ** +> + **Runtime Left** - Shuts down the system when there is X runtime left on the battery (in seconds)** +> + **Time on Battery** - Shuts down the system when the user defined timer reaches zero (in seconds)** + + +Battery level to initiate shutdown (%): +: + +> If during a power failure, the remaining battery percentage (as reported by the UPS) is below or equal to *Battery level*, NUT will initiate a system shutdown. + + +Runtime left to initiate shutdown (seconds): +: + +> If during a power failure, the remaining runtime seconds (as calculated internally by the UPS) is below or equal to *seconds*, NUT will initiate a system shutdown. +> **Note** depeding on your ups this value can "fluctuate" so be aware. if yot ups have an old battery dont use this + +Time on battery before shutdown (seconds): +: + +> The time in seconds before the system will power down if a power failure accours. + + +Turn off UPS after shutdown: +: + +> Set to *Yes* to turn off the power to the UPS after a shutdown. +> +> Check your bios and enable, "Automatically power on after power loss" so that the server starts again when powers is restored. + + +


+ +> **Additional Notes** +> +> The usernames and passwords for *admin* and *slave* accounts in *upsd.users* file ** +> +> + **Admin Account**: "admin / adminpass" - **Slave Account**: "slaveuser / slavepass" +> +> NUT is set to listen on all interface in upsd.conf "LISTEN 0.0.0.0" +> +> macester (macecapri@gmail.com) + + + +: +
+
NUT Status
+ + + + + + + + + + +
UPS Statistics
diff --git a/source/nut/usr/local/emhttp/plugins/nut/README.md b/source/nut/usr/local/emhttp/plugins/nut/README.md new file mode 100755 index 00000000..9e82ca6f --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/README.md @@ -0,0 +1,3 @@ +####Network UPS Tools (NUT)#### + +A Plugin that supports a wide variety of UPS brands and models. diff --git a/source/nut/usr/local/emhttp/plugins/nut/default.cfg b/source/nut/usr/local/emhttp/plugins/nut/default.cfg new file mode 100755 index 00000000..ca4afa9b --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/default.cfg @@ -0,0 +1,10 @@ +SERVICE="disable" +DRIVER="custom" +SERIAL="none" +PORT="auto" +MODE="standalone" +SHUTDOWN="sec_timer" +BATTERYLEVEL="20" +SECONDS="240" +TIMEOUT="240" +UPSKILL="disable" diff --git a/source/nut/usr/local/emhttp/plugins/nut/event/disks_mounted b/source/nut/usr/local/emhttp/plugins/nut/event/disks_mounted new file mode 100755 index 00000000..78e573e0 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/event/disks_mounted @@ -0,0 +1,11 @@ +#!/bin/bash +cfg=/boot/config/plugins/nut/nut.cfg + +# Daemon already running or no custom file? +[[ -f /var/run/upsmon.pid || ! -f $cfg ]] && exit + +# Read settings +source $cfg + +# Start daemon +[[ $SERVICE == enable ]] && /usr/local/emhttp/plugins/nut/scripts/rc.nut start diff --git a/source/nut/usr/local/emhttp/plugins/nut/icons/nutdetails.png b/source/nut/usr/local/emhttp/plugins/nut/icons/nutdetails.png new file mode 100644 index 0000000000000000000000000000000000000000..55b2a5997b35464daec2ba910d5c5fcde7529ec8 GIT binary patch literal 952 zcmV;p14sOcP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D02p*dSaefwW^{L9 za%BK;VQFr3E^cLXAT%y8E;VI^GGzb&11U*FK~y+Tb(3pMQeha!>FQQ9q?>(cja;*A zt&v(3R_I(-TCSO7E?T8DvLa+jYHQ?7Gc;0a({#~RqG)Swc}Xm5i6oea2?|MY;IO!8 z+VWf8_V%39G;Qm@{XU%M?EgFGeb0F*^Rg6J2W*%}AV6dhFn>-!%ob#$LQzv=He>iV z_b9H>7|cv2iwpB5KtO(8eqnrke3s;vM4FhGD1_NEqC|wYe}7DBVPQ$B)#Ccmtv4Za zza1G3R`gK*_@!8!dn+(7=y+~+?(^8#LuKMDu_`X^NHO$+|I{gzP>rU?GC1f!IPOO_ z8;<&nR;1UNkXGA=y1M!&7>o#&!-4&DV$Lx)?jCnJU8B%r1LQK8j}+y;m#-S8J3e;K zG`2QPmp`qXDk?3WB#@PtP0~X)>r5sK`f9`A0l*N3pmzdTObB9Gq7aL)itDdK?U&rxG38@~7Psz0?c!V&#qb^om3 zUHe;9YEB^0>vbq0AzlW(Ab{LNQjn)hcU7BZqea>)BgwV9#-bEKo&!qiKxW1H?;fuQ z;clG7KkP$ENlCY%x11;w0RikmcRE)4MeZvx2;FCzBJHfIAm83{sz2)dj&VL;0I&a- zkdQ4B=*|5|fA*l8XRou#Jr9f`?A#mXz(EylaBsyb?@__N$)cjWsGy*LL@Je{@bK^j z0(P7{v{%xe=B)l)=96{^@zeE@R#p|&#FSHq4rlBe=kYEfk?1#+gL5PyRY5e}3CThR6^6`WUYUtef#4l{Wc>q^q1AD{3i*gXtiF_vfXek%}# z{oeVH8yLy6c(3}<2xkw^F^G#eiJy3dEYISXb;^GMkF8hGm*4Vk P00000NkvXXu0mjf9T&0r literal 0 HcmV?d00001 diff --git a/source/nut/usr/local/emhttp/plugins/nut/images/nut.png b/source/nut/usr/local/emhttp/plugins/nut/images/nut.png new file mode 100755 index 0000000000000000000000000000000000000000..d048eba3ddfe8a476fe015696ebd0bf9315eddaf GIT binary patch literal 2162 zcmV-&2#xoNP)|7QRv0{q^33=;INka&>35jX*p(SZb(>t_Dn#xv!A3_lk z386@1XG0S#v@xxiD(OoSFit9xgiQ!mM1rLDj)Wkx6cGs`%YFJ`Uhdr8z3#o<4e5d1 zeRk%|nddp@IsfxNXT<+;38Qon`!1vjU>Mj7K9t0@t3{n9Umm?Kim4$z3v9-K*a12T zoGz*id}-vmD8|FUDgXq)7Pb>Ghi_L(MWrpMI+xB0e#cvTi{!SYKCEn0mD`BMWNHcDdYEqjwNyM`GW1%8=EX% z68+?sM`T+C8QHK1m5kHke_DvZ(>fn!M=TO)4)53 zYrsas7NJAngGpR#Ryp*!4)~<)^K)%e#n-jae0V)#FL*0v6qpc913Qzrc4an${sdU( zgpTE-(1(R8SOQpw^gQAZz!3JfBo5BcN1q?+InU|ClJ z!YCcWuG{_|XydP=CJ z*dX{DuIcJ%V$9oNgOm< zbsOstF>r6$fH)8AK$==C3ze2ccZX40CVg`RRLI|4y>9%mtRJ+KPuCmmGi(7~45Rdc zFiHoLIA|tua0dH9;3Gq`)lRj|8^H2XVC%dBavR)Ut&q1cjkG6=G-cfp@Gk7}Y!UTp z+dIK`7j=@!+6CaJnD>F^V%Z6}qgWT(1lWuK-vd^c-jfbF0ylJZPc z0P8V(!>F;b(&~p2+p*3pT10|T@G_W{PD|VjJkSzmKxeSFCzHXIyyu6339^m$61&8eNdIKZ%12 zVZ8y;T3|o0x@wyR@EGQB7&Ru0j^wNLhhfzCHt?#lfH4F;8|gCmBD7E9+w$~<{O>8? zMBc5-fTsCm0m3*a~5=|6p)1^Z!G3w{`aqx z8)$FAJS-Soz&3Mn)^(#xkrCNn9&;Sz*+87_$Ru9Sa{coB_U;N4IPQjD|6T zg@u4~n9uT%`3zLa_h_MldiI_{#8#s?j2h$PQKKMx$l}Lj=b+72{pzy-Tn3&m$d>{n z4_FLTI_%?QG61;Ka8Gsm+=zv$HUI_$w}N7!$HFLGn#8pkq4$BOfgQkwc`Vcfzizuc z7o})(JW5w7^L(*E`K|3|CG8_iVT*3f;f-2oPHBSQ1SUXdi=GXmbhYtgLJwh<8-@f& zk+&srt?hI|`2)*rzX95;Y&-P=(ksAWU}Yr&mpTf&Er`|X$>N2xvEU=%H^5(#IJjJp z3!`+c?e+Ol{jvFuR*FeHi1ZfOS^(xxi1&bBGbdwh#e&Y1+Ud3A(*YaMG}&HbTM`Ft z$A{M|SzMm1U9(+1d$|Sp26l_QTg2>^GZ)6)EKCAe4m@huA4ci&ZUvMfOVS6W%|#77 zgerYCPZY|ZyFTv1LM65vg6EOS-CoxMFvnFrs6~}i;@MG1Wu#dccVi&{&Iz_59q;Dz zQ&CEbB$L5;%)4!OD+|LHYaymEk02fCg?yU;0OQweH!JCMd1rAJvbmTn=XwHhqF3@T zdqaaxAU*{~J5IO7vrrR!9N3Y>weozaF9CU;-TF>dS-hys@Lm_PdAi~<@DgZu5(iyR z*o6gTrweN`t_xcTdX7R(;A6q>fXRG+&{rutCr@+#P4Q*CW9e%l`y=*Vq*K@ZwAw?d zjDS~wR?NCT<1G0axDg8hNPLF*2(d4TgEtq0bO!3rJF+IQ0 +".shell_exec("/usr/bin/upsc ups").""; + echo $sStatus; +?> diff --git a/source/nut/usr/local/emhttp/plugins/nut/include/update.nut.php b/source/nut/usr/local/emhttp/plugins/nut/include/update.nut.php new file mode 100755 index 00000000..ffac280f --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/include/update.nut.php @@ -0,0 +1,32 @@ + + diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_batterylevel b/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_batterylevel new file mode 100755 index 00000000..ed9db48c --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_batterylevel @@ -0,0 +1,30 @@ +#!/bin/bash +# +# NUT NOTIFYCMD script macester macecapri@gmail.com +PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin + +BATTERYLEVEL=50 + +trap "exit 0" SIGTERM +if [ "$NOTIFYTYPE" = "ONLINE" ] +then + echo $0: power restored | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is no longer on battery" -i "normal" + # Cause all instances of this script to exit. + killall -s SIGTERM `basename $0` +fi +if [ "$NOTIFYTYPE" = "ONBATT" ] +then + echo $0: System shuts down when battery reaches $BATTERYLEVEL % | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is on battery" -i "alert" + # Loop with one second interval to allow SIGTERM reception. + timer=$( /usr/bin/upsc ups|grep battery.charge:|awk '{print $2}' ) + while [ $timer -gt $BATTERYLEVEL ] + do + sleep 1 + timer=$( /usr/bin/upsc ups|grep battery.charge:|awk '{print $2}' ) + done + echo $0: commencing shutdown | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is shutting down unRAID" -i "alert" + upsmon -c fsd +fi diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_seconds b/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_seconds new file mode 100755 index 00000000..0df4e4d8 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_seconds @@ -0,0 +1,30 @@ +#!/bin/bash +# +# NUT NOTIFYCMD script macester macecapri@gmail.com +PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin + +SECONDS=200 + +trap "exit 0" SIGTERM +if [ "$NOTIFYTYPE" = "ONLINE" ] +then + echo $0: power restored | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is no longer on battery" -i "normal" + # Cause all instances of this script to exit. + killall -s SIGTERM `basename $0` +fi +if [ "$NOTIFYTYPE" = "ONBATT" ] +then + echo $0: System shuts down when there is $SECONDS seconds left on the battery... | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is on battery" -i "alert" + # Loop with one second interval to allow SIGTERM reception. + timer=$( /usr/bin/upsc ups|grep battery.runtime:|awk '{print $2}' ) + while [ $timer -gt $SECONDS ] + do + sleep 1 + timer=$( /usr/bin/upsc ups|grep battery.runtime:|awk '{print $2}' ) + done + echo $0: commencing shutdown | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is shutting down unRAID" -i "alert" + upsmon -c fsd +fi diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_timeout b/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_timeout new file mode 100755 index 00000000..da3dc093 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/notifycmd_timeout @@ -0,0 +1,30 @@ +#!/bin/bash +# +# NUT NOTIFYCMD script macester macecapri@gmail.com +PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin + +TIMEOUT=200 + +trap "exit 0" SIGTERM +if [ "$NOTIFYTYPE" = "ONLINE" ] +then + echo $0: power restored | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is no longer on battery" -i "normal" + # Cause all instances of this script to exit. + killall -s SIGTERM `basename $0` +fi +if [ "$NOTIFYTYPE" = "ONBATT" ] +then + echo $0: $TIMEOUT seconds till system powers down... | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is on battery" -i "alert" + # Loop with one second interval to allow SIGTERM reception. + let "n = $TIMEOUT" + while [ $n -ne 0 ] + do + sleep 1 + let "n--" + done + echo $0: commencing shutdown | wall + /usr/local/emhttp/plugins/dynamix/scripts/notify -e "UPS Notification" -s "UPS is shutting down unRAID" -i "alert" + upsmon -c fsd +fi diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter b/source/nut/usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter new file mode 100755 index 00000000..405acccd --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter @@ -0,0 +1,14 @@ +#!/bin/sh +# This file is part of Network UPS Tools build for Slackware Linux. +# It should be sourced in /etc/rc.d/rc.0 as part of the poweroff sequence. +# Here, we signal the UPS to shut off the power. +# Reference: http://www.mail-archive.com/nut-upsdev@lists.alioth.debian.org/msg01099.html +# To be sourced after the genpowerd block and before the actual halt. +if [ -f /etc/ups/flag/killpower ]; then + echo "----------------------------------------Shutting down UPS power!" + /usr/sbin/upsdrvctl shutdown + sleep 40 + echo "The UPS was not shut off properly, or the power came back in the" + echo "middle of the shutdown process, or USB communication failed." + sleep 10 +fi diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/nut_restart_udev b/source/nut/usr/local/emhttp/plugins/nut/scripts/nut_restart_udev new file mode 100755 index 00000000..36c9bffa --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/nut_restart_udev @@ -0,0 +1,11 @@ +#!/bin/sh +# This file is part of Network UPS Tools build for Slackware Linux. +# It should be sourced in /etc/rc.d/rc.0 as part of the poweroff sequence. +# Here, we restart udev so that the system is able to kill off the UPS inverter. +# Reference: http://www.mail-archive.com/nut-upsdev@lists.alioth.debian.org/msg01099.html +# To be sourced after the rootfs has been remounted read-only. +if [ -f /etc/ups/flag/killpower ]; then +echo "Restarting udev to be able to shut the UPS inverter off..." +/etc/rc.d/rc.udev start +sleep 10 +fi diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/rc.nut b/source/nut/usr/local/emhttp/plugins/nut/scripts/rc.nut new file mode 100755 index 00000000..92323dc5 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/rc.nut @@ -0,0 +1,69 @@ +#!/bin/sh +# Slackware startup script for Network UPS Tools +# Copyright 2010 V'yacheslav Stetskevych +# Edited for unRAID by macester macecapri@gmail.com + +DPATH=/usr/bin +FILE=/boot/config/plugins/nut/nut.cfg +export PATH=$DPATH:$PATH + + + +start_driver() { + /usr/sbin/upsdrvctl -u root start || exit 1 +} + +start_upsd() { + /usr/sbin/upsd -u root || exit 1 +} + +start_upsmon() { + /usr/sbin/upsmon -u root || exit 1 +} + +stop() { + echo "Stopping the UPS services... " + if pgrep upsd 2>&1 >/dev/null; then + /usr/sbin/upsd -c stop; fi + if pgrep upsmon 2>&1 >/dev/null; then + /usr/sbin/upsmon -c stop; fi + /usr/sbin/upsdrvctl stop + sleep 2 + if [ -f /var/run/upsmon.pid ]; then + rm /var/run/upsmon.pid; fi +} + +write_config() { + /usr/local/emhttp/plugins/nut/scripts/write_config.sh +} + +case "$1" in + start) # starts everything (for a ups server box) + sleep 1 + write_config + sleep 3 + start_driver + start_upsd + start_upsmon + ;; + start_upsmon) # starts upsmon only (for a ups client box) + start_upsmon + ;; + stop) # stops all UPS-related daemons + stop + ;; + shutdown) # shuts down the UPS + echo "Killing inverter..." + /usr/sbin/upsdrvctl shutdown + ;; + reload) + write_config + /usr/sbin/upsd -c reload + /usr/sbin/upsmon -c reload + ;; + write_config) + write_config + ;; + *) + echo "Usage: $0 {start|start_upsmon|stop|shutdown|reload|restart|write_config}" +esac diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/start b/source/nut/usr/local/emhttp/plugins/nut/scripts/start new file mode 100755 index 00000000..668b866c --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/start @@ -0,0 +1,2 @@ +#!/bin/sh +/etc/rc.d/rc.nut start diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/stop b/source/nut/usr/local/emhttp/plugins/nut/scripts/stop new file mode 100755 index 00000000..9d14b27d --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/stop @@ -0,0 +1,2 @@ +#!/bin/sh +/etc/rc.d/rc.nut stop diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/txt/kill.txt b/source/nut/usr/local/emhttp/plugins/nut/scripts/txt/kill.txt new file mode 100755 index 00000000..0ecf3025 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/txt/kill.txt @@ -0,0 +1,4 @@ +if [ -f /usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter ]; then + . /usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter +fi + diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/txt/udev.txt b/source/nut/usr/local/emhttp/plugins/nut/scripts/txt/udev.txt new file mode 100755 index 00000000..6251dc19 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/txt/udev.txt @@ -0,0 +1,4 @@ + +if [ -f /usr/local/emhttp/plugins/nut/scripts/nut_restart_udev ]; then + . /usr/local/emhttp/plugins/nut/scripts/nut_restart_udev +fi diff --git a/source/nut/usr/local/emhttp/plugins/nut/scripts/write_config.sh b/source/nut/usr/local/emhttp/plugins/nut/scripts/write_config.sh new file mode 100755 index 00000000..791c658d --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/scripts/write_config.sh @@ -0,0 +1,99 @@ +#!/bin/bash +# +# NUT plugin configuration script for unRAID +# By macester macecapri@gmail.com +# +NUTCFG=/boot/config/plugins/nut/nut.cfg + + +# Killpower flag permissions +chmod 777 /etc/ups/flag + +# Add nut user and group for udev at shutdown +GROUP=$( grep -ic "218" /etc/group ) +USER=$( grep -ic "218" /etc/passwd ) + +if [ $GROUP -ge 1 ]; then + echo "NUT Group already configured" +else + groupadd -g 218 nut +fi + +if [ $USER -ge 1 ]; then + echo "NUT User already configured" +else + useradd -u 218 -g nut -s /bin/false nut +fi + +# Nut config files + +# Add the driver config +driver_custom=$( grep -ic 'DRIVER="custom"' $NUTCFG ) +if [ $driver_custom -eq 1 ]; then + var1=$( grep -i "SERIAL=" $NUTCFG|cut -d \" -f2|sed 's/^/driver = /' ) + sed -i "2 s/.*/$var1/" /etc/ups/ups.conf +else + var2=$( grep -i "DRIVER=" $NUTCFG|cut -d \" -f2|sed 's/^/driver = /' ) + sed -i "2 s/.*/$var2/" /etc/ups/ups.conf +fi + +# add the port +var3=$( grep -i "PORT=" $NUTCFG|cut -d \" -f2|sed 's/^/port = /' ) +sed -i "3 s~.*~$var3~" /etc/ups/ups.conf + +# add mode standalone/netserver +var4=$( grep -i "MODE=" $NUTCFG|cut -d \" -f2|sed 's/^/MODE=/' ) +sed -i "1 s/.*/$var4/" /etc/ups/nut.conf + +# Set which shutdown script NUT should use +mode_bat_level=$( grep -ic 'SHUTDOWN="batt_level"' $NUTCFG ) +mode_bat_timer=$( grep -ic 'SHUTDOWN="batt_timer"' $NUTCFG ) + +if [ $mode_bat_level -eq 1 ]; then + sed -i "6 s,.*,NOTIFYCMD \"/usr/local/emhttp/plugins/nut/scripts/notifycmd_batterylevel\"," /etc/ups/upsmon.conf +else + if [ $mode_bat_timer -eq 1 ]; then + sed -i "6 s,.*,NOTIFYCMD \"/usr/local/emhttp/plugins/nut/scripts/notifycmd_seconds\"," /etc/ups/upsmon.conf + else + sed -i "6 s,.*,NOTIFYCMD \"/usr/local/emhttp/plugins/nut/scripts/notifycmd_timeout\"," /etc/ups/upsmon.conf + fi +fi + +# Edit timers fo shutdown scripts + +# shutdown when battery gets to level +var5=$( grep -i "BATTERYLEVEL=" $NUTCFG|cut -d \" -f2|sed 's/^/BATTERYLEVEL=/' ) +sed -i "6 s/.*/$var5/" /usr/local/emhttp/plugins/nut/scripts/notifycmd_batterylevel + +# shutdown when battery time runs out +var6=$( grep -i "SECONDS=" $NUTCFG|cut -d \" -f2|sed 's/^/SECONDS=/' ) +sed -i "6 s/.*/$var6/" /usr/local/emhttp/plugins/nut/scripts/notifycmd_seconds + +# shutdown on user timer +var7=$( grep -i "TIMEOUT=" $NUTCFG|cut -d \" -f2|sed 's/^/TIMEOUT=/' ) +sed -i "6 s/.*/$var7/" /usr/local/emhttp/plugins/nut/scripts/notifycmd_timeout + +# Set if the ups should be turned off +ups_kill=$( grep -ic 'UPSKILL="enable"' $NUTCFG ) +if [ $ups_kill -eq 1 ]; then + var8='POWERDOWNFLAG /etc/ups/flag/killpower' + sed -i "3 s,.*,$var8," /etc/ups/upsmon.conf +else + var9='POWERDOWNFLAG /etc/ups/flag/no_killpower' + sed -i "3 s,.*,$var9," /etc/ups/upsmon.conf +fi + +# Link shutdown scripts for poweroff in rc.0 and rc.6 +UDEV=$( grep -ic "/usr/local/emhttp/plugins/nut/scripts/nut_restart_udev" /etc/rc.d/rc.6 ) +if [ $UDEV -ge 1 ]; then + echo "UDEV lines already exist in rc.0,6" +else + sed -i '/\/bin\/mount -v -n -o remount,ro \//r /usr/local/emhttp/plugins/nut/scripts/txt/udev.txt' /etc/rc.d/rc.6 +fi + +KILL=$( grep -ic "/usr/local/emhttp/plugins/nut/scripts/nut_kill_inverter" /etc/rc.d/rc.6 ) +if [ $KILL -ge 1 ]; then + echo "KILL_INVERTER lines already exist in rc.0,6" +else + sed -i -e '/# Now halt (poweroff with APM or ACPI enabled kernels) or reboot./r /usr/local/emhttp/plugins/nut/scripts/txt/kill.txt' -e //N /etc/rc.d/rc.6 +fi