前提条件
- ハード
- Raspbery Pi 3B+
- OMRON BW120T(添付の通信用USBケーブル)
- ソフト
- Raspberry Pi OS Lite(今回はMay 2020を利用)
UPSをOSから認識させるための設定
Raspberry PiとUPSをUPSに添付されている通信用USBケーブルで接続します。
デバイスのIDなどの確認
lsusbコマンドを叩いて、UPSが見えているか確認します。
$ lsusb
Bus 001 Device 005: ID 0590:00d2 Omron Corp.
Bus 001 Device 004: ID 0424:7800 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
udevの設定を行う
udevの設定を変更し、常に /dev/ups でUPSにアクセス出来るようにします。
下記の設定ファイルの中での idVendor / idProduct は lsusb で出力された「0590:00d2」を「:」で分割して分けて設定します。
# Omron BW120T - blazer_usb
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", \
ATTRS{idVendor}=="0590", ATTRS{idProduct}=="00d2", \
MODE="0664", GROUP="nut", SYMLINK+="ups"
- OMRON BY35SはidProductが0080でした。
次のコマンドで設定を再読込します。
$ sudo udevadm control --reload
USBケーブルを抜き差しするか、Raspberry Piを再起動すると、/dev/の下にupsデバイスが生えてきます。
$ ls /dev/ups
/dev/ups
Network UPS Tools(nut)の導入と設定
nutのインストール
$ sudo apt update
$ sudo apt install nut
nutの設定
MODE=standalone
下記の設定ファイルの中での vendorid / productid は lsusb で出力された「0590:00d2」を「:」で分割して分けて設定します。
default.battery.voltageのhigh/lowは電圧でバッテリーの充電率を見ているため、個別に調整必要な場合あります。
[bw120t]
driver = blazer_usb
port = /dev/ups
desc = "Omron UPS BW120T"
vendorid = 0590
productid = 00d2
subdriver = ippon
default.battery.voltage.high = 27.2
default.battery.voltage.low = 11
upsmon.conf / upsd.users の upsmon / upspass はユーザ名とパスワードになるため、必要に応じて変更してください。
MONITOR bw120t@localhost 1 upsmon upspass master
[upsmon]
password = upspass
upsmon master
動作確認
次のコマンドを叩いて、「read: (101.9 000.0 102.9 000 59.9 26.9 39.0 00101000」のように表示が出ればとりあえず、OKだと思います。
$ sudo -u nut /lib/nut/blazer_usb -a bw120t -DDD
nutサービスの再起動
サービスを再起動し、Active: active(running)になっていることを確認します。
$ sudo sudo systemctl restart nut-server
$ sudo systemctl status nut-server
● nut-server.service - Network UPS Tools - power devices information server
Loaded: loaded (/lib/systemd/system/nut-server.service; enabled; vendor prese
Active: active (running) since Sat 2020-06-27 23:57:01 BST; 11min ago
Process: 851 ExecStart=/sbin/upsd (code=exited, status=0/SUCCESS)
Main PID: 852 (upsd)
Tasks: 1 (limit: 2200)
Memory: 1.4M
CGroup: /system.slice/nut-server.service
mq852 /lib/nut/upsd
再度、動作確認
upscコマンドでUPSの状態が確認できれば完了です。
$ upsc bw120t
Init SSL without certificate database
battery.charge: 100
battery.voltage: 27.00
battery.voltage.high: 27.2
battery.voltage.low: 11
device.type: ups
driver.name: blazer_usb
driver.parameter.pollinterval: 2
driver.parameter.port: /dev/ups
driver.parameter.productid: 00d2
driver.parameter.subdriver: ippon
driver.parameter.synchronous: no
driver.parameter.vendorid: 0590
driver.version: 2.7.4
driver.version.internal: 0.12
input.frequency: 60.0
input.voltage: 101.9
input.voltage.fault: 0.0
output.voltage: 102.9
ups.beeper.status: disabled
ups.delay.shutdown: 30
ups.delay.start: 180
ups.load: 0
ups.productid: 00d2
ups.status: OL BYPASS
ups.temperature: 39.0
ups.type: offline / line interactive
ups.vendorid: 0590
Zabbix Agentの導入と設定
Zabbix Agentのインストール
$ sudo wget https://repo.zabbix.com/zabbix/5.0/raspbian/pool/main/z/zabbix-release/zabbix-release_5.0-1+$(lsb_release -sc)_all.deb
$ sudo dpkg -i zabbix-release_5.0-1+$(lsb_release -sc)_all.deb
$ sudo apt update
$ sudo apt install zabbix-agent
Zabbix Agentの設定
基本的な設定はお好みで
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.x.x
ServerActive=192.168.x.x
Hostname=UPS
nutと連動する部分はこんな感じです。
, のあとのパスは、このあと作成するシェルスクリプトのパスと合わせます。
UserParameter=upsmon[*],/etc/zabbix/scripts/nut-ups.sh $1 $2
nutからUPSの情報を取得するためのシェルスクリプト
このスクリプトのベースはどっかの受け売りなんだけど、情報元が探し切れませんでした。
# !/bin/bash
# For Raspberry Pi OS
# zabbix_agentd.conf
# UserParameter=upsmon[*],/etc/zabbix/scripts/nut-ups.sh $1 $2
# UPSC Path
UPSC="/bin/upsc"
if [ -z $1 ] || [ $1 != ups.discovery ] && [ -z $2 ]; then
echo "Usage: $0 ups.discovery"
echo " $0 [UPS Name] [Action]"
exit 0
fi
if [ $1 = ups.discovery ]; then
echo -e "{\n\t\"data\":["
first=1
${UPSC} -l 2>&1 | grep -v SSL | while read discovered ; do
if [ $first -eq 0 ]; then
echo ","
fi
echo -e "\t\t{ \"{#UPSNAME}\":\t\"${discovered}\" }"
first=0
done
echo -e "\n\t]\n}"
else
key=$2
if [ $key = ups.status ]; then
state=`${UPSC} $1 $key 2>&1 | grep -v SSL`
# echo $state
case $state in
OL*) echo 1 ;; #'On line (mains is present)' ;;
OB) echo 2 ;; #'On battery (mains is not present)' ;;
LB) echo 3 ;; #'Low battery' ;;
RB) echo 4 ;; #'The battery needs to be replaced' ;;
CHRG) echo 5 ;; #'The battery is charging' ;;
DISCHRG) echo 6 ;; #'The battery is discharging (inverter is providing load power)' ;;
BYPASS) echo 7 ;; #'UPS bypass circuit is active echo no battery protection is available' ;;
CAL) echo 8 ;; #'UPS is currently performing runtime calibration (on battery)' ;;
OFF) echo 9 ;; #'UPS is offline and is not supplying power to the load' ;;
OVER) echo 10 ;; #'UPS is overloaded' ;;
TRIM) echo 11 ;; #'UPS is trimming incoming voltage (called "buck" in some hardware)' ;;
BOOST) echo 12 ;; #'UPS is boosting incoming voltage' ;;
* ) echo 0 ;; #'unknown state' ;;
esac
else
${UPSC} $1 $key 2>&1 | grep -v SSL
fi
fi
$ sudo chmod 755 /etc/zabbix/scripts/nut-ups.sh
Zabbix Serverの設定
下記のテンプレートをインポートして、対象のホストに割り当てる
参考にした記事
Omron BY50S を Linux で使う (udev のイケてる設定編) - @sugi_0000 - Qiita