Synology + UPS as NUT Primary

Last updated on May 18, 2024

I recently purchased a CyberPower CST135XLU Uninterruptible Power Supply (UPS) for my linux server, Orbi Wifi Router, and Synology NAS. However, I only need my Orbi running on UPS in the event of a power outage; the others don’t need to be running. With my Fios on a dedicated UPS and Orbi on this new UPS, I will be able to stay on line while I wait for the power to be restored. This means I need to setup Network UPS Tools (NUT) so that I can gracefully shutdown my Synology NAS and linux server – giving my Orbi extended uptime on UPS battery mode.

Options

I can either attach the UPS to my linux server with NUT as primary then setup Synology NAS as secondary. Or, reverse it with Synology NAS as primary with linux as secondary. There are many other factors but, ultimately, primary has to stay up longer than secondary to avoid unexpected behavior caused by loss of communication to primary. Additionally, given that my linux server consumes more raw power than my Synology, it was an obvious decision to make Synology my primary.

Synology UPS Setup

To setup local UPS support, connect the UPS device to a USB port of the Synology NAS. Login to your DSM and navigate to “Control Panel” then click “Hardware & Power”. Under the UPS tab, “Enable UPS support” and select “USB UPS”. In my case, I want to gracefully shutdown after 5 minutes – not have it running “Until low battery”

Tick “Enable network UPS server” to allow NUT client to connect to this service as secondary. Click “Permitted DiskStation Devices” and enter the client IP address – in my case, this would be my linux server’s fixed IP address.

Click “OK” then click “Apply”. You now have NUT service running on Synology as primary.

Extract Required NUT client details

To access required data to properly configure NUT client, you must login to DSM via terminal over SSH. From DSM, go to “Control Panel” and click “Terminal & SNMP”. Under “Terminal” tab, tick “Enable SSH service”.

SSH into DSM as user with administrative role. Once logged in, run sudo tail /etc/ups/upsd.users command. Look for the line password = <password>, note this password down. Prior to the password line, note down the username defined within square brackets ([monuser]). Then, run sudo tail /etc/ups/ups.conf. The name within square braces is the UPS name. See green outline in screenshot

Linux Server Setup as NUT client

Pre-requisite

First install Network UPS Tools – if not already installed

$ sudo apt update
$ sudo apt install nut

Setup NUT Monitor

First, configure NUT as netclient by editing /var/nut/nut.conf. Look for MODE and change it to netclient

MODE=netclient

Then, edit /etc/nut/upsmon.conf to add MONITOR line with the name capture from ups.conf along with the monitor user (e.g. monuser) and password from upsd.user from above Synology section.

MONITOR <name from ups.conf on synology>@<ip address of your synology> 1 <monitor user from above> <password from above> slave

Then setup NOTIFYFLAG. If NOTIFYFLAG are commented out, please add the follow. Otherwise, edit them according.

NOTIFYFLAG ONLINE     SYSLOG+EXEC
NOTIFYFLAG ONBATT     SYSLOG+EXEC
NOTIFYFLAG LOWBATT    SYSLOG+EXEC
NOTIFYFLAG FSD        SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK     SYSLOG+EXEC
NOTIFYFLAG COMMBAD    SYSLOG+EXEC
NOTIFYFLAG SHUTDOWN   SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT   SYSLOG+WALL+EXEC
NOTIFYFLAG NOCOMM     SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT   SYSLOG+WALL+EXEC

The SYSLOG will trigger logging. WALL will send message to all terminal sessions. The EXEC command will trigger script defined by NOTIFYCMD. This will be further discussed in next section.

NOTIFYCMD /usr/sbin/upssched

This will enable NUT to monitor your UPS status on Synology and notify and/or execute action

Setup NUT Schedule

When UPS notification occurs, NOTIFYFLAG is used to perform actions for a given notification type. For example, when UPS moves to battery mode, ONBATT will be triggered. In the above case, It will perform SYSLOG which will log to /var/log/syslog and EXEC will trigger the script defined by NOTIFYCMD. When power is restored to UPS, ONLINE will be trigger.

Edit /etc/nut/upssched.conf uncomment the following line. This sets the file name of the First In First Out (FIFO) between processes to start and stop timer.

PIPEFN /run/nut/upssched/upssched.pipe

Then uncomment the following line. This will prevent race condition.

LOCKFN /run/nut/upssched/upssched.lock

Add the following commands

AT ONLINE * EXECUTE online
AT ONBATT * EXECUTE onbatt

Search for CMDSCRIPT and edit the file referenced (e.g. /bin/upssched-cmd). Replace with following:

#! /bin/sh
#
# This script should be called by upssched via the CMDSCRIPT directive.
#
# Here is a quick example to show how to handle a bunch of possible
# timer names with the help of the case structure.
#
# This script may be replaced with another program without harm.
#
# The first argument passed to your CMDSCRIPT is the name of the timer
# from your AT lines.

UPS="<name from ups.conf on synology>@<ip address of your synology>"

STATUS=$( upsc $UPS ups.status )
CHARGE=$( upsc $UPS battery.charge )
CHMSG="[$STATUS]:$CHARGE%"

case $1 in
        online)
                MSG="$UPS, $CHMSG - power supply has been restored."
                /sbin/shutdown -c
                ;;
        onbatt)
                MSG="$UPS, $CHMSG - power failure - save your work!"
                /sbin/shutdown -h +2
                ;;
        lowbatt)
                MSG="$UPS, $CHMSG - shutdown now!"
                /sbin/shutdown -h now
                ;;
        upsgone) MSG="The UPS $UPS has been gone for awhile" ;;
        *) MSG="$UPS, $CHMSG - Unrecognized command: $1" ;;
esac
logger -i -t upssched-cmd $MSG

In above example, when NOTIFYFLAG ONBATT occurs, it will execute /usr/sbin/upssched defined by NOTIFYCMD. This will trigger AT ONLINE * EXECUTE online which will call /bin/upssched-cmd defined by CMDSCRIPT. In /bin/upssched-cmd onbatt case will result execute shutdown in 2 minutes (see /sbin/shutdown -h +2). However, when power is restored, the online trigger will cancel the shutdown (see /sbin/shutdown -c).

Restart NUT Monitor

Reload NUT monitor with the latest changes by executing following commands

$ sudo service nut-monitor stop
$ sudo service nut-monitor start

To test, unplug your battery from power source to trigger ONBATT notification then plug it back in. If everything is setup correctly, you should see in the /var/log/syslog similar to below.