Matthewの備忘録

忘れたときはここを見ろ。何か書いてある。

昆布乾燥室温湿度モニタリングシステム その3

 温湿度が計測できることは確認できたので、情報を保存し、その情報を可視化し、家庭内ネットワーク内の他の機器・端末からグラフの閲覧を可能にすることにした。利用するのは資源監視ソフトウェアのMUNINである。MUNINについては検索すれば非常に詳しく、日本語でも、詳細に解説があるので、ここでは備忘録的な設定方法だけを書くことに努めた。また、MUNINのグラフを閲覧するにはウェブサーバーが必要であり、メモリとディスクが小さく限られ、軽量動作で発熱の低減を期待したい環境で利用したいために、nginxというウェブサーバーを利用することにした。これもまた詳細な情報がネット上に溢れているので設定方法だけを書くことに努めた。

必要パッケージインストール

 Raspbianのパッケージでは、MUNINは監視サーバーのmuninパッケージ、そして監視対象ノードから情報を発信するmunin-nodeパッケージに分かれている。今回は情報を発信するのと監視するのを同一のRaspberry Pi自身に行わせるので、どちらもインストールした。また、debパッケージシステムは依存情報による親切な設定を行ってくれるものがあり、確認してはいないが、nginxなどのウェブサーバーがmuninパッケージより先にインストールされていると、幾つかの設定をmuninパッケージをインストールしたときに自動的に行ってくれるというので、nginx、munin、munin-nodeの順でインストールした。

$ sudo apt-get install nginx munin munin-node

温度取得コマンドと湿度取得コマンド

 nginxのリクエストやセッション数などを監視するためのnginxとMUNIN設定を解説しているページを散見するが、今回は監視するまでもないので設定はしないし、書き残すことはしなかった。*1最初から温湿度センサーDHT11からの温湿度情報をグラフ化した画像をRaspberry Pi以外からの機器で閲覧可能にすることにした。最初に行うことは、温度と湿度それぞれの情報を分離することである。つまりその2で実行した"AdafruitDHT.py"を温度計測用と湿度計測用に分離する。自分で後に説明するプラグインなどを作成するのであれば、分離は必要ないのだが、今回は手っ取り早く目的を達成するために、参考サイトの記述に沿った。
 参考にした『温湿度センサDHT11をRaspberryPiに接続しグラフ表示』で示されていた分離されたPythonプログラムのソースをそのまま頂くことにした。改造点だけを掲載しておく。*2まずは温度用。

$ cp AdafruitDHT.py temp.py # スクリプト名は何でもよいが、分かりやすい方がよいだろう
$ vi temp.py
# 省略
if humidity is not None and temperature is not None: # このif文もtemperatureだけをみればよいだろう
    print '{}'.format(temperature) 
# 省略

実行して動作確認。

pi@raspberrypi:examples $ temp.py 11 4
25.0

続いて湿度用。

$ cp AdafruitDHT.py humid.py # スクリプト名は何でもよいが、分かりやすい方がよいだろう
$ vi humid.py
# 省略
if humidity is not None and temperature is not None: # このif文もhumidityだけをみればよいだろう
    print '{}'.format(humidity) 
# 省略

実行して動作確認。

pi@raspberrypi:examples $ humid.py 11 4
47.0

Raspbianで提供されるパッケージシステムとは関係がない自作実行形式ファイル(コマンド)であるのと、パスが予め通してある/usr/local/binに上記のスクリプトをコピーした。これで/usr/local/binにアクセスできるユーザーは温度と湿度を取得できる。

$ sudo cp temp.py /usr/local/bin
$ sudo cp humid.py /usr/local/bin

MUNIN監視対象プラグインの作成

 munin-nodeはユーザー独自のプラグインを作成して監視対象を増やすことができる。これもまた『温湿度センサDHT11をRaspberryPiに接続しグラフ表示』に掲載されているソースをそのまま使うことにした。若干の間違いがあるので、修正版を貼り付けておく。プラグインは/usr/share/munin/pluginsにあり、独自のプラグインもそこに設置する。尚、プラグイン名が重ならないかどうか確認すべきである。まずは温度用。

$ sudo vi /usr/share/munin/plugins/temp
#!/bin/sh

#%# family=auto
#%# capabilities=autoconf

