Red HatのChristian Hornです。
家電の消費電力に興味があって、僕はスマートプラグを3つ買いました。 その3つのプラグはそれぞれの携帯のアプリで消費電力の提示ができるが、 一つしかAPIの供給ができません。この記事では、そのAPIで得たデータを PCPのデータベースに保存しましょう。
ソフトをインストールする
この記事はRed Hat Enterprise Linux 9で作られたが、FedoraやCentos Streamなども使えます。
まずは必要なソフトをインストールします。Performance Co-pilotは パーフォマンス分析でよく使われているソフトです。省略して、PCPに なります。pcp-zeroconfというパッケージをインストールすると、 定義的なシステムのパーフォマンスデータの保存が始まります。 pcp-pmda-openmetricsはPCPで使われているソフトです。 jqというソフトはjsonデータの分析ができます。
dnf -y install pcp-zeroconf pcp-pmda-openmetrics jq cd /var/lib/pcp/pmdas/openmetrics/ ./Install
スマートプラグの設置
僕はSwitchBot プラグミニ スマートプラグを 買って、説明書どおりに設置して、アプリでプラグで測った消費電力の 値を読めます。
その後で、そのアプリを実行して、「プロフィール」と「設定」を クリックして、「アプリバージョン」を10回クリックして下さい。そうしたら、 「開発者向けオプション」が生じます。クリックしたら、トークンの取得が できます。これから、そのトークンを秘密のように使いましょう。
これからはスマートプラグのdeviceIdを聞きましょう。tokenとして、 取得したトークンを使って下さい。
[chris@rhel9a ~]# token="[アプリで得たトークン]"
[chris@rhel9a ~]# auth="Authorization:${token}"
[chris@rhel9a ~]# curl -s -H "${auth}" https://api.switch-bot.com/v1.0/devices | jq
{
"statusCode": 100,
"body": {
"deviceList": [
{
"deviceId": "232323424242",
"deviceName": "Plug Mini(US) 56",
"deviceType": "Plug Mini (US)",
"enableCloudService": true,
"hubDeviceId": ""
}
],
"infraredRemoteList": []
},
"message": "success"
}
[chris@rhel9a ~]#
スクリプトでAPIを使う事
これから、スクリプトでAPIを使いましょう。下記のスクリプトで 得たトークンとdeviceidを入れて下さい。
[chris@rhel9a ~]# cat switchbot.sh
#!/usr/bin/bash
token="[アプリで得たトークン]"
auth="Authorization:${token}"
deviceid="232323424242"
url_list="https://api.switch-bot.com/v1.0/devices"
url_status="https://api.switch-bot.com/v1.0/devices/${deviceid}/status"
curl -s -H "${auth}" "${url_status}" | jq >/tmp/curl_switchbot
volt=$(grep voltage /tmp/curl_switchbot | sed -e 's/,//' -e 's,.*: ,,')
echo "voltage {func=\"voltage\"} ${volt}"
weight=$(grep weight /tmp/curl_switchbot | sed -e 's/,//' -e 's,.*: ,,')
echo "weight {func=\"weight\"} ${weight}"
electricityOfDay=$(grep electricityOfDay /tmp/curl_switchbot | sed -e 's/,//' -e 's,.*: ,,')
echo "electricityOfDay {func=\"electricityOfDay\"} ${electricityOfDay}"
electricCurrent=$(grep electricCurrent /tmp/curl_switchbot | sed -e 's/,//' -e 's,.*: ,,')
echo "electricCurrent {func=\"electricCurrent\"} ${electricCurrent}"
[chris@rhel9a ~]# chmod +x switchbot.sh
実際に実行したら...
[chris@rhel9a ~]# ./switchbot.sh
voltage {func="voltage"} 101.5
weight {func="weight"} 32.1
electricityOfDay {func="electricityOfDay"} 27
electricCurrent {func="electricCurrent"} 5.3
[chris@rhel9a ~]# rm /tmp/curl_switchbot
[chris@rhel9a ~]#
openmetricsを使う
このスクリプトはメーカーのAPIを使って、データを出力します。 pcp-pmda-openmetricが期待する形ですので、これからそのスクリプトを openmetricsから実行しましょう。 /var/lib/pcp/pmdas/openmetrics/config.d/というディレクトリーに移動して から、selinuxのコンテクストを設定します。
[chris@rhel9a ~]# sudo mv switchbot.sh /var/lib/pcp/pmdas/openmetrics/config.d/
[chris@rhel9a ~]# sudo chcon --reference /var/lib/pcp/pmdas/openmetrics/config.d \
/var/lib/pcp/pmdas/openmetrics/config.d/switchbot.sh
これから、pcpもプラグのデータを示します:
[chris@rhel9a ~]$ pmrep -g openmetrics.switchbot
[ 1] - openmetrics.switchbot.electricCurrent["0 func:electricCurrent"] - none
[ 2] - openmetrics.switchbot.electricityOfDay["0 func:electricityOfDay"] - none
[ 3] - openmetrics.switchbot.weight["0 func:weight"] - none
[ 4] - openmetrics.switchbot.voltage["0 func:voltage"] - none
1 2 3 4
0.160 83.000 0.500 101.500
0.160 83.000 0.500 101.500
0.160 83.000 0.500 101.500
0.160 83.000 0.500 101.500
^C
[chris@rhel9a ~]$
現在のデータを保存ファイルにいれましょう:
[root@rhel9a ~]# cat /etc/pcp/pmlogconf/openmetrics/switchbot
#pmlogconf-setup 2.0
ident OpenMetrics switchbot
force include
openmetrics.switchbot
[root@rhel9a ~]# cd /var/lib/pcp/config/pmlogconf/openmetrics/
[root@rhel9a openmetrics]# ln -s ../../../../../../etc/pcp/pmlogconf/openmetrics/switchbot .
[root@rhel9a openmetrics]# systemctl restart pmlogger
Performance Co-Pilot configuration on rhel9a.local:
platform: Linux rhel9a.local 5.14.0-70.13.1.el9_0.x86_64 #1 SMP PREEMPT Thu Apr 14 12:42:38 EDT 2022 x86_64
hardware: 4 cpus, 1 disk, 1 node, 3722MB RAM
timezone: JST-9
services: pmcd
pmcd: Version 5.3.5-8, 12 agents, 6 clients
pmda: root pmcd proc pmproxy xfs linux nfsclient mmv kvm jbd2
dm openmetrics
pmlogger: primary logger: /var/log/pcp/pmlogger/rhel9a.local/20220918.17.33
pmie: primary engine: /var/log/pcp/pmie/rhel9a.local/pmie.log
[root@rhel9a openmetrics]#
これから、プラグの過去のデータも読めます:
[root@rhel9a openmetrics]# pmrep -p -g -a /var/log/pcp/pmlogger/rhel9a.local/20220918.17.33 openmetrics.switchbot
[ 1] - openmetrics.switchbot.voltage["0 func:voltage"] - none
[ 2] - openmetrics.switchbot.weight["0 func:weight"] - none
[ 3] - openmetrics.switchbot.electricityOfDay["0 func:electricityOfDay"] - none
[ 4] - openmetrics.switchbot.electricCurrent["0 func:electricCurrent"] - none
1 2 3 4
17:33:06 N/A N/A N/A N/A
17:33:07 N/A N/A N/A N/A
17:33:08 101.100 0.000 114.000 0.000
17:33:09 101.100 0.000 114.000 0.000
17:33:10 101.100 0.000 114.000 0.000
17:33:11 101.100 0.000 114.000 0.000
図形にしたデータ
保存したデータを図形にする事もできます。これはプラグで測ったシステムとモニターの消費量です:
