简单介绍下背景:UPS现在接在群晖的NAS上,准备用它通知其他几个接在UPS上的设备停电后安全关机。群晖NAS的UPS服务器说白了也就是依赖NUT(Network UPS Tools)的一种实现,所以做需要关机的设备上配置nut-client即可。
网上的教程乱七八糟,我也没认真留档,总之把几个教程里抄过来的内容缝合在一起,搞出来一个能用的配置,先记录下。
首先在群晖DSM上配置,进入“控制面板>硬件和电源>不断电系统”。由于我的UPS在带动NAS和两台虚拟机宿主机后能够运行25分钟,我设置了断电后10分钟待机,然后启用网络UPS服务器并在“允许的Synology NAS设备”中填入其他设备的IP地址。
随后在需要安全关机的设备上进行设置。先安装nut-client
sudo apt install nut-client
然后进入/etc/nut
设置配置文件。我抄来抄去,最后缝出来一套,我也懒得解释具体哪个配置项是什么意思了,将就着先用着吧,不行就看文档。主要是三个配置文件和一个脚本。
# /etc/nut/nut.conf,所有者为root:nut,权限为640
MODE=netclient
# /etc/nut/upsmon.conf,所有者为root:nut,权限为640
# RUN_AS_USER root
# 某些教程里使用root运行upsmon,但实测非root账户也没问题;nut-client的upsmon会分root权限和普通权限两个进程,分别执行不同权限的操作
MONITOR ups@群晖NAS的IP 1 monuser secret slave
# 群晖NAS的UPS服务器统一使用上述的用户和密码
MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h now"
NOTIFYCMD /usr/sbin/upssched
POLLFREQ 2
POLLFREQALERT 1
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
NOTIFYMSG ONLINE "UPS %s on line power"
NOTIFYMSG ONBATT "UPS %s on battery"
NOTIFYMSG LOWBATT "UPS %s battery is low"
NOTIFYMSG FSD "UPS %s: forced shutdown in progress"
NOTIFYMSG COMMOK "Communications with UPS %s established"
NOTIFYMSG COMMBAD "Communications with UPS %s lost!"
NOTIFYMSG SHUTDOWN "Auto logout and shutdown proceeding"
NOTIFYMSG REPLBATT "UPS %s battery needs to be replaced"
NOTIFYMSG NOCOMM "UPS %s is unavailable"
NOTIFYMSG NOPARENT "UPSMon parent process died - shutdown impossible"
NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT SYSLOG+WALL
NOTIFYFLAG FSD SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL
NOTIFYFLAG NOCOMM SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT SYSLOG+WALL
RBWARNTIME 43200
NOCOMMWARNTIME 600
FINALDELAY 5
# /etc/nut/upssched.conf,所有者为root:nut,权限为640
CMDSCRIPT /etc/nut/upssched-cmd
# 运行相应命令的脚本
PIPEFN /var/run/nut/upssched.pipe
LOCKFN /var/run/nut/upssched.lock
# 这两个文件稍微注意下,如果路径不存在或无权限会导致upssched运行失败
AT ONBATT * START-TIMER onbatt 60
AT ONLINE * CANCEL-TIMER onbatt
# 简单解释下……ONBATT时触发一个onbatt计时器,达到60s计时后未被取消的话则以onbatt为参数执行上面那个脚本;下略
AT LOWBATT * START-TIMER shutdowncritical 10
AT ONLINE * CANCEL-TIMER shutdowncritical
AT COMMBAD * START-TIMER upsgone 30
AT COMMOK * CANCEL-TIMER upsgone
# /etc/nut/upssched-cmd,所有者为root:root,权限为755
#! /bin/sh
case $1 in
onbatt)
logger -t upssched-cmd "The UPS has been on battery for awhile, shutdown"
/usr/sbin/upsmon -c fsd
;;
shutdowncritical)
logger -t upssched-cmd "UPS on battery critical, forced shutdown"
/usr/sbin/upsmon -c fsd
;;
upsgone)
logger -t upssched-cmd "The UPS has been gone for awhile"
;;
*)
logger -t upssched-cmd "Unrecognized command: $1"
;;
esac
大概就是这四个文件,搞了nm个把小时。不同设备设定的关机时间不一样,既然UPS能供电25分钟,NAS设定了断电10分钟后待机,那么剩下的设备在10分钟内陆续关机就行了。于是设定虚拟机宿主机等物理设备等待5分钟,虚拟机等待1分钟,就能实现虚拟机->宿主机等设备->作为UPS服务器的NAS依次关机了。