Skip to main content

LS-WVL 使用NUT (Network UPS Tools)監控UPS

運行環境
  • LS-WVL刷Debian 12
安裝套件
apt install -y nut
設定

編輯/etc/nut/nut.conf ,修改MODE=netclient,確認權限為root nut

none: Nut 未設定
standalone: 獨立模式, 一般在只有一個 UPS 且只負責本地系統(不提供網路服務)的情況下使用.
netserver:  Nut Server.
netclient: 客戶端模式, 只啟動 nut-monitor, 用於連接遠端的 Nut 服務.

root@LS-WVL885:~# cat /etc/nut/nut.conf 
...
MODE=netclient

編輯/etc/nut/upsmon.conf,以下範例為當UPS切換到電池後執行upssched調度程序,,確認權限為root nut

#監控的UPS IP 使用者及密碼,模式為slave
MONITOR ups@192.168.1.1 1 monuser secret slave
MINSUPPLIES 1
#UPS低電量時會執行shutdown
SHUTDOWNCMD "/sbin/shutdown -h +0"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5
#NUT自帶調度程序
NOTIFYCMD /sbin/upssched
#UPS切換電池後呼叫程序
NOTIFYFLAG ONBATT EXEC

編輯/etc/nut/upssched.conf,確認權限為root nut

#執行腳本位置
CMDSCRIPT /bin/upssched-cmd
#確認以下路徑正確
PIPEFN /var/run/nut/upssched.pipe
LOCKFN /var/run/nut/upssched.lock
#當UPS切換電池後創建shutdown計時器120秒
AT ONBATT * START-TIMER shutdown 120
#當UPS恢復供電後取消計時器
AT ONLINE * CANCEL-TIMER shutdown

編輯/bin/upssched-cmd,確認權限為root nut和可執行chmod +x當觸發計時器120秒後寫入log及shutdown設備

#! /bin/sh
case $1 in
                shutdown)
                                echo "$(date): AC not restored after 120s, shutting down system." >> /var/log/ups-shutdown.log
                                sleep 3
                                sudo /sbin/shutdown -h +0

                                ;;
                onbattwarn)
                                echo "The UPS has been on battery for awhile" 
                                # Create a flag-file on the filesystem, for your own processing
                                #/usr/bin/touch /some/path/ups-on-battery
                                ;;
                ups-back-on-power)
                                logger -t upssched-cmd "NUT back on power"
                                # Delete the flag-file on the filesystem
                                #/bin/rm -f /some/path/ups-on-battery
                                ;;
                upsgone)
                                logger -t upssched-cmd "The communication with UPS has been gone for awhile"
                                ;;
                *)
                                logger -t upssched-cmd "Unrecognized command: $1"
                                ;;
esac

使用visudo,加入nut使用者允許使用shutdown指令 

root@LS-WVL885:~# visudo 

...
nut ALL=(ALL) NOPASSWD: /sbin/shutdown

 

建立nut程序可寫入log檔

touch /var/log/ups-shutdown.log

chown root:nut /var/log/ups-shutdown.log

chmod 664 /var/log/ups-shutdown.log
systemctl enable nut-monitor

systemctl start nut-monitor
測試

測試可讀取UPS狀態

root@LS-WVL885:~# upsc ups@192.168.1.1
Init SSL without certificate database
battery.charge: 96
battery.charge.low: 10
battery.charge.warning: 50
battery.date: 2001/09/25
battery.mfr.date: 2023/08/10
battery.runtime: 2636
ups.status: OL CHRG
ups.test.result: No test initiated
ups.timer.reboot: 0
ups.timer.shutdown: -1
ups.vendorid: 051d

測試UPS切換電池,120秒內未恢復AC供電則會關機和紀錄log

root@LS-WVL885:~# cat /var/log/ups-shutdown.log
Mon Sep  1 01:11:06 AM CST 2025: AC not restored after 120s, shutting down system.