GETNUM=`python /usr/local/bin/temp.py 11 4`

if [ "$1" = "autoconf" ]; then
        if [ -n ${GETNUM} ] ; then
                echo yes
                exit 0
        else
                echo no
                exit 0
        fi
fi

if [ "$1" = "config" ]; then
        echo 'graph_title temperature'
        echo 'graph_args -r --lower-limit 0'
        echo 'graph_vlabel C'
        echo 'graph_category Weather'
        echo 'total.label temperature'
        echo 'total.min 0'
        echo 'total.draw LINE2'
        echo 'total.type GAUGE'
        exit 0
fi

echo "total.value $GETNUM";

実行して動作確認。

pi@raspberrypi:examples $ /usr/share/munin/plugins/temp autoconf
yes
pi@raspberrypi:examples $ /usr/share/munin/plugins/temp config
graph_title temperature
graph_args -r --lower-limit 0
graph_vlabel C
graph_category Weather
total.label temperature
total.min 0
total.draw LINE2
total.type GAUGE
pi@raspberrypi:examples $ 
pi@raspberrypi:examples $ /usr/share/munin/plugins/temp 
total.value 26.0

続いて湿度。

$ sudo vi /usr/share/munin/plugins/humid
#!/bin/sh

#%# family=auto
#%# capabilities=autoconf

GETNUM=`python /usr/local/bin/humid.py 11 4`

if [ "$1" = "autoconf" ]; then
        if [ -n ${GETNUM} ] ; then
                echo yes
                exit 0
        else
                echo no
                exit 0
        fi
fi

if [ "$1" = "config" ]; then
        echo 'graph_title humidity'
        echo 'graph_args -r --lower-limit 0'
        echo 'graph_vlabel %'
        echo 'graph_category Weather'
        echo 'total.label humidity'
        echo 'total.min 0'
        echo 'total.draw LINE2'
        echo 'total.type GAUGE'
        exit 0
fi

echo "total.value $GETNUM";

実行して動作確認。

pi@raspberrypi:examples $ /usr/share/munin/plugins/humid autoconf
yes
pi@raspberrypi:examples $ /usr/share/munin/plugins/humid config
graph_title humidity
graph_args -r --lower-limit 0
graph_vlabel %
graph_category Weather
total.label humidity
total.min 0
total.draw LINE2
total.type GAUGE
pi@raspberrypi:examples $ /usr/share/munin/plugins/humid 
total.value 47.0
pi@raspberrypi:examples $ 

カテゴリー名やラベルなどは日本語対応したいところであるが、カテゴリー(graph_category)は英数字のみで、上記のスクリプトではキャピタライズしてあるが全て小文字で表示されるので、ひょっとするとcase insensitiveかもしれない。日本語対応は次の機会に挑戦することにした。
参考:
graph_category – Munin

MUNIN監視対象プラグインを使えるようにしておく

 ディレクトリ/usr/share/munin/plugins/の中にあるプラグインを使いたいときには、ディレクトリ/etc/munin/plugins/にシンボリックリンクを張る。使わないときはシンボリックリンクを削除すればよい(/etc/munin/plugins/のリンクを削除)。

$ sudo ln -s /usr/share/munin/plugins/temp /etc/munin/plugins/temp
$ sudo ln -s /usr/share/munin/plugins/humid /etc/munin/plugins/humid

それぞれプラグイン固有の設定を書くことができる。今回の場合、rootユーザーで実行しなければならないので、ディレクトリ/etc/munin/plugins-conf.d/にそれぞれの設定を書いておく。
温度用。

$ sudo vi /etc/munin/plugin-conf.d/temp
[temp]
user root

湿度用。

$ sudo vi /etc/munin/plugin-conf.d/humid
[humid]
user root

 ひょっとすると、/etc/munin/plugin-conf.d/munin-node内で一括して書けるかもしれないが、試さなかった。
 ここまできたら、監視対象ノードでmunin-nodeを立ち上げる。インストール時の設定によっては既にmunin-nodeが立ち上がっているかもしれない。チェックするには色々あるが、chkconfigを使った。

$ sudo apt-get install chkconfig

$ chkconfig -l | grep munin
munin                     0:off  1:off  2:on   3:on   4:on   5:on   6:off
munin-node                0:off  1:off  2:on   3:on   4:on   5:on   6:off
$ 

