0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RaspberryPiをMackerelで監視する

Last updated at Posted at 2025-12-17

本記事は、めんどい太郎の Advent Calendar 2025 18日目の記事です。

はじめに

今回はRaspberry PiをMackerelで監視します。

環境

  • Raspberry Pi 4 Model B (8GB)
  • OS: Raspberry Pi OS Lite (64bit)

Mackerel agentのインストール

これまで、Raspberry PiでMackerelを使うには手動インストールを行う必要があったのですが、最近公式のスクリプトでインストールできるようになりました。やったね。

ということで早速やります。

まずはMackerelのダッシュボードにアクセスします。

そうしたらメニューからホストを開きます。

「ホストを新規登録する」ボタンを押します。

Debianの項目にあるコマンドをコピーします。

image.png

先ほどコピーしたコマンドをRaspberry Piで実行します。

wget -q -O - https://mackerel.io/file/script/setup-all-apt-v2.sh | MACKEREL_APIKEY='*******************' sh

これでagentのインストールは完了です。

CPU温度の取得

Raspberry PiのCPU温度をMackerelで監視できるようにします。

こちらの記事に書かれているものを使ってもいいですが、ちょっと違う方法で取得します。

なにやらcatじゃなくても温度が取れるらしいです。

なのでこちらを使います。

vcgencmd measure_temp

これです。

今回はシェルスクリプトを配置して、それを実行してもらう形でやります。

/usr/bin/mackerel-plugin-raspberrypi-cpu-tempというファイル名で配置します。

mackerel-plugin-raspberrypi-cpu-temp
#!/bin/bash

SECONDS=`date '+%s'`

NAME='temprerature.cpu'
VALUE=`vcgencmd measure_temp | grep -o -E '([0-9]+(.[0-9]+)?)'`

echo -e "${NAME}\t${VALUE}\t${SECONDS}"

実行権限を付与します。

chmod 755 /usr/bin/mackerel-plugin-raspberrypi-cpu-temp

あとはmackerel側の設定ファイルを変更します。

/etc/mackerel-agent/mackerel-agent.conf
# 以下を追記
[plugin.metrics.raspberrypi-cpu-temp]
command = ["mackerel-plugin-raspberrypi-cpu-temp"]

設定を反映させるためにmackerel-agentを再起動します。

sudo systemctl restart mackerel-agent.service

Nginxの監視

昨日Nginxを入れましたのでついでに監視もしてみましょう。

まずはNginx側の設定をします。

/etc/nginx/conf.d/status.conf
server {
    listen       8001;
    server_name  localhost 127.0.0.1;

    access_log  /var/log/nginx/etc.access.log  main;

    location = /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

設定を反映させるためにNginxを再起動します。

sudo systemctl restart nginx.service

そうしたらmackerelのプラグインを入れます。

sudo apt-get update
sudo apt-get install mackerel-agent-plugins

あとはmackerel側の設定ファイルを変更します。

/etc/mackerel-agent/mackerel-agent.conf
# 追記
[plugin.metrics.nginx]
command = ["mackerel-plugin-nginx","--port","8001","--path","/nginx_status"]

最後に、設定を反映させるためにmackerel-agentを再起動します。

sudo systemctl restart mackerel-agent.service

しばらく待つと...

image.png

出てきました!

おわりに

いい感じにMackerelでRaspberry Piを監視することができるようになりました!

CPU温度見れるのはいいですね。

それでは!

参考

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?