Controlled Shutdown of your NAS(es) in case of defined Level of Battery in UPS

Currently reading
Controlled Shutdown of your NAS(es) in case of defined Level of Battery in UPS

thx @Shadow
1. does your NAS also automatically boot up again when the power is back online after it has been shutdown by this script?

This will be done in the mentioned next round. Need a time for prepare, tests, finalize another small script, based on upsmon.conf

2. How did you leave your UPS settings in the Synology interface? Still like this?

1600859373282.png


a) when you will read my research above (it doesn't mater when you use USB/LAN connected UPS), there is still same Synology DRV and MIB for it.

b) you can check your UPS name by
Code:
upsc -l
output:
upsc ups@localhost
# For rest of my NASes there is IP adress of my Syno UPS Server (my current setup)
# My plan is, that primary Syno UPS Server will be disabled. Not needed anymore. And all NAS will be connected directly just like NUT client to UPS. This setup is suitable only for LAN UPS connection. For the USB connection is Syno UPS Server OK. My advice for this setup is to change:
Proposed part of code in my solution:
Bash:
#Shutdown action def ("P" for PowerOff; "now" for an immediate action)
SHTD='/usr/sbin/shutdown -P now'
Customized part of code in my solution:
Bash:
#Shutdown action def ("P" for PowerOff; "+60" for delayed action)
SHTD='/usr/sbin/shutdown -P +60'
to get +60 seconds for other NASes connected to this Syno UPS Server (shutdown signal handling).

# you can check setup of listening here:
/usr/syno/etc/ups/upsd.conf
 
I have tried the code and the .sh, but I can't get the NAS to shut down when the APC reaches 20% load. Any advice?
 
first make a screenshot from:

then:

then:

Good evening, sorry for the delay. Here I attach the screenshots:

1 (1).jpg

My code for the NAS to turn off when the UPS reaches 90% battery:
#!/bin/sh
#
#Shutdown action def ("P" for PowerOff; "now" for an immediate action)
SHTD='/usr/sbin/shutdown -P now'
#
# main check of UPS status by "upsc" command and "localhost or IP address
STATUS=`/usr/bin/upsc ups@localhost ups.status`
if [ $STATUS = 'OB' ];
then
echo "UPS on Battery"
else
echo "UPS On Line"
fi
# # OB = On Battery; OL = On Line
#
# second check of battery level in %
BATT=`/usr/bin/upsc ups@localhost battery.charge`
echo "$BATT % of battery level"
#
#
# change the BATT level value to proper format (integer) = BATINT
float=`/usr/bin/upsc ups@localhost battery.charge`
BATINT=${float%.*}
#
# For the proper Shutdown of the NAS we have to test 2 scenarios:
# First: UPS in on Battery from the "$STATUS" variable
# Second: UPS battery level is Equal or Lower than variable (in my case = 20%)
if [ $BATINT -le 90 ] && [ $STATUS = 'OB' ];
then
$SHTD
echo "shutdown"
else
echo "no need Shutdown"
fi
But the NAS never shuts down.

Thanks you for your help.
 
Last edited:
check your copy paste for all the steps:
- create upstest.sh
- create task

EDIT:
check the command, whne you are on Battery
upsc ups@localhost ups.status

TEST:
then arrange a test directly from CLI, e.g. with 99% of UPS (change the value in the code), you will get this message:
Bash:
XXX@YOURNASNAME:/usr/syno/bin# upstest.sh
UPS on Battery
98.00 % of battery level


Broadcast message from XXX@YOURNASNAME
        (/dev/pts/14) at 11:55 ...


The system is going down for power off NOW!
shutdown

when everything is correctly done, follow my guide.
-- post merged: --

btw, don't forget to rewrite the test value (99%) back to previous value!
 
Last edited:
I think there could be an error in the script above.
On my NAS (DS718+, DSM 6.2.3-25426 Update 3), the ups.status value when on battery is:

ups.status: OB DISCHRG

...whereas the @jeyare script tests for just 'OB'.

If this is true of other NAS's, the script needs to be amended to something like:

Bash:
#!/bin/sh
#
#Shutdown action def ("P" for PowerOff; "now" for an immediate action)
SHTD='/usr/sbin/shutdown -P now'
#
# main check of UPS status by "upsc" command and "localhost or IP address
STATUS=`/usr/bin/upsc ups@localhost ups.status`
if [[ $STATUS == *"OB"* ]];
    then
        echo "UPS on Battery"
    else
        echo "UPS On Line"
