APC UPS

来自 ArchWiki

本文档描述了如何安装 APC UPS 守护进程。APC UPS 可以通过 RS-232 或 USB 串行连接与 Linux 系统通信。如果长时间断电,APC UPS 的电池容量即将耗尽时,它可以告知 Linux 系统执行安全关机。

安装软件包

安装 apcupsd 软件包。

配置 APC UPS

APC UPS 守护进程的主配置文件可以在 /etc/apcupsd/apcupsd.conf 中找到。默认配置适用于通过 USB 电缆连接的设备。

测试

首先,启用启动 apcupsd.service

接下来,等待约一分钟,确认守护进程正在运行并正确监控电池

# apcaccess status
APC      : 001,033,0819
DATE     : Sat Mar 05 SOMETIME 2005
HOSTNAME : somehostname
RELEASE  : 3.10.16
VERSION  : 3.10.16 (04 November 2004) unknown
UPSNAME  : somehostname
CABLE    : USB Cable
MODEL    : Back-UPS ES 725
UPSMODE  : Stand Alone
STARTTIME: Sat Mar SOMETIME 2005
STATUS   : ONLINE
LINEV    : 119.0 Volts
LOADPCT  :  23.0 Percent Load Capacity
BCHARGE  : 100.0 Percent
TIMELEFT :  30.5 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
LOTRANS  : 088.0 Volts
HITRANS  : 138.0 Volts
ALARMDEL : Always
BATTV    : 13.5 Volts
NUMXFERS : 0
TONBATT  : 0 seconds
CUMONBATT: 0 seconds
XOFFBATT : N/A
STATFLAG : 0x02000008 Status Flag
MANDATE  : 2002-12-02
SERIALNO : QB0249360043
BATTDATE : 2000-00-00
NOMBATTV :  12.0
FIRMWARE : 02.n2.D USB FW:n2
APCMODEL : Back-UPS ES 725
END APC  : Sat SOMETIME 2005

要完整测试您的设置

  1. /etc/apcupsd/apcupsd.conf 文件中的 TIMEOUT0 更改为 1
  2. 断开 UPS 的墙壁电源。
  3. 观察到您的 Linux 电脑在短时间内关机。
  4. 将 UPS 插回墙壁电源。
  5. 启动您的 Linux 电脑。
  6. /etc/apcupsd/apcupsd.conf 文件中的 TIMEOUT1 更改回 0

当一切正常后,剩下要做的就是启用 apcupsd 服务。

休眠而不是关机

您可以使系统休眠而不是关机。首先,确保系统可以干净地休眠。要设置休眠,请参阅 电源管理/挂起和休眠

创建休眠脚本

/usr/local/bin/hibernate 中以 root 身份创建此文件

#!/bin/bash
# Hibernate the system - designed to be called via symlink from /etc/apcupsd
# directory in case of apcupsd initiating a shutdown/reboot.  Can also be used
# interactively or from any script to cause a hibernate.

# Do the hibernate
/usr/bin/systemctl hibernate

# At this point system should be hibernated - when it comes back, we resume this script here

# On resume, tell controlling script (/etc/apcupsd/apccontrol) NOT to continue with default action (i.e. shutdown).
exit 99

使其 可执行

链接休眠脚本以便 apcupsd 使用

/etc/apcupsd 目录创建一个到该脚本的符号链接。这样做的结果是,此目录中的 apcupd 的 apccontrol 脚本将调用休眠脚本,而不是为这些操作执行默认的关机操作。

# ln -s /usr/local/bin/hibernate /etc/apcupsd/doshutdown

如果您正在运行 apcupsd 作为另一台运行 apcupsd 作为服务器的机器的客户端,并且希望您的机器在服务器关机或与服务器的通信丢失时休眠,那么您可能还希望添加

# ln -s /usr/local/bin/hibernate /etc/apcupsd/remotedown

让 apcupsd 在休眠完成后断开 UPS 电源

一旦 PC 成功休眠,通常的做法是关闭 UPS 以节省电池电量并防止电池完全耗尽。这可以通过 systemd 中的电源挂起事件来实现。

创建 /usr/lib/systemd/system-sleep/ups-kill 并将以下内容放入其中

#!/bin/bash

case $2 in

  # In the event the computer is hibernating.
  hibernate)
    case $1 in

       # Going into a hibernate state.
       pre)

         # See if this is a powerfail situation.
         if [ -f /etc/apcupsd/powerfail ]; then
           echo
           echo "ACPUPSD will now power off the UPS"
           echo
           /etc/apcupsd/apccontrol killpower
           echo
           echo "Please ensure that the UPS has powered off before rebooting"
           echo "Otherwise, the UPS may cut the power during the reboot!!!"
           echo
         fi
       ;;

       # Coming out of a hibernate state.
       post)

         # If there are remnants from a powerfail situation, remove them.
         if [ -f /etc/apcupsd/powerfail ]; then
           rm /etc/apcupsd/powerfail
         fi

         # This may also exist, need to remove it.
         if [ -f /etc/nologin ]; then
           rm /etc/nologin
         fi

	 # Restart the daemon; otherwise it may be unresponsive in a
         # second powerfailure situation.
	 systemctl try-restart apcupsd
       ;;
    esac
  ;;
esac

使该脚本 可执行

现在您可以测试您的设置

故障排除

如果通过 USB 电缆连接,桌面环境也会感知到 UPS

例如,默认的 KDE 设置是在 UPS 电池供电超过 10 分钟且鼠标未移动时使计算机进入睡眠状态。在许多计算机上,这会导致崩溃。可以从KDE 系统设置 > 电源管理 > 电池供电时更改此设置。

hid-generic :control queue full

如果您正在使用基于 systemd 的 initramfs,您可能会遇到“control queue full”日志消息每秒发生多次的情况。您需要将 hid-generic 添加到您的 modules 并重新生成您的 initramfs。

参见