4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ラズパイにmackerelを導入

Last updated at Posted at 2018-12-29

#環境

  • Raspberry Pi 3 Model B
  • OSバージョン
$ uname -a
Linux raspberrypi 4.14.79-v7  #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux
$ grep -H "" /etc/*version ; grep -H "" /etc/*release
/etc/debian_version:9.6
/etc/os-release:PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
/etc/os-release:NAME="Raspbian GNU/Linux"
/etc/os-release:VERSION_ID="9"
/etc/os-release:VERSION="9 (stretch)"
/etc/os-release:ID=raspbian
/etc/os-release:ID_LIKE=debian
/etc/os-release:HOME_URL="http://www.raspbian.org/"
/etc/os-release:SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
/etc/os-release:BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

#golangのインストール
mackerel-agentはバイナリで用意されているが、今回は、ソースからコンパイルしてインストールする。
ソースからコンパイルしてインストールする為には、golangが必要になります。
aptでインストールするとバージョンが古いので最新版をDLしてインストールする。

$ curl -O https://dl.google.com/go/go1.11.4.linux-armv6l.tar.gz
$ tar -C /usr/local -xzf go1.11.4.linux-armv6l.tar.gz

/usr/local/go/bin をPATH環境変数に追加する

/etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games"
fi
export PATH=$PATH:/usr/local/go/bin # Add /usr/local/go/bin

〜省略〜

#mackerel-agentインストール

$ go get -d github.com/mackerelio/mackerel-agent
$ cd ~/go/src/github.com/mackerelio/mackerel-agent
$ make build
$ sudo cp ./build/mackerel-agent /usr/local/bin/
$ sudo mkdir -p /etc/mackerel-agent/
$ sudo cp ./mackerel-agent.sample.conf /etc/mackerel-agent/mackerel-agent.conf
$ sudo cp ./packaging/deb-systemd/debian/mackerel-agent.default /etc/default/mackerel-agent
$ sudo cp ./packaging/deb-systemd/debian/mackerel-agent.service /lib/systemd/system/mackerel-agent.service

APIキーを設定する

$ sudo mackerel-agent init -apikey="xxxx"

/lib/systemd/system/mackerel-agent.service を mackerel-agent のインストールパスに合わせて編集

mackerel-agent.service
[Unit]
Description=mackerel.io agent
Documentation=https://mackerel.io/
After=network-online.target nss-lookup.target

[Service]
Environment=MACKEREL_PLUGIN_WORKDIR=/var/tmp/mackerel-agent
Environment=ROOT=/var/lib/mackerel-agent
EnvironmentFile=-/etc/default/mackerel-agent
ExecStartPre=/bin/mkdir -m 777 -p $MACKEREL_PLUGIN_WORKDIR
ExecStart=/usr/local/bin/mackerel-agent supervise --root $ROOT $OTHER_OPTS
ExecStopPost=/bin/sh -c '[ "$AUTO_RETIREMENT" = "" ] || [ "$AUTO_RETIREMENT" = "0" ] && true || /usr/local/bin/mackerel-agent retire -force --root $ROOT $OTHER_OPTS'
ExecReload=/bin/kill -HUP $MAINPID
LimitNOFILE=65536
LimitNPROC=65536

[Install]
WantedBy=multi-user.target

サービスとして登録

$ sudo systemctl enable mackerel-agent.service
Created symlink /etc/systemd/system/multi-user.target.wants/mackerel-agent.service → /lib/systemd/system/mackerel-agent.service.

mackerel-agentの起動

$ sudo systemctl start mackerel-agent.service

動作しているか確認

$ sudo systemctl status mackerel-agent.service
● mackerel-agent.service - mackerel.io agent
  Loaded: loaded (/lib/systemd/system/mackerel-agent.service; enabled; vendor preset: enabled)
  Active: active (running) since Sat 2018-12-29 23:44:24 JST; 5s ago
    Docs: https://mackerel.io/
 Process: 1779 ExecStartPre=/bin/mkdir -m 777 -p $MACKEREL_PLUGIN_WORKDIR (code=exited, status=0/SUCCESS)
Main PID: 1782 (mackerel-agent)
     CPU: 99ms
  CGroup: /system.slice/mackerel-agent.service
          ├─1782 /usr/local/bin/mackerel-agent supervise --root /var/lib/mackerel-agent
          └─1794 /usr/local/bin/mackerel-agent --root /var/lib/mackerel-agent -child

12月 29 23:44:24 raspberrypi systemd[1]: Starting mackerel.io agent...
12月 29 23:44:24 raspberrypi systemd[1]: Started mackerel.io agent.
12月 29 23:44:24 raspberrypi mackerel-agent[1782]: 2018/12/29 23:44:24 INFO <main> Starting mackerel-agent version:0.58.2, rev:b5bee6b, apibase:https://api.mackerelio.com

ログの確認

$ sudo journalctl -u mackerel-agent.service
12月 29 23:44:24 raspberrypi systemd[1]: Starting mackerel.io agent...
12月 29 23:44:24 raspberrypi systemd[1]: Started mackerel.io agent.
12月 29 23:44:24 raspberrypi mackerel-agent[1782]: 2018/12/29 23:44:24 INFO <main> Starting mackerel-agent version:0.58.2, rev:b5bee6b, apibase:https://api.mackerelio.com
12月 29 23:44:34 raspberrypi mackerel-agent[1782]: 2018/12/29 23:44:34 INFO <command> Start: apibase = https://api.mackerelio.com, hostName = raspberrypi, hostID = xxxx

#CLIツールmkrのインストール

$ go get github.com/mackerelio/mkr
$ sudo cp ~/go/bin/mkr /usr/local/bin/

#mackerel公式プラグイン集のインストール
簡単にミドルウェアのメトリックを収集することができる mackerel-agent-plugins をインストールする

$ go get github.com/mackerelio/mackerel-agent-plugins
$ sudo cp ~/go/bin/mackerel-agent-plugins /usr/local/bin/

conntrack,inode,linux,multicore,uptime カスタムメトリックを追加する為に /etc/mackerel-agent/mackerel-agent.conf を編集する

mackerel-agent.conf
[plugin.metrics.conntrack]
command = ["mackerel-agent-plugins", "conntrack"]

[plugin.metrics.inode]
command = ["mackerel-agent-plugins", "inode"]

[plugin.metrics.linux]
command = ["mackerel-agent-plugins", "linux"]

[plugin.metrics.multicore]
command = ["mackerel-agent-plugins", "multicore"]

[plugin.metrics.uptime]
command = ["mackerel-agent-plugins", "uptime"]

mackerel-agentの再起動して設定を反映する

$ sudo systemctl restart mackerel-agent.service

#mackerel公式チェックプラグイン集のインストール
プロセス監視やログ監視などのチェック監視を簡単に実現することができるgo-check-pluginsをインストールする

$ go get github.com/mackerelio/go-check-plugins
$ sudo cp ~/go/bin/go-check-plugins /usr/local/bin/

ディスクの空き容量(10%未満でWARNING,5%未満でCRITICAL)をチェックする為に /etc/mackerel-agent/mackerel-agent.conf を編集する

mackerel-agent.conf
[plugin.checks.disk]
command = ["go-check-plugins", "disk", "-w", "10%", "-c", "5%", "-p", "/"]

mackerel-agentの再起動して設定を反映する

$ sudo systemctl restart mackerel-agent.service
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?