使用 UPS 自带的 USB 线缆插到 NAS 主机 USB 接口之后,ESXi 的 USB 设备列表里也能正确地识别到了该设备。但将该设备添加到 Linux 虚拟机上之后,apcupsd 却无法获取该 UPS 的设备信息,而且在内核日志中一直会出现 USB disconnect 的信息,emmm,怀疑是 ESXi 直通 USB 的问题,遂放弃。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[ 124.759971] usb 1-2.1: USB disconnect, device number 4 [ 126.840674] usb 1-2.1: new full-speed USB device number 5 using uhci_hcd [ 127.364001] usb 1-2.1: New USB device found, idVendor=051d, idProduct=0002, bcdDevice= 1.06 [ 127.364006] usb 1-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [ 127.364009] usb 1-2.1: Product: Back-UPS BK650M2-CH FW:294803G -292804G [ 127.364011] usb 1-2.1: Manufacturer: American Power Conversion [ 127.364013] usb 1-2.1: SerialNumber: 9B2118A06920 [ 127.403811] hid-generic 0003:051D:0002.0003: hiddev0,hidraw1: USB HID v1.10 Device [American Power Conversion Back-UPS BK650M2-CH FW:294803G -292804G ] on usb-0000:02:01.0-2.1/input0 [ 248.394259] usb 1-2.1: USB disconnect, device number 5 [ 250.589751] usb 1-2.1: new full-speed USB device number 6 using uhci_hcd [ 251.123039] usb 1-2.1: New USB device found, idVendor=051d, idProduct=0002, bcdDevice= 1.06 [ 251.123044] usb 1-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [ 251.123048] usb 1-2.1: Product: Back-UPS BK650M2-CH FW:294803G -292804G [ 251.123050] usb 1-2.1: Manufacturer: American Power Conversion [ 251.123052] usb 1-2.1: SerialNumber: 9B2118A06920 [ 251.160223] hid-generic 0003:051D:0002.0004: hiddev0,hidraw1: USB HID v1.10 Device [American Power Conversion Back-UPS BK650M2-CH FW:294803G -292804G ] on usb-0000:02:01.0-2.1/input0 [ 268.621010] usb 1-2.1: USB disconnect, device number 6
不幸 USB 直通给虚拟机的方案翻车了,于是想着要不将 USB 线缆连接到我的 R4S 软路由上🤔️。连接到软路由上要比在 ESXi 主机上好一些,这样在来电之后写的监控脚本也能检测到 UPS 已经通电了,这样就可以自动启动 NAS 主机以及上面的一些 VM。于是就琢磨了以下的方案,线路图如下:
我的 NAS 服务器、交换机、R4S 软路由的电源都连接到 UPS 上。R6300v2 通过无线桥接的方式连接到房东家的 Wi-Fi。无线桥接之后,R6300v2 就变成了一台”无线交换机”,连接到它的设备将会从房东家 Wi-Fi 路由器的 DHCP 那里获取到同一网段的 IP。R4S 的 WAN 口通过网线连接到 R6300v2 的 LAN 口上,这样 R4S 就能通过 R6300v2 连接到房东家的 Wi-Fi,从而连接到公网。
使用 lsusb 或者 dmesg 命令查看 USB 设备是否正常连接以及内核加载 USB 设备的信息。如果 USB 设备能正常识别到,那就没问题啦。如果没出现的话,那就重启大法好!看看重启之后能不能识别到 UPS 设备信息。
1 2 3 4 5 6 7 8 9 10
# root @ OpenWrt in ~ [21:04:29] $ lsusb Bus 005 Device 003: ID 051d:0002 American Power Conversion Uninterruptible Power Supply # root @ OpenWrt in ~ [21:04:29] $ dmesg [ 64.485003] usb 5-1: new full-speed USB device number 2 using xhci-hcd [ 64.669133] hid-generic 0003:051D:0002.0001: hiddev96,hidraw0: USB HID v1.10 Device [American Power Conversion Back-UPS BK650M2-CH FW:294803G -292804G ] on usb-xhci-hcd.0.auto-1/input0 [ 1260.590529] usb 5-1: USB disconnect, device number 2 [ 1261.285846] usb 5-1: new full-speed USB device number 3 using xhci-hcd [ 1261.468989] hid-generic 0003:051D:0002.0002: hiddev96,hidraw0: USB HID v1.10 Device [American Power Conversion Back-UPS BK650M2-CH FW:294803G -292804G ] on usb-xhci-hcd.0.auto-1/input0
doshutdown When the UPS is running on batteries and one of the limits expires (time, run, load), this event is generated to cause the machine to shutdown.
Default: Shuts down the system using shutdown -h or similar
当出现如下事件的时候则会调用 doshutdown 后的执行内容:
UPS 电池即将用尽
UPS 运行在电池模式下剩余时间低于所定义的 MINUTES 值
UPS 电池剩余百分比低于所定义的 BATTERYLEVEL 值
UPS. 运行在电池模式下所超出的时间 TIMEOUT 值
When one of the conditions listed below occurs, apcupsd issues a shutdown command by calling /etc/apcupsd/apccontrol doshutdown, which should perform a shutdown of your system using the system shutdown(8) command. You can modify the behavior as described in Customizing Event Handling.
The conditions that trigger the shutdown can be any of the following:
Running time on batteries have expired (TIMEOUT)
The battery runtime remaining is below the configured value (BATTERYLEVEL)
The estimated remaining runtime is below the configured value (MINUTES)
#!/bin/sh LOG_PATH=/vmfs/volumes/NVME/.log/suspend.log echo"$(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)" >> ${LOG_PATH}
poweroff_vms(){ for vm in $(vim-cmd vmsvc/getallvms | grep -E 'NAS' | awk '{print $1}' | xargs); do if vim-cmd vmsvc/power.getstate ${vm} | grep 'Powered on'; then echo"$(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S) shutdown vm ${vm}" >> ${LOG_PATH} vim-cmd vmsvc/power.shutdown ${vm} fi done }
suspend_vms(){ for vm in $(vim-cmd vmsvc/getallvms | grep -Ev 'NAS|Vmid' | awk '{print $1}' | xargs); do if vim-cmd vmsvc/power.getstate ${vm} | grep 'Powered on'; then echo"$(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S) suppend vm ${vm}" >> ${LOG_PATH} vim-cmd vmsvc/power.suspend ${vm} done
}
suspend_vms poweroff_vms echo"Poweroff at $(TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)" >> ${LOG_PATH} /bin/host_shutdown.sh