CyberPowerのUPSをLinux(Fedora 25)に導入してmuninでグラフ化する

## UPSのセットアップ

環境は下記の通り。`munin`は既に動いているものがあるという前提で進める

```shell-session
sh-4.3# cat /etc/redhat-release
Fedora release 25 (Twenty Five)
sh-4.3# uname -a
Linux tndl.net 4.11.3-202.fc25.x86_64 #1 SMP Mon Jun 5 16:38:21 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
sh-4.3# rpm -qa | grep munin
munin-common-2.0.33-1.fc25.noarch
munin-nginx-2.0.33-1.fc25.noarch
munin-2.0.33-1.fc25.noarch
munin-node-2.0.33-1.fc25.noarch
```

マニュアルに従って下記の順番で設置を行う
3については、付属する日本語マニュアルには載っていないため、[CPJ500用のダウンロードページ](https://www.cyberpower.com/jp/jp/product/sku/CPJ500)から英語マニュアルとrpmファイルをダウンロードして、インストールを行う

1. 8時間以上の充電
2. UPSのコンセントへPC(Server)を接続
3. 管理用ツールのインストール
4. USBケーブルの接続

セットアップが正常に完了していれば、`pwrstat`コマンドでUPSの状態を取得できる

```shell-session
sh-4.3# pwrstat -status

The UPS information shows as following:

	Properties:
		Model Name................... CPJ500
		Rating Voltage............... 100 V
		Rating Power................. 300 Watt(500 VA)

	Current UPS status:
		State........................ Normal
		Power Supply by.............. Utility Power
		Utility Voltage.............. 100 V
		Output Voltage............... 100 V
		Battery Capacity............. 100 %
		Remaining Runtime............ 27 min.
		Load......................... 93 Watt(31 %)
		Line Interaction............. None
		Test Result.................. Unknown
		Last Power Event............. Blackout at 2017/10/23 02:13:16
```

## Muninでのグラフ化

`pwrstat`で見た感じ`Load`の消費電力くらいしかグラフ化するものがないので、それ用にテキトーにスクリプトを仕立てる

```:/etc/munin/plugin-conf.d/wattage
[wattage]
user root
```

```shell-session:/etc/munin/plugins/wattage
#!/bin/bash

if [ "$1" = "autoconf" ]; then
  echo yes
  exit 0
fi

if [ "$1" = "config" ]; then

  echo 'graph_title Server wattage'
  echo 'graph_args --base 1000 -l 0'
  echo 'graph_vlabel Wattage'

  echo 'graph_scale no'
  echo 'graph_category sensors'

  echo 'wattage.label Server'
  print_warning wattage
  print_critical wattage

  echo 'graph_info Server wattage'
  echo 'wattage.info /sbin/pwrstat'

  # Last, if run with the "config"-parameter, quit here (don't
  # display any data)
  exit 0
fi

echo -n "wattage.value "
pwrstat -status | grep Load | cut -d' ' -f2
```

`/etc/munin/plugins/wattage`は実行できるよう実行権限を付与しておく

```shell-session
sh-4.3# chmod +x /etc/munin/plugins/wattage
sh-4.3# ls -la /etc/munin/plugins/wattage
-rwxr-xr-x 1 root root 578 10月 31 21:11 /etc/munin/plugins/wattage
```

`munin-node`を念のためリロードしておく

```shell-session
sh-4.3# systemctl restart munin-node.service
```

うまく行けば下記のようなグラフが生成される

![wattage-day.png](https://qiita-image-store.s3.amazonaws.com/0/2978/a93c847f-ed5b-6f49-2236-add4a1a67c78.png)

![wattage-week.png](https://qiita-image-store.s3.amazonaws.com/0/2978/88cd9c4d-462f-6166-6c6b-336febcddb75.png)

_Sun 29 Oct_の消費電力の上がりは、`mdadm`のcheckによるもの。HDDがぶん回ると30Wほど消費電力が高くなるのがよく分かる

## 余談

UPSを購入後のテスト以降特に出番が無かったと思ったが、先日の台風の際に長時間停電していたようで、ログを見たところそれ以前にも何度か瞬断的なUPSの出番があったようだ

```shell-session:/var/log/pwrstatd.log
sh-4.3# cat /var/log/pwrstatd.log
2017/07/21 16:29:14 PM	Daemon startups.
2017/07/21 16:30:26 PM	Communication is established.
2017/07/21 16:34:57 PM	Utility failed.
2017/07/21 16:35:07 PM	Battery capacity is low.
2017/07/21 16:35:07 PM	The UPS will be turned off at 10 min., 0 sec. later.
2017/07/21 16:35:07 PM	Begin to shutdown PC immediately!
2017/07/21 16:37:01 PM	Daemon startups.
2017/07/21 16:37:03 PM	Communication is established.
2017/07/21 16:48:55 PM	Daemon startups.
2017/07/21 17:25:17 PM	Daemon stops its service.
2017/07/21 17:33:08 PM	Daemon startups.
2017/07/21 17:33:10 PM	Communication is established.
2017/07/21 17:47:09 PM	Daemon stops its service.
2017/07/21 17:47:09 PM	Daemon startups.
2017/07/21 17:47:09 PM	Communication is established.
2017/07/21 17:49:24 PM	Utility failed.
2017/07/21 17:49:32 PM	Utility restored.
2017/08/19 16:16:10 PM	Utility failed.
2017/08/19 16:16:27 PM	Utility restored.
2017/08/19 16:16:33 PM	Utility failed.
2017/08/19 16:16:36 PM	Utility restored.
2017/08/19 16:23:13 PM	Utility failed.
2017/08/19 16:23:33 PM	Utility restored.
2017/08/19 16:27:31 PM	Utility failed.
2017/08/19 16:27:52 PM	Utility restored.
2017/08/19 16:27:55 PM	Utility failed.
2017/08/19 16:27:58 PM	Utility restored.
2017/08/19 16:44:34 PM	Daemon stops its service.
2017/08/19 16:45:42 PM	Daemon startups.
2017/08/19 16:45:44 PM	Communication is established.
2017/10/23 02:13:16 AM	Utility failed.
2017/10/23 02:14:17 AM	The UPS will be turned off at 10 min., 0 sec. later.
2017/10/23 02:14:17 AM	Begin to shutdown PC immediately!
2017/10/24 16:52:04 PM	Daemon startups.
2017/10/24 16:52:05 PM	Communication is established.
```

_7/21_が導入日
_8/19_に何度か瞬断かUSBの接続不良でもあった？
_10/23_が台風による停電