fi
# # OB = On Battery; OL = On Line
#
# second check of battery level in %
BATT=`/usr/bin/upsc ups@localhost battery.charge`
echo "$BATT % of battery level"
#
#
# change the BATT level value to proper format (integer) = BATINT
float=`/usr/bin/upsc ups@localhost battery.charge`
BATINT=${float%.*}
#
# For the proper Shutdown of the NAS we have to test 2 scenarios:
# First: UPS in on Battery from the "$STATUS" variable
# Second: UPS battery level is Equal or Lower than variable (in my case = 20%)
if [ $BATINT -le 20 ] && [[ $STATUS == *"OB"* ]];
    then
        $SHTD
        echo "shutdown"
    else
        echo "no need Shutdown"
fi
 
RE: “Synology DSM does not provide SNMP trap capability” (Synology Support; 29 AUG 2020)

@jeyare - Through your investigations, have you been able to determine whether the DSM UPS service operates (a) by receiving SNMP traps or (b) by conducting SNMP polling?
 
Last edited:
for every OS based client's setup:
1. DMS- Firewall
enable port 3493 for your LAN, or defined IPs

2. CLI
/usr/syno/etc/ups# vi upsd.users
Code:
[username]
        password = psw
        upsmon = slave
you will use the username & password later

for Win OS based clients setup:

1. download
2. edit the installed target:
C:\Program Files (x86)\NUT\etc
2.A. file> nut.conf
Code:
MODE=netclient

2.B. file> upsd.conf
Code:
LISTEN YOURNASIP 3493

2.C. file> upsmon.conf
Code:
MONITOR ups@YOURNASIP 1 username psw slave
SHUTDOWNCMD "C:\\WINDOWS\\system32\\shutdown.exe /s /t 120"
Note: you need use user/psw based on setup in the> /usr/syno/etc/ups# upsd.users
the 120 value means time of the shutdown countdown 120 seconds from the ups event ... change as you wish

3. Copy file libgcc_s_dw2-1.dll from the installed target:
C:\Program Files (x86)\NUT\bin
to
C:\Program Files (x86)\NUT\sbin

then you need download SSL libraries from
last one up to your sys, this one for sure:
unzip and both DLLs from the zip here:
C:\Program Files (x86)\NUT\sbin

Final stage:
launch Windows services by WIN+R
find: Network UPS Tools
set: Automated start and Run
done

Check the setup:
Code:
upsc ups@YOURNASIP:3493

you will get same info like from NAS
Code:
upsc ups@localhost

done

Very last stage is setup of controlled shutdown:
- same attitude as for the Syno NAS
Code:
upsc ups@YOURNASIP:3493 battery.charge .... level of %
upsc ups@YOURNASIP:3493 ups.status .... ups is OB/OL
then just a batch file is necessary
 
Last edited:
I don't have more time, then here is batch script for Win users (NUT PC client side).
Save it to a file called "xxx.bat", here:
C:\Program Files (x86)\NUT\bin\

Bash:
:: main check of UPS status by "upsc" command and "localhost or IP address
@echo off
CD C:\Program Files (x86)\NUT\bin\
FOR /F %%S IN ('upsc.exe [email protected] ups.status') DO SET STATUS = %%S
:: ECHO %STATUS%


:: Test of the STATUS
IF %STATUS% == OB (ECHO UPS is on Battery) ELSE (ECHO UPS is On Line)
:: Results note: OB = On Battery; OL = On Line


:: second check of battery level in %
FOR /F %%L IN ('upsc.exe [email protected] battery.charge') DO SET /A BLEVEL = %%L
ECHO %BLEVEL% perc. of battery in your UPS remains


