UbuntuServerの停電対策を行う

リモートワークへの対応で常時稼働させる必要が出てきたUbuntuServerに停電対策を行うことにした

設定メモ

APC(現在のSchneider Electric)のSmartUPSを接続する

今回は余っていたSmartUPSをUSBで接続した

┌─╼ | up 1 day, 22:14, 1 user | [~]
└────╼ $ lsusb
Bus 001 Device 007: ID 051d:0002 American Power Conversion Uninterruptible Power Supply

apcupsdを使う場合

Apcupsd UPS control software : SourceForge

Linuxだけでなく Windows / macOS / BSD系 も連携可能

apcupsdをインストールする

┌─╼ | up 1 day, 22:20, 1 user | [~]
└────╼ $ sudo apt-get install apcupsd

設定ファイルを書き換える

┌─╼ | up 1 day, 22:28, 1 user | [~]
└────╼ $ sudo nano /etc/apcupsd/apcupsd.conf
UPSCABLE usb
UPSTYPE usb
DEVICE
LOCKFILE /var/lock
SCRIPTDIR /etc/apcupsd
PWRFAILDIR /etc/apcupsd
NOLOGINDIR /etc
ONBATTERYDELAY 6
BATTERYLEVEL 5
MINUTES 3
TIMEOUT 120
ANNOY 300
ANNOYDELAY 60
NOLOGON disable
KILLDELAY 0
NETSERVER on
NISIP 0.0.0.0
NISPORT 3551
EVENTSFILE /var/log/apcupsd.events
EVENTSFILEMAX 10
UPSCLASS standalone
UPSMODE disable
STATTIME 0
STATFILE /var/log/apcupsd.status
LOGSTATS off
DATATIME 0

書き換えた設定を反映させる

┌─╼ | up 1 day, 22:38, 1 user | [~]
└────╼ $ sudo nano /etc/default/apcupsd
ISCONFIGURED=yes
┌─╼ | up 1 day, 22:40, 1 user | [~]
└────╼ $ sudo /etc/init.d/apcupsd restart
Restarting apcupsd (via systemctl): apcupsd.service.

UPSの状態を確認する

┌─╼ | up 1 day, 22:41, 1 user | [~]
└────╼ $ apcaccess status
APC      : 001,043,1010
DATE     : 2021-04-23 07:09:48 +0900
HOSTNAME : hp-elite
VERSION  : 3.14.14 (31 May 2016) debian
UPSNAME  : hp-elite
CABLE    : USB Cable
DRIVER   : USB UPS Driver
UPSMODE  : Stand Alone
STARTTIME: 2021-04-23 07:09:45 +0900
MODEL    : Smart-UPS 1000
STATUS   : ONLINE
LINEV    : 100.8 Volts
LOADPCT  : 8.4 Percent
BCHARGE  : 100.0 Percent
TIMELEFT : 104.0 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 120 Seconds
OUTPUTV  : 101.5 Volts
SENSE    : High
DWAKE    : -1 Seconds
DSHUTD   : 90 Seconds
LOTRANS  : 92.0 Volts
HITRANS  : 108.0 Volts
RETPCT   : 0.0 Percent
ITEMP    : 30.6 C
ALARMDEL : 30 Seconds
BATTV    : 27.4 Volts
LINEFREQ : 60.2 Hz
LASTXFER : No transfers since turnon
NUMXFERS : 0
TONBATT  : 0 Seconds
CUMONBATT: 0 Seconds
XOFFBATT : N/A
SELFTEST : NO
STESTI   : 14 days
STATFLAG : 0x05000008
MANDATE  : 2012-07-19
SERIALNO : AS1229233588
BATTDATE : 2012-07-19
NOMOUTV  : 100 Volts
NOMBATTV : 24.0 Volts
FIRMWARE : 600.18.A USB FW:11.
END APC  : 2021-04-23 07:10:00 +0900

NUT(Network UPS Tools)を使う場合

Network UPS Tools

apcupsdと同様にLinuxだけでなく Windows / macOS / BSD系 も連携可能
大手NASメーカー(Synology、QNAP、Netgearなど)も標準のUPSサポートとしてNUTを使っているようなので、それらとも連携できる

