USBRH on Linux

手順 (MRTG)

動作確認

  • 先代サーバ機器が故障して早一年、室内とは言え野ざらしで放置してしまったが、無事動作しているようだ。
Jul 16 20:16:01 rp3 kernel: [183627.477953] usb 1-1.3: USB disconnect, device number 6
Jul 16 20:16:01 rp3 upowerd[1459]: unhandled action 'unbind' on /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/0003:1774:1001.0001
Jul 16 20:16:01 rp3 upowerd[1459]: unhandled action 'unbind' on /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0
Jul 16 20:16:01 rp3 upowerd[1459]: unhandled action 'unbind' on /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3
Jul 16 20:16:03 rp3 kernel: [183629.490329] usb 1-1.3: new low-speed USB device number 7 using dwc_otg
Jul 16 20:16:03 rp3 kernel: [183629.606053] usb 1-1.3: New USB device found, idVendor=1774, idProduct=1001
Jul 16 20:16:03 rp3 kernel: [183629.606069] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Jul 16 20:16:03 rp3 kernel: [183629.606080] usb 1-1.3: Product: Hygrometer/Thermometer
Jul 16 20:16:03 rp3 kernel: [183629.606089] usb 1-1.3: Manufacturer: Strawberry Linux Co.,Ltd.
Jul 16 20:16:03 rp3 kernel: [183629.612976] hid-generic 0003:1774:1001.0002: hiddev0,hidraw0: USB HID v1.00 Device [Strawberry Linux Co.,Ltd. Hygrometer/Thermometer] on usb-3f980000.usb-1.3/input0
Jul 16 20:16:04 rp3 mtp-probe: checking bus 1, device 7: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3"
Jul 16 20:16:04 rp3 mtp-probe: bus: 1, device: 7 was not an MTP device
Jul 16 20:16:04 rp3 upowerd[1459]: unhandled action 'bind' on /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/0003:1774:1001.0002
Jul 16 20:16:04 rp3 upowerd[1459]: unhandled action 'bind' on /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0
Jul 16 20:16:04 rp3 upowerd[1459]: unhandled action 'bind' on /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3

USBRHコマンドのインストール

github.com

  • githubからRSBRHコマンドをダウンロードする
$ sudo apt install gcc libusb-dev

$ make

$ sudo chown root:root usbrh 

$ sudo chmod u+s usbrh 

$ ./usbrh 
26.78 57.03

MRTGシェルスクリプト

#! /usr/bin/zsh

export LANG=C

(( TEMPERATURE    = 0.0 ))
(( HUMIDITY       = 0.0 ))


TEMPERATURE=`/home/cutxout/bin/usbrh | awk '{ print $1*100 }'`
HUMIDITY=`/home/cutxout/bin/usbrh | awk '{ print $2*100 }'`

echo $TEMPERATURE
echo $HUMIDITY
echo "0"
echo "USBRH"

mrtg.conf

Target[rp3.usbrh]: `/home/cutxout/bin/usbrh_mrtg.sh`
Options[rp3.usbrh]: growright, gauge, nopercent, dorelpercent
MaxBytes[rp3.usbrh]: 100
ShortLegend[rp3.usbrh]: Bytes
kMG[rp3.usbrh]: 
kilo[rp3.usbrh]: 1000
YLegend[rp3.usbrh]: USBRH Temperature/Humidity
Legend1[rp3.usbrh]: USBRH Temperature
Legend2[rp3.usbrh]: USBRH Humidity
LegendI[rp3.usbrh]: USBRH Temperature
LegendO[rp3.usbrh]: USBRH Humidity
XSize[rp3.usbrh]: 600
YSize[rp3.usbrh]: 200
Title[rp3.usbrh]: USBRH Temperature/Humidity
PageTop[rp3.usbrh]: <H1>USBRH Temperature/Humidity</H1>

結果

f:id:cutxout:20190728120959p:plain

  • 一応表示できたが、MRTGって小数を扱えないことを思い出した。観測対象が室温と湿度で整数のみと言うのはさすがにおおざっぱにすぎると思うので、とりあえず100倍してお茶を濁すとして、根本的にはRRDToolsをやり直しであるな。
  • せっかくだから、Raspberry Piの温度も観測対象に入れてみましょう。CPUとかHDD位だったら取れるんじゃないだろうか。

手順 (RRDTool)

データ系列

  • 以前は気象庁アメダスデータも取得していたけど、半分以上の期間でうまく行かなかったので今回は見送り。
  • USBRHから取得する室温と湿度、Raspberry Piから取得するCPU温度の三系列でスタートしましょう。
  • RRDToolsはアーキテクチャが異なるとデータベースの使い回しはできないらしいので、最初からやり直し。orz
% rrdtool dump usbrh.rrd
ERROR: This RRD was created on another architecture

Raspberry PiのCPU温度を取得

% cat /sys/devices/virtual/thermal/thermal_zone0/temp | awk '{ print $1/1000 }'
41.856

rrdデータベースを作成

rrdtool create usbrh.rrd                                     \
                         --start "07/01/2019 00:00"           \
                         --step 300                           \
                         DS:TEMPERATURE_CPU:GAUGE:600:U:100   \
                         DS:TEMPERATURE_ROOM:GAUGE:600:U:100  \
                         DS:HUMIDITY_ROOM:GAUGE:600:U:100          \
                         RRA:AVERAGE:0.5:1:210840             \
                         RRA:MAX:0.5:1:210840                 \
                         RRA:MIN:0.5:1:210840                 \
                         RRA:LAST:0.5:1:210840

$ ls -lh
-rw-rw-r-- 1 cutxout cutxout  20M  7月 28 16:12 usbrh.rrd

スクリプト

#! /usr/bin/zsh

#Initialize Variables

export LANG=C

(( TEMPERATURE_CPU     = 0.0 ))
(( TEMPERATURE_ROOM    = 0.0 ))
(( HUMIDITY_ROOM       = 0.0 ))


TEMPERATURE_CPU=`cat /sys/devices/virtual/thermal/thermal_zone0/temp | awk '{ print $1/1000 }'`
TEMPERATURE_ROOM=`/home/cutxout/bin/usbrh | awk '{ print $1 }'`
HUMIDITY_ROOM=`/home/cutxout/bin/usbrh | awk '{ print $2 }'`


rrdtool update /home/cutxout/public_html/usbrh/usbrh.rrd N:$TEMPERATURE_CPU:$TEMPERATURE_ROOM:$HUMIDITY_ROOM


rrdtool graph   /home/cutxout/public_html/usbrh/usbrh-day.png                      \
               --imgformat=PNG                                                     \
               --start=-173100                                                     \
               --end=-300                                                          \
               --base=1000                                                         \
               --height=200                                                        \
               --width=600                                                         \
               --title="Temperature and Humidity (Daily)"                          \
               --vertical-label="Celsius / Percent"                                \
               DEF:a="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_CPU:AVERAGE         \
               DEF:b="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_ROOM:AVERAGE        \
               DEF:c="/home/cutxout/public_html/usbrh/usbrh.rrd":HUMIDITY_ROOM:AVERAGE           \
               AREA:a#00663B:"CPU  Temperature"                                    \
               GPRINT:a:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:a:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:a:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:a:LAST:"LAST\:%5.2lf %S\n"                                   \
               AREA:b#009959:"ROOM Temperature"                                    \
               GPRINT:b:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:b:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:b:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:b:LAST:"LAST\:%5.2lf %S\n"                                   \
               LINE2:a#00663B:                                                     \
               LINE2:b#009959:                                                     \
               LINE1:c#C90929:"ROOM Humidity   "                                   \
               GPRINT:c:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:c:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:c:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:c:LAST:"LAST\:%5.2lf %S\n"

rrdtool graph   /home/cutxout/public_html/usbrh/usbrh-week.png                     \
               --imgformat=PNG                                                     \
               --start=-605100                                                     \
               --end=-300                                                          \
               --base=1000                                                         \
               --height=200                                                        \
               --width=600                                                         \
               --title="Temperature and Humidity (Daily)"                          \
               --vertical-label="Celsius / Percent"                                \
               DEF:a="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_CPU:AVERAGE         \
               DEF:b="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_ROOM:AVERAGE        \
               DEF:c="/home/cutxout/public_html/usbrh/usbrh.rrd":HUMIDITY_ROOM:AVERAGE           \
               AREA:a#00663B:"CPU  Temperature"                                    \
               GPRINT:a:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:a:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:a:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:a:LAST:"LAST\:%5.2lf %S\n"                                   \
               AREA:b#009959:"ROOM Temperature"                                    \
               GPRINT:b:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:b:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:b:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:b:LAST:"LAST\:%5.2lf %S\n"                                   \
               LINE2:a#00663B:                                                     \
               LINE2:b#009959:                                                     \
               LINE1:c#C90929:"ROOM Humidity   "                                   \
               GPRINT:c:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:c:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:c:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:c:LAST:"LAST\:%5.2lf %S\n"

rrdtool graph   /home/cutxout/public_html/usbrh/usbrh-month.png                    \
               --imgformat=PNG                                                     \
               --start=-3629100                                                    \
               --end=-300                                                          \
               --base=1000                                                         \
               --height=200                                                        \
               --width=600                                                         \
               --title="Temperature and Humidity (Daily)"                          \
               --vertical-label="Celsius / Percent"                                \
               DEF:a="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_CPU:AVERAGE         \
               DEF:b="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_ROOM:AVERAGE        \
               DEF:c="/home/cutxout/public_html/usbrh/usbrh.rrd":HUMIDITY_ROOM:AVERAGE           \
               AREA:a#00663B:"CPU  Temperature"                                    \
               GPRINT:a:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:a:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:a:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:a:LAST:"LAST\:%5.2lf %S\n"                                   \
               AREA:b#009959:"ROOM Temperature"                                    \
               GPRINT:b:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:b:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:b:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:b:LAST:"LAST\:%5.2lf %S\n"                                   \
               LINE2:a#00663B:                                                     \
               LINE2:b#009959:                                                     \
               LINE1:c#C90929:"ROOM Humidity   "                                   \
               GPRINT:c:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:c:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:c:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:c:LAST:"LAST\:%5.2lf %S\n"

rrdtool graph   /home/cutxout/public_html/usbrh/usbrh-year.png                     \
               --imgformat=PNG                                                     \
               --start=-31536000                                                   \
               --end=-300                                                          \
               --base=1000                                                         \
               --height=200                                                        \
               --width=600                                                         \
               --title="Temperature and Humidity (Daily)"                          \
               --vertical-label="Celsius / Percent"                                \
               DEF:a="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_CPU:AVERAGE         \
               DEF:b="/home/cutxout/public_html/usbrh/usbrh.rrd":TEMPERATURE_ROOM:AVERAGE        \
               DEF:c="/home/cutxout/public_html/usbrh/usbrh.rrd":HUMIDITY_ROOM:AVERAGE           \
               AREA:a#00663B:"CPU  Temperature"                                    \
               GPRINT:a:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:a:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:a:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:a:LAST:"LAST\:%5.2lf %S\n"                                   \
               AREA:b#009959:"ROOM Temperature"                                    \
               GPRINT:b:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:b:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:b:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:b:LAST:"LAST\:%5.2lf %S\n"                                   \
               LINE2:a#00663B:                                                     \
               LINE2:b#009959:                                                     \
               LINE1:c#C90929:"ROOM Humidity   "                                   \
               GPRINT:c:MAX:"MAX\:%5.2lf %S"                                       \
               GPRINT:c:MIN:"MIN\:%5.2lf %S"                                       \
               GPRINT:c:AVERAGE:"AVERAGE\:%5.2lf %S"                               \
               GPRINT:c:LAST:"LAST\:%5.2lf %S\n"

crontab -e

*/5 * * * * /home/cutxout/bin/usbrh_rrd.sh

結果

f:id:cutxout:20190728193619p:plain

uptime
rp3% snmpwalk -v2c -c nvr510.snmp 192.168.100.1 1.3.6.1.2.1.1.3.0 | awk '{ print $5 }'                    
35