Skip to content

Instantly share code, notes, and snippets.

@KidA001
Last active February 23, 2024 14:10
Show Gist options
  • Save KidA001/a49bcb96fce2c29be208aea67e544e7d to your computer and use it in GitHub Desktop.
Save KidA001/a49bcb96fce2c29be208aea67e544e7d to your computer and use it in GitHub Desktop.

UPS Shutdown Scripts

This documents everything I needed to get my Proxmox instance to listen to my UPS Server and initiate a shutdown script that would shutdown my UDM Pro. This is needed as the UDM Pro does not have the ability to listen to a NUT server on it's own (what the hell ubiquiti).

I'm using a Synology NAS as a NUT server, but you can point your NUT client to any supported UPS. It's set to alert any clients subscribed to it when the UPS connected to it is in low battery. This all applies regardless of what machine you have a NUT server on, it does not have to be Synology.

Additionally these install steps were on my Proxmox machine, but these steps will probably work on any Debian GNU/Linux OS with little to no modification. YMMV

High level summary of the steps:

  • Install the NUT client on Proxmox
  • Enable SSH on the UDM Pro
  • Creating a no-passphrase SSH Key on Proxmox
  • Add the SSH Public Key to Authorized Keys on UDM Pro
  • Create a shutdown script that will SSH into UDM and initiate shutdown
  • Call shutdown script with NUT Server broadcasts low battery

Install NUT client on Proxmox Server

With small tweaks I believe most of these steps would work on most linux systems

apt-get install nut
  • Edit /etc/nut/nut.conf:
MODE=netclient
  • Edit /etc/nut/upsmon.conf: MONITOR <system> <powervalue> <username> <password> ("master"|"slave"):
MONITOR ups@192.168.30.60 1 monuser secret slave
  • Start monitoring:
upsmon start
  • To check UPS status: upsc <name> for direct attached units or upsc <name>@<address> for remote
upsc ups@192.168.30.60

Allow IPs to access NUT Server

This applies to my Synology NAS, I'm not sure if other NUT Servers will require this.

In synology go to Power>UPS Settings, add the IPs of all devices that will be accessing the NUT server. In my case I need to add the IP of my Proxmox instance. Ensure that you're using a static IP.

Enable SSH on UDM Pro

Go to your UniFi OS page select Console Settings. Enable SSH and set a password

Create SSH Key on Proxmox

I used the ssh-keygen -t rsa command to generate a new key. I stored it in the .ssh dir but gave it a name specific to the UDM id_rsa_udmp

Adding SSH Public Key to UDM Pro

This was a bit more involved...

Because keys get wiped after firmware updates (and restarts?), we need to ensure the Pubic key is added to the list of authorized keys on boot. I installed the UDMPro Boot Script Utility. Then added a script which ensures the public key lives in authorized_keys.

Once that utility is installed you can create the following file which will ensure the key lives in /root/.ssh/authorized_keys.

/data/on_boot.d/07-pub-key.sh

#!/bin/sh

echo 'ssh-rsa AAAAB...your-ssh-public-key root@proxmox' >> /root/.ssh/authorized_keys

Make sure the script is executeable with chmod +x 07-pub-key.sh

Create Proxmox Shutdown Script

On proxmox, create a shutdown script

/etc/nut/shutdown.sh

#!/bin/sh

echo 'Staring Shutdown Scirpt, initiated by NUT client'
ssh -i /root/.ssh/id_rsa_udmp root@192.168.50.1 'poweroff' & sleep 2 && /sbin/shutdown -h +0

Make sure the script is executeable with chmod +x shutdown.sh

Now update SHUTDOWNCMD in /etc/nut/upsmon.conf

SHUTDOWNCMD "/bin/bash /etc/nut/shutdown.sh >> /var/log/ups/ups.log"

On Promox you can check the file /var/log/ups/ups.log to validate the shutdown script is being executed in a shutdown situation. That's it. Make sure you test and ensure this works on your system. Please feel free to add comments or suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment