2019-05-08

Linux Mint (Ubuntu) 使用 NUT (Network UPS Tools)


因為舊的台達水瓶座UPS因為沒有Linux上可支援的軟體,因此改採買了一台科風WAR-1000AP(更新:用了二年多同時採購的二台同型號UPS板子都壞了,強列不建議購買),
不過科風的Linux軟體裝上去發現爛得可以(Linux UPSMON PRO),看起來用java7寫的,體積大不說,不但抓不到usb設備,一些test指令下了還會噴錯誤....因此用Linux作業系統的就不建議買科風了,當初是看它有支援Linux才買的,沒想到Linux軟體寫得這麼爛(科風工程師自已都沒先好好測過嗎?),早知就買台達的新型號UPS。

不過買都買了,要是沒法解決就要換貨了,還好看到NUT(Network UPS Tools)的相容列表對科風的支援好像還不錯:
因此就改用NUT來監視UPS,不過不得不說這軟體設定有夠囉嗦的...

=================================
OS:Linux Mint 19.04 Xfce (Ubuntu 18.04)

1.安裝以下套件:
apt-get install nut-server nut-client nut-monitor s-nail

2.設定/etc/nut.conf:(設定為netserver,以方便監控)
MODE=netserver

3.設定/etc/ups.conf: (這邊請參考Nut官方相容列表,找到適合的driver,並自定ups名稱為myups)
[myups]
driver = usbhid-ups
port = auto

4.設定/etc/upsd.conf:(server要開放的port,因為目前只有本機,所以只開放local ip)
LISTEN 127.0.0.1 3493
LISTEN ::1 3493

5.設定/etc/upsd.users:(新增使用者admin,權限可以設置變數及下指令,因為只有一台電腦,設為master即可)
[admin]
password = admin
actions = SET
instcmds = ALL
upsmon master

6.設定/etc/upsmon.conf:
MONITOR myups@localhost 1 admin admin master
SHUTDOWNCMD "/sbin/shutdown -h now"
NOTIFYCMD /sbin/upssched
NOTIFYFLAG ONLINE       SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT       SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD       SYSLOG+WALL+EXEC

7.設定/etc/upssched.conf:

CMDSCRIPT /bin/upssched-cmd
PIPEFN /var/run/nut/upssched/upssched.pipe
LOCKFN /var/run/nut/upssched/upssched.lock

#斷電後60秒後關機
AT ONBATT * EXECUTE onbattstart
AT ONBATT * START-TIMER onbattwarn 60

#復電後取消關機並通知
AT ONLINE * CANCEL-TIMER onbattwarn
AT ONLINE * EXECUTE ups-back-on-power

#無法連接UPS時通知
AT COMMBAD * START-TIMER upsgone 10

8.設定/bin/upssched-cmd:
case $1 in
        onbattstart)
                echo "The UPS has been on battery, and will shutdown in one minute." \
                | s-nail -s "UPS斷電啦!一分鐘後準備關機啦!" -v your-email-account@gmail.com
                #重要,要使用TIMER功能,必須先建立/var/run/nut/upssched資料夾,如同upssched.conf中的PIPEFN及LOCKFN設定一樣
                mkdir -p /var/run/nut/upssched
                logger -t upssched-cmd "The UPS has been on battery and will shutdown in one minute!"
                ;;
        onbattwarn)
                logger -t upssched-cmd "The system will shutdown now!"
                /sbin/upsmon -c fsd
                ;;
        ups-back-on-power)
                echo "the UPS has return power." \
                | s-nail -s "UPS復電啦!我又不關機啦!" -v your-email-account@gmail.com
                logger -t upssched-cmd "The UPS has been back on power!"
                ;;
        upsgone)
                echo "lost connection to UPS!please check UPS ASAP." \
                | s-nail -s "UPS不見啦!是不是快懷啦!" -v your-email-account@gmail.com
                logger -t upssched-cmd "The UPS has been gone for 10 seconds!"
                ;;
        *)
                logger -t upssched-cmd "Unrecognized command: $1"
                ;;
esac

9.設定/etc/s-nail.rc: (使用s-nail透過外部smtp寄通知信)
#SMTP SETTING
set v15-compat ssl-method=auto
set mta=smtps://accnout:password@your-email-smtp-server
set from=account@your-email-domain.com

(若使用gmail當smtp,請先照此設定)

10.測試
upsdrvctl start
upscmd -l myups
upsc myups
NUT-Monitor

systemctl enable nut-server
systemctl enable nut-client
systemctl start nut-server
systemctl start nut-client
systemctl status nut-server
systemctl status nut-client

11.事後發現重開機時,nut-driver.service有時沒法成功載入(usb裝置還沒ready的關係?),但nut-driver.service本身似乎已經有加了等待udev裝置ready的條件:
After = ... systemd-udev-settle.service
Wants=systemd-udev-settle.service

原來是我忘了把usbhid-ups相關的udev rules手動複製過去,所以還要再:
cp /lib/udev/rules.d/62-nut-usbups.rules /etc/udev/rules.d/

======== 要是做了上個複製udev rules的步驟,這邊可省略,除非不起作用====
或者自已手動加udev rule也可以(確保usb root hub裝置有載入):
#vi /etc/udev/rules.d/20-usb-bus.rules
新增
KERNEL=="usb[1-2]", TAG+="systemd"

# vi /lib/systemd/system/nut-driver.service
加入
After=... dev-bus-usb-001-001.device dev-bus-usb-002-001.device
============================================================

測試一下:
#sudo service udev reload
#sudo service udev restart
# systemctl status dev-bus-usb-001-001.device
# systemctl status dev-bus-usb-001-002.device


2 則留言: