LoginSignup
43

More than 3 years have passed since last update.

Raspberry Pi を Mackerel で監視する

Last updated at Posted at 2016-12-03

最近 Raspberry Pi をよく使うので監視が欲しくなり、手軽に導入できる Mackerel を採用しました。

Raspberry Pi でなくても Linux + ARM のコンピュータなら、この方法で導入出来ると思います。

※ Raspberry Pi Zero, Raspberry Pi 3 で動作確認済み。

インストール

GitHub で release されているバイナリを配置します。
https://github.com/mackerelio/mackerel-agent/releases

ダウンロードと展開

最新版の場合
curl -sL https://github.com/mackerelio/mackerel-agent/releases/latest/download/mackerel-agent_linux_arm.tar.gz | tar xz

バイナリと設定ファイルの配置

sudo mkdir -p /usr/local/bin /etc/mackerel-agent
sudo cp mackerel-agent_linux_arm/mackerel-agent /usr/local/bin
sudo cp mackerel-agent_linux_arm/mackerel-agent.conf /etc/mackerel-agent

API Key の設定

API Key は オーガニゼーションのページ に記載されています。

sudo mackerel-agent init -apikey="YOUR API KEY"

Service として登録

そのまま起動しても良いのですが、最新の Raspbian なら systemd が使えるので service として登録しましょう。

/lib/systemd/system/mackerel-agent.service
[Unit]
Description=mackerel-agent
After=network.target network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mackerel-agent
ExecReload=/bin/kill -HUP $MAINPID
KillMode=control-group
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=mackerel-agent.service
自動起動の有効化と起動
sudo systemctl enable mackerel-agent
sudo systemctl start mackerel-agent

カスタムメトリックの追加

Raspberry Pi が熱暴走しないか監視したいので、 CPU 温度をカスタムメトリックとして投稿できるようにします。

CPU 温度を取得する shell script

実行するために bc が必要なのでインストールしておきます。

sudo apt install bc
temp.sh
#!/bin/bash
# ref: https://github.com/ymyzk/mackerel-plugins/blob/master/raspberrypi/temp.sh
SECONDS=`date '+%s'`

NAME='temperature.cpu'
TEMP=`cat /sys/class/thermal/thermal_zone0/temp`
VALUE=`echo "scale=3; ${TEMP} / 1000" | bc`
echo -e "${NAME}\t${VALUE}\t${SECONDS}"

設定ファイルに追記

/etc/mackerel-agent/mackerel-agent.conf
[plugin.metrics.temp]
command = "/path/to/temp.sh"

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43