上の例のように、RaspbianのLiteではなくXモードが立ち上がる版では2-5がonになっていれば立ち上がっている。立ち上がっていなければ、

$ sudo service munin-node start

とりあえず設定を読み込ませて再起動させるなら、

$ sudo service munin-node restart

すればよい。因みに監視サーバーのmuninは指定の時間間隔毎で定期的に呼び出されるcronから起動される。

MUNIN監視サーバーの設定

 nginxをインストールしたときのデフォルトのルート・ディレクトリが/var/www/html/であるので、munin監視サーバーがHTML形式ドキュメントを出力する先を変更しておいた。インストール状態・条件によってはapt-getを実行したときにすでに設定されているかもしれない。*3
 /etc/munin/munin.confのHTML出力ディレクトリ指定をRapsbianのnginx用に書き換えた。それ以外はデフォルト値でよいはずだ。

$ sudo vi /etc/munin/munin.conf
#dbdir  /var/lib/munin
##htmldir /var/cache/munin/www
htmldir /var/www/html/munin
#logdir /var/log/munin
#rundir  /var/run/munin

# Where to look for the HTML templates
#
#tmpldir        /etc/munin/templates

cronを強制的に実行して動作確認。

$ sudo su - munin --shell=/usr/bin/munin-cron & sudo tail -f /var/log/munin/munin-update.log

エラーが出なければnginxを設定するだけだ。"...Permission denied..."や"...can't create /var/lib/munin/state-*..."などのファイルのアクセスに関わるエラーがでたら/var/lib/muninディレクトリのオーナーやグループ、モードを確認するべし。今回の場合は、オーナーとグループがrootユーザーになっていた。セキュリティは後から確認するとして、muninユーザーとそのグループに変更して、エラーがでなくなった。

$ sudo chown munin.munin /var/lib/munin

nginxの設定

 ここまできたら、あとはnginxの設定をして起動するだけだ。今回はデフォルトの設定ファイルに次の設定を加えた。server設定内の最終行にから加えた。ドメイン設定やセキュリティのためのパスワードなどは今回は割愛して、設定できていれば、http://ip_address/munin/を閲覧するとMUNIN監視サーバーが収集した情報に基づいたグラフが閲覧できる。

$ sudo vi /etc/nginx/sites-available/default
        location /munin {
                alias /var/www/html/munin;
                autoindex on;
                allow 127.0.0.1;
                allow 192.168.0.0/24;
                allow ::1;
                deny all;
        }

cpu温度も監視対象にする

 室温が20-25℃で一日中放置していて、Raspberry Piをクリアケース越しに触ってみるとジンワリと暖かい気がするので、基盤かcpuの温度を測りたくなった。
 Raspbian上では次のコマンドでcpu温度を知ることができる。

pi@raspberrypi:~ $ vcgencmd measure_temp
temp=47.8'C
pi@raspberrypi:~ $ 

結構高いので、cpu温度をRaspberry Piの代表温度として監視対象にすることにした。幸いMUNIN用プラグインが公開されていたので、そのまま利用した。

$ git clone https://gist.github.com/informationsea/4148209.git sensors_temp

sensors_tempのソースをみるとsystemカテゴリー内分類してあることがわかる。また、グラフのタイトルは"CPU core temperature"である。
 温湿度のときと同じように/usr/share/munin/pluginsにプラグインを設置し、/etc/munin/pluginsにシンボリックリンクを張って、munin-nodeデーモンを再起動させればよい。

$ sudo cp gistfile1.sh /usr/share/munin/plugins/sensors_temp
$ sudo chmod 755 /usr/share/munin/plugins/sensors_temp
$ sudo ln -s /usr/share/munin/plugins/sensors_temp /etc/munin/plugins/sensors_temp

5分待つか、強制的にcronを実行して、sensors_tempのグラフ、タイトルが"CPU core temperature"のグラフが表示されていればよい。

*1:興味深いこともあるので後々別の機会に触ろうと思う。

*2:gistに置けばよいのでは?

*3:先にnginxをインストールしていれば、はじめから/var/www/html/muninがデフォルト値になっていたのだろうか?嫌、ビルド時に決定されるはずだ。