:: For the proper Shutdown of the NAS we have to test 2 scenarios:
:: First: UPS in on Battery from the "$STATUS" variable
:: Second: UPS battery level is Equal or Lower than variable (in my case = 20% = variable for GEQ test.
IF %BLEVEL% GEQ 20 (ECHO no need Shutdown) ELSE cmd /c "shutdown.exe /s /t 120"
:: you can change the 120 seconds variable as you wish

Then you need create Task in Windows Task Scheduller.
here is a code for import of new task:

XML:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-03-22T17:49:28.1450965</Date>
    <Author>YOURCOMPUTERNAME\USER</Author>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <Repetition>
        <Interval>PT1M</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <Enabled>true</Enabled>
      <UserId>YOURCOMPUTERNAME\USER</UserId>
      <Delay>PT0M</Delay>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>YOURCOMPUTERNAME\USER</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Program Files (x86)\NUT\bin\upstest.bat"</Command>
    </Exec>
  </Actions>
</Task>

You need edit/change:
YOURCOMPUTERNAME\USER .... everywhere in the script by your real defined IDs
Then just import this Task into the Task Scheduler.
Done
-- post merged: --

RE: “Synology DSM does not provide SNMP trap capability” (Synology Support; 29 AUG 2020)

@jeyare - Through your investigations, have you been able to determine whether the DSM UPS service operates (a) by receiving SNMP traps or (b) by conducting SNMP polling?

discussed last year with 2nd level of tech support from Taiwan.
They probably lost someone who was responsible for the NUT integration. They have a link to this forum. But no way fto find this solution in new DSM7. Because it's a peanut to integrate such setup into GUI.

Seems to be it's about pooling right now, because I don't have a time to investigate how to run the traps. From one point of view, it's standardized. But from my 10+Y experiences with SYNO, ...
I have got Pro grade UPSes from Legrand include special network card for SNMP management. No time for long tests. Works perfect for me now, because controlled shutdown of the NAS environment is for me more important, than pointless setup from Syno GUI.
 
I think there could be an error in the script above.
On my NAS (DS718+, DSM 6.2.3-25426 Update 3), the ups.status value when on battery is:

ups.status: OB DISCHRG

...whereas the @jeyare script tests for just 'OB'.

If this is true of other NAS's, the script needs to be amended to something like:

Bash:
#!/bin/sh
#
#Shutdown action def ("P" for PowerOff; "now" for an immediate action)
SHTD='/usr/sbin/shutdown -P now'
#
# main check of UPS status by "upsc" command and "localhost or IP address
STATUS=`/usr/bin/upsc ups@localhost ups.status`
if [[ $STATUS == *"OB"* ]];
    then
        echo "UPS on Battery"
    else
        echo "UPS On Line"
fi
# # OB = On Battery; OL = On Line
#
# second check of battery level in %
BATT=`/usr/bin/upsc ups@localhost battery.charge`
echo "$BATT % of battery level"
#
#
# change the BATT level value to proper format (integer) = BATINT
float=`/usr/bin/upsc ups@localhost battery.charge`
BATINT=${float%.*}
#
# For the proper Shutdown of the NAS we have to test 2 scenarios:
# First: UPS in on Battery from the "$STATUS" variable
# Second: UPS battery level is Equal or Lower than variable (in my case = 20%)
if [ $BATINT -le 20 ] && [[ $STATUS == *"OB"* ]];
    then
        $SHTD
        echo "shutdown"
    else
        echo "no need Shutdown"
fi

thx for the idea -seems to be, each vendor creates his own way to comply with NUT standards :cool:
 
some update for WIN based NUT client

it's better to run the automated task w/o cmd window, then you need create new VBS script in the same folder as the .bat file, called up to you "XXX.vbs"
Bash:
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "C:\Program Files (x86)\NUT\bin\upstest.bat" & Chr(34), 0
Set WshShell = Nothing
you can edit the path as you wish

then you need just replace Actions part in existing Task scheduler:

1616501608790.png

then write to Program/script field:
Code:
wscript.exe
and to field Add arguments : Path to your saved VBS file in double quotes, e.g.: "C:\Prog.......\XXX.vbs"
1616501675836.png


quite better now
-- post merged: --

for lazy bears, here is XML code to create import to the Task scheduler:

CSS:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-03-22T17:49:28.1450965</Date>
    <Author>YOUCOMPUTERNAME\USER</Author>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <Repetition>
        <Interval>PT1M</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <Enabled>true</Enabled>
      <UserId>YOUCOMPUTERNAME\USER</UserId>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>YOUCOMPUTERNAME\USER</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>wscript.exe</Command>
      <Arguments>"C:\Program Files (x86)\NUT\bin\upstest.vbs"</Arguments>
    </Exec>
  </Actions>
</Task>
 
@jeyare, I do enjoy reading your interesting and insightful contributions to this topic. But, assuming that the UPS manufacturer already provides an application to automatically and safely shutdown a PC/Mac during a power failure, what are the advantages/disadvantages of using a NUT client together with a custom script?
 
@DataNAS, thank you.

re: PC/Mac client side
ofc, there are plenty of client’s UPS apps and there is the problem. Each vendor has diff GUI, name convention, ... and diff options for a setup.

My approach can help you keep one central and managed service for the diff UPS enviro. Yeap, there is also right place for a question - how frequently is UPC changed? Another question is how many UPSes do I have in operation (work, home)? The Identical client Monitor/shutdown setup for all OS platforms is the added value for me.
Second added value is a dashboard for a data enthusiast (my primary job). When you have the standardized data (based on this Data taxonomy) and when you have Grafana, .... then you can get all important info from single site.

The client side support is for me in secondary level. More important, as was written is the NASes farm and controlled shutdown in any case.

Finaly, the NUT is really standardized solution, also for datacenter operation.
 
when you need more knowledge about NUT possibilities and better control of your NASes operation (not only) in case of power supply outage incidents (not only), you can read this great doc:
still under active maintenance, last ver. from 8/2021

whether no one in the DSM7 product team understood how important it is to have a managed shutdown under control on the basis of defined rules and the type of UPS used operating conditions, ... because get drives to safe mode is really not enough for someone who knows how is important to shut down every electronics (components) properly.
-- post merged: --

Well, probaly good news that this 'missing feature' has been brought to attention to the devvers. I would wonder why nobody within Synology tought about this themselves... I would expect Synology to only run their own server gear, and as a server admin I would not feel comfortable that I can't set a NAS or any other server to shutdown at xx% battery level.

(Surprisingly enough they have their own stuff running in AWS......)

year after:
nobody cares
 
Last edited:
Hi, I have opened a ticket with Synolgoy on the UPS support and that both APC and Cyberpower USB connected UPS's under DMS 7 will give an incorrect status after a manual UPS test. If I pull the power cord from the wall, let it run for 2 minutes, and then restore line power the status returned is:

ups.status: FSD OL

I cannot find any material on why the forced shut down flag is still on ups.status unless there is a bug USB driver.

device.mfr: CPS
device.model: BRG1500AVRLCD
device.serial: hidden
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 5
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.version: DSM7-0-0-NewModel-repack-41859-210506
driver.version.data: CyberPower HID 0.4
driver.version.internal: 0.41

Lets say the Synolgoy seems to work fine, stays up and running, notifies it's back on mains, but if you have slaves like I do when they see FSD they start an immediate shutdown. And to make things worse the slaves when you reboot them as soon as NUT starts and sees the FSD status they shutdown again. I had to take the SD cards out of the Pi's and disable nut for them to boot.

If I restart the NUT services on the Synolgy the ups.status is just fine:

ups.status: OL

I am to the point I can't trust sharing the UPS off the Synology. I am thinking about mirroring the UPS configuration on one of the PI's and connecting the UPS to it and making the Synology a slave but this is just another unknown on how it would work.

I just found this was posted in another thread also. DSM 7.0 - Another DSM7 regression: UPS
 
I cannot find any material on why the forced shut down flag is still on ups.status unless there is a bug USB driver.
FSD OL is flag defined by upsmon

and making the Synology a slave but this is just another unknown on how it would work
this is my solution, described here. Syno is just a slave, my UPS network controller is the master (the advantage of SNMP driver)
 
Yes I would assume a SNMP UPS would solve this but I don't have the $'s to purchase another UPS and the costs of getting one that has a network card for SNMP for CyberPower UPS that adds abut $260 dollars:

RMCARD205 - Hardware - Product Details, Specs, Downloads | CyberPower

I found the table of supported statu flags, still strange that the driver would set FSD within a few seconds of the mains gong offline and then when mains are restored the FSD would still be there. I am going to wait to see what I hear back from Synolgoy on my ticket.

1632057528288.png
 
Syno is using generic drivers (USB or SNMP) from NUT developers. In your case the: usbhid-ups

Back to my previous post:
for the FSD is responsible the upsmon - When upsmon is forced to bring down the local system, it sets the "FSD" (forced shutdown) flag on any UPSes that it is running in master mode. This is used to synchronize slaves in the event that a master UPS that is otherwise OK needs to be brought down due to some pressing event on the master.
Then Force Shut Down is the command sent from the upsmon to the UPS (based on UPS situation).

Steps to find a reason:
1. Try to disconnect USB cable from the UPS (connected to the NAS). And check the status again. Write status here.
Then connect the USB cable back.
2. Try to restart the UPS manually. After the restart, check the status again. Write status here.
3. Try to reload upsmon config by
Bash:
upsmon -c reload
After the restart, check the status again. Write status here.
3. Try to restart your NAS. After the restart, check the status again. Write status here.

Second:
- do you have right setup in upsmon.conf and upsd.users? (last attached documentation will help you)
- in upsmon - there are plenty of setup steps, responsible for such status one of them is DEADTIME - If the UPS was on battery and the deadtime runs out [no update] the monitor will trigger immediately the FSD flag (Force ShutDown) and start the shutdown sequence
- also ups.conf delays and sync

You can attach here all of the docs for a check.
 
I have a Cyberpower UPS connected to my NAS, which i want to tell my computer to power down 30 mins after a power cut.

btw you can install NUT client at your PC and use the NAS as NUT server, when you have just USB connection from UPS to NAS:
- IP address of NAS is known
- device name is: ups
- user is: monuser
- password is: secret

But I can't even work out how to install NUT and get to the GUI.
"Windows (complete port, Beta): Windows MSI installer 2.6.5-6"
Just seems to dump a load of files in Program Files (X86).
 
Last edited:
I found this thread on WinNUT's Github page and it relates to configuring a windows client for Synolgoy's implementation, you should use the package from the latest stable on this site.


I will state that the dials are a bit screwy on windows after calibration:

1638625139133.png
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Similar threads

  • Question
Thanks Rusty. Appreciate the help and your knowledge. :)
Replies
7
Views
1,188

Welcome to SynoForum.com!

SynoForum.com is an unofficial Synology forum for NAS owners and enthusiasts.

Registration is free, easy and fast!

Trending threads

Back
Top