基于NUT配置UPS电池供电时的停电通知及自动关机
Hey there! Let's get your Orvaldi KC2000 set up with NUT so you get those critical GNOME notifications when the power cuts out, and your system auto-shuts down before the battery dies. Here's a step-by-step guide tailored to your setup:
1. Install NUT Packages
First, grab all the required NUT components from Debian's repos:
sudo apt update && sudo apt install nut nut-client nut-server
2. Confirm UPS Detection
Check if your USB-connected UPS is recognized by the system:
lsusb
You should see an entry for your Orvaldi KC2000 (look for labels like "Orvaldi" or "UPS"). The usbhid-ups driver (built into NUT) works with most USB UPSes, including this model.
3. Configure the UPS Driver
Edit the core UPS configuration file to define your device:
sudo nano /etc/nut/ups.conf
Add this section at the end:
[orvaldi-kc2000] driver = usbhid-ups port = auto desc = "Orvaldi KC2000 2000VA/1400W"
Save and exit (Ctrl+O, Enter, Ctrl+X).
4. Set NUT to Standalone Mode
Since this is a single-machine setup, configure NUT to run in standalone mode:
sudo nano /etc/nut/nut.conf
Update the line to:
MODE=standalone
5. Configure Server Access & Monitoring User
First, allow local connections to the NUT server:
sudo nano /etc/nut/upsd.conf
Ensure these lines are present (uncomment if they're commented out):
LISTEN 127.0.0.1 3493 LISTEN ::1 3493
Next, create a monitoring user for the client to authenticate with:
sudo nano /etc/nut/upsd.users
Add this section (replace your-strong-password with a secure passphrase):
[monuser] password = your-strong-password upsmon master
6. Set Up Client Monitoring & Actions
Configure the upsmon client to watch the UPS and trigger alerts/shutdowns:
sudo nano /etc/nut/upsmon.conf
First, add the monitor entry (use the password you set earlier):
MONITOR orvaldi-kc2000@localhost 1 monuser your-strong-password master
Then, tweak the notification settings:
- Point to our custom GNOME notification script (we'll build this next):
NOTIFYCMD /usr/local/bin/ups-notify.sh - Update flags to trigger actions on key events:
NOTIFYFLAG ONBATT SYSLOG+EXEC NOTIFYFLAG LOWBATT SYSLOG+EXEC+SHUTDOWN NOTIFYFLAG ONLINE SYSLOG+EXEC - Set shutdown behavior and polling frequency:
SHUTDOWNCMD "/sbin/shutdown -h +0" MINSUPPLIES 1 POLLFREQ 5 POLLFREQALERT 1 NOCOMMWARNTIME 300
7. Build the GNOME Notification Script
Since NUT runs as root, we need a script to send notifications to your user's GNOME session. Create it with:
sudo nano /usr/local/bin/ups-notify.sh
Paste this content (replace your-username with your actual Debian login):
#!/bin/bash # Get the user's DBus session address (required for GNOME notifications) USER="your-username" DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $USER gnome-session)/environ | cut -d= -f2-) # Export the DBus address so notify-send works export DBUS_SESSION_BUS_ADDRESS # Send appropriate notification based on the event case $1 in ONBATT) su - $USER -c 'notify-send -u critical "UPS Alert" "Power outage detected! Running on battery."' ;; LOWBATT) su - $USER -c 'notify-send -u critical "UPS Alert" "Battery critically low! System will shut down immediately."' ;; ONLINE) su - $USER -c 'notify-send "UPS Alert" "Power restored! Running on mains."' ;; esac
Make the script executable:
sudo chmod +x /usr/local/bin/ups-notify.sh
8. Start & Enable NUT Services
Enable and launch the NUT server and client to run on boot:
sudo systemctl enable --now nut-server nut-client
Verify they're running properly:
sudo systemctl status nut-server nut-client
9. Test the Setup
- Power Loss Alert: Unplug the UPS from the wall. You should see a critical GNOME notification within 5 seconds.
- Power Restoration Alert: Plug the UPS back in. A confirmation notification should pop up.
- Auto-Shutdown Test (Optional): To simulate a low-battery shutdown (warning: this will turn off your system!), run:
sudo upsmon -c fsd
10. Check UPS Status Anytime
You can view real-time UPS details like battery percentage and load with:
upsc orvaldi-kc2000@localhost
内容的提问来源于stack exchange,提问作者patryk.beza




