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 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s77>k44ofy`glX=O&z`&C3 z=qM&os5wD3yVMS<}o%1`Aw%Ohx|2xw`S=;l0$(qvp zzJFY0zt!-N;NPUu|7#w^{*DgG@>96@x>Q3^F^+AoLc<-Gpb5*p%R^_UE_S`8t$cFp z?qiEY9Op56y>du9`t!ZqdiIBTbB@@*arX4|+`e%891USc#y5`TR$;$g-)S3i+)baQ z^mffEh5Pr4R@uM$(^qI0&#?ag^a&Gmgrwx0!R++)n@`&E_b9!&BmL}vqiAkH z{!y*>fd}`UT(oHSiUkWAT(qVx`taccD^uAVn=Rp&<~>{UFXq|gGLDazzfKGb6TNz5 zN^m=0`WxxzAN3R`zOXuV?wli&qgs-IL`#E(bxMTq+qH7J{@17c>`slkf8F@%A3xj3 z`*#ci*;Af&r0=+6@}oql0 z%9P;02M->sEXY@OP-BQ-Uh|)SHiMOeOyZ*|-3>ww0UG-mpGB-#^(^KiHvG$#0hO`>iJ<29KMUT9W51TH_Eus(#FA~x8;U_MO%h<_hnfnsUw~ON*sqCy44#9 z&5}`hvshY)SMZ_$FCXvszYiXNi{+HBX#c+FeC4^~bJZ41leR>7UV0>GlEuM$=g`#y zmF>qX#p*eyWrj?$7ExU^SyJ8F*24C6L$ep12Z&^d=*Yz(NC-MyoE`6(eGjjQiE5%-WU0NhR|AYDsTb_8s zY3U#9v;|uK-uv1AL``q2y0b{#!gE#J(h?^(SFHbafg_#&ykSSTMd$pAR-N;Yrk9?o aXY5}aeqs9kPk9Us3=E#GelF{r5}E)V7qR&O 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 zcmeAS@N?(olHy`uVBq!ia0y~yU@!n-4mJh`hH$2z?F$D9W9UX6C!~{1cb+d3*zV`EU=GeyBnfZC4YNpp^>n)zMPK7neI|&MMF7!xF zcNW&!Vz_pO)=O_813#@vExsIKE=^0m^|dH$5)tH7I$irKG49=+yL;d5y~q2q@osI| zvokZFTRyk^|JgqM$p1pl#U%p2^_FlZNZjDIn{s69YS+1{@p60hx>hRMa_1atxWQ<} zIY)05Pfg~NT^h#@AJAf8IUrHty7k5yK`^4o1*`6{w zH|AZ4e4y~bIKkleMUU#auFCf%7;7&-i+&K@lGT>j$#^*QgX_Y}2Q(jlZ|PGl{P45s z?KSDyI&q>=v*UN^cQ#+|{@rcFlsKiY>3dS%)ID?V%qmKYITPe*x*)B%|A=dXR?j8z z#$->KnAbOUSg3x{+c0g7vfkGF@7gPDWu5NuzhTRoo4xDPDziH*eT`)d%dLAoBuwR4 z-?E=ekn>V&Ha@!dpV~%=d9yBEqlhkvz1Fp)*yM(yn|JiYnE$2ue=)G=zZY!f%?_vzh*Hz{dY9cv~tQd*WR&c zjqtI<9^Vu0@x6X|`G9pt_5ruUVhf!gG<(Kmhgp8v$@gQ)+t0$+4w|+|`^B>8VA?kLvoE(cqI)C@db1W|D=y@UX zLzHow<__^2W$RpRlRs!3HcP0z&vd9Nnc;`s2L2npH(4eeImmO3`+(=lt8ZEJVn6dH z&OCeIoPpQ#r?;jy7I*vgMO7Bf%fSm+kszeyu+r!PBrm`A4dR zDOl*9h2zUh#eOK&yb^<8QCI`*cGRmdtt}m=GDxbVx(?(7N0IL=EnA8SLZQlk(X` z==v?8)hc}J7;bOmXGm?jHoY~fp!4DG18-wF8Q!^6d97G_L*a+?hSzgXChQW4_74|# zo_)m8*#F$WM%&F1Vy8a(ZmZw1puxuF9n-ZCakbOwj>4WYCQ+r*uLTSX@^3a+@V{}G z6{W*Hb(%|gdtj7WZ81y8iTg2o^ZsmHBfn*`|8?oN#yN~}Jd3|H8hEi!w#ZN`EMWWX z_(0it^HsmCXQdB(3QBmrhp9;VPTblF+YNfRsCS#B-WB|y_vT}c;Mc6%)(6YQODCPY z#rk0RjPH%ll5TSDSR5gKi{(ZS!+qwk=}(Ny*jd}V<_1Ujw>P|9pgZZ4PYAYGf3K*Td8-f4Q$W#UghZxXGz zlXm0WqdmO264w~28+LcKPqMIR7xmnepCNW{-MTxm$(vi=Oj(q=(7=oRWR-n+^M+cV zXBmO%Hd$+!%b2uPm7kux{ii~CgMQDYFav?$1Hp%a7d^cYXPDGhGk0ZFhRg~%sn`iy z>^qye6ZbM+ib}BGuq$ge>j#Nv1rnxeJ$+ABe{D~^^26-Is!a`sEVj}wjeaO?@qcvt z)0fsi?=>H|+*^CPVL9(?t6R&2#a8FOomgqpBgM?%88iFXpCdmS&Q6WfRVfxLSt&B1 zRV82NS<=7n>!(O(Ufdg9EU9zrcan3Y^T0!>x7T@H;dZK zPu*}&!m)~ZhpM<0GmEE8jhNd@i4Tn5jCO54D|1I{U)t-AC$XL}+U1dnS1KenGiSm z>&RBgh2I;OH_9Gpt<<@~vOjOPjrh`xVjHh;&CfL(9e=KUo38p+**md2@9e|gs0_c% zru!^v4(Z+Vco&y!{g~{;pEymrjm3T9cGbeQZ%dqhGM~=b&vg1!=%^M#mKQKQKKgnW8%mnVmC2QY){TcXoD||FJd>N7@qle zd~b+1R!`17+H`Kpt(RM$TxOKMU~)4qIa-K4_jvnt?Z}vUYhpLY+E(vm{lQk&y(jv} zn`y@4$L{L*Fh%g$WZjT=d-*Akb&8_*CHAFfU3qOWN9&1?pS$JcYrwO^K0rWcBB+Brk#c@nmEeXEdx7Pwv&@3$mZbz`(%Z>FVdQ&MBb@0H{{>dH?_b literal 0 HcmV?d00001 diff --git a/source/nut/usr/local/emhttp/plugins/nut/include/nut_status.php b/source/nut/usr/local/emhttp/plugins/nut/include/nut_status.php new file mode 100755 index 00000000..c18eb7c1 --- /dev/null +++ b/source/nut/usr/local/emhttp/plugins/nut/include/nut_status.php @@ -0,0 +1,6 @@ + +".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