NUTをインストールする

┌─╼ | up 2:35, 1 user | [~]
└────╼ $ sudo apt-get install nut

設定ファイルを書き換える

┌─╼ | up 2:58, 1 user | [~]
└────╼ $ sudo nano /etc/nut/ups.conf
[smartups]
    driver = usbhid-ups
    port = auto
┌─╼ | up 3:02, 1 user | [~]
└────╼ $ sudo upsdrvctl start
Network UPS Tools - UPS driver controller 2.7.4
Network UPS Tools - Generic HID driver 0.41 (2.7.4)
USB communication driver 0.33
Using subdriver: APC HID 0.96
┌─╼ | up 3:07, 1 user | [~]
└────╼ $ sudo nano /etc/nut/nut.conf
MODE=netserver
┌─╼ | up 3:10, 1 user | [~]
└────╼ $ sudo nano /etc/nut/upsd.conf
LISTEN 127.0.0.1 3493
LISTEN ::1 3493
┌─╼ | up 3:12, 1 user | [~]
└────╼ $ sudo nano /etc/nut/upsd.users
[upsmaster]
    password = Password1!
    upsmon master
┌─╼ | up 3:19, 1 user | [~]
└────╼ $ sudo nano /etc/nut/upsmon.conf
MONITOR [email protected] 1 upsmaster Password1! master
MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYCMD /sbin/upssched
POLLFREQ 5
POLLFREQALERT 5
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"
NOTIFYFLAG ONLINE     SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT     SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT      SYSLOG+WALL+EXEC

RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5
┌─╼ | up 3:51, 1 user | [~]
└────╼ $ sudo nano /etc/nut/upssched.conf
CMDSCRIPT /bin/upssched-cmd
PIPEFN /var/run/nut/upssched.pipe
LOCKFN /var/run/nut/upssched.lock

AT ONBATT * START-TIMER upsgone 300
AT ONLINE * CANCEL-TIMER upsgone
AT LOWBATT * START-TIMER upsgone 5
┌─╼ | up 3:59, 1 user | [~]
└────╼ $ sudo nano /bin/upssched-cmd
case $1 in
        upsgone)
                logger -t upssched-cmd "The UPS has been gone for awhile"
                upsmon -c fsd
                ;;
        lowbatt)
                logger -t upssched-cmd "The UPS became lowbattery"
                upsmon -c fsd
                ;;
        *)
                logger -t upssched-cmd "Unrecognized command: $1"
                ;;
esac
┌─╼ | up 4:08, 1 user | [~]
└────╼ $ sudo systemctl restart nut-monitor.service

UPSの状態を確認する

┌─╼ | up 4:50, 1 user | [~]
└────╼ $ upsc smartups
Init SSL without certificate database
battery.charge: 100
battery.charge.low: 10
battery.charge.warning: 50
battery.mfr.date: 2012/07/19
battery.runtime: 6240
battery.runtime.low: 120
battery.temperature: 30.1
battery.type: PbAc
battery.voltage: 27.4
battery.voltage.nominal: 24.0
device.mfr: American Power Conversion
device.model: Smart-UPS 1000
device.serial: AS1229233588
device.type: ups
driver.name: usbhid-ups
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.version: 2.7.4
driver.version.data: APC HID 0.96
driver.version.internal: 0.41
input.sensitivity: high
input.transfer.high: 108
input.transfer.low: 92
input.voltage: 100.8
output.current: 0.61
output.frequency: 60.0
output.voltage: 100.8
output.voltage.nominal: 100.0
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.delay.start: 30
ups.firmware: 600.18.A
ups.firmware.aux: 11.03
ups.load: 8.4
ups.mfr: American Power Conversion
ups.mfr.date: 2012/07/19
ups.model: Smart-UPS 1000
ups.productid: 0002
ups.serial: AS1229233588
ups.status: OL
ups.test.result: No test initiated
ups.timer.reboot: -1
ups.timer.shutdown: -1
ups.timer.start: -1
ups.vendorid: 051d