継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

Mackerel の公式プラグインを使ってメトリック監視を行う

タダです.

Mackerel でメトリック監視を実装するにあたって Mackerel 公式プラグインを検証する機会があったのでこの記事でその内容をまとめていきます.

mackerel.io

github.com

Mackerel の導入環境

Mackerel の導入環境として Amazon Linux2 に入れました.公式プラグインをインストールします.

$ sudo yum install mackerel-agent-plugins

mackerel-agent.conf の中身を見てみると,いろんなプラグインを使うことができる状況です.

$ cat /etc/mackerel-agent/mackerel-agent.conf
# pidfile = "/var/run/mackerel-agent.pid"
# root = "/var/lib/mackerel-agent"
# verbose = false
# apikey = ""

# [host_status]
# on_start = "working"
# on_stop  = "poweroff"

# [filesystems]
# ignore = "/dev/ram.*"

# Configuration for Custom Metrics Plugins
# see also: https://mackerel.io/ja/docs/entry/advanced/custom-metrics

# followings are mackerel-agent-plugins https://github.com/mackerelio/mackerel-agent-plugins

# Plugin for Apache2 mod_status
#   By default, the plugin accesses to http://127.0.0.1/server-status?auto
# [plugin.metrics.apache2]
# command = "mackerel-plugin-apache2"

# Plugin for EC2 CPU Credit
# [plugin.metrics.aws-ec2_cpucredit]
# command = "mackerel-plugin-aws-ec2-cpucredit"

# Plugin for AWS ELB
# [plugin.metrics.aws-elb]
# command = "mackerel-plugin-aws-elb"

# Plugin for Amazon RDS
# [plugin.metrics.aws-rds]
# command = "mackerel-plugin-aws-rds -identifier=<required>"

# Plugin for Elasticsearch
#   By default, the plugin accesses Elasticsearch on localhost.
# [plugin.metrics.elasticsearch]
# command = "mackerel-plugin-elasticsearch"

# Plugin for HAProxy
#   By default, the plugin accesses Elasticsearch on localhost.
# [plugin.metrics.haproxy]
# command = "mackerel-plugin-haproxy"

# Plugin for JVM
#   Required javaname
# [plugin.metrics.jvm]
# command = "mackerel-plugin-jvm -javaname=<required>"

# Plugin for Linux
# [plugin.metrics.linux]
# command = "mackerel-plugin-linux"

# Plugin for Memcached
# [plugin.metrics.memcached]
# command = "mackerel-plugin-memcached"

# Plugin for MongoDB
# [plugin.metrics.mongodb]
# command = "mackerel-plugin-mongodb"

# Plugin for MySQL
#   Appropriate previlege settings required.
#   By default, the plugin accesses MySQL on localhost by 'root' with no password.
# [plugin.metrics.mysql]
# command = "mackerel-plugin-mysql"

# Plugin for Nginx
#   By default, the plugin accesses to http://localhost:8080/nginx_status
# [plugin.metrics.nginx]
# command = "mackerel-plugin-nginx"

# Plugin for PHP APC
# [plugin.metrics.php-apc]
# command = "mackerel-plugin-php-apc"

# Plugin for Plack
#   By default, the plugin accesses to http://localhost:5000/server-status?json
# [plugin.metrics.plack]
# command = "mackerel-plugin-plack"

# Plugin for PostgreSQL
#   Appropriate previlege settings required.
#   By default, the plugin accesses PostgreSQL on localhost.
# [plugin.metrics.postgres]
# command = "mackerel-plugin-postgres"

# Plugin for Redis
#   By default, the plugin accesses Redis on localhost.
#   Currently AUTH password has not been supported yet.
# [plugin.metrics.redis]
# command = "mackerel-plugin-redis"

# Plugin for SNMP
# [plugin.metrics.pps]
# command = "mackerel-plugin-snmp -name='pps' -community='private' '.1.3.6.1.2.1.31.1.1.1.7.2:eth01in:1:0' '.1.3.6.1.2.1.31.1.1.1.11.2:eth01out:1:0'"

# Plugin for Squid
# [plugin.metrics.squid]
# command = "mackerel-plugin-squid"

# Plugin for Varnish
# [plugin.metrics.varnish]
# command = "mackerel-plugin-varnish"

# Plugin for munin (wrapper)
# [plugin.metrics.nfsd]
# command = "mackerel-plugin-munin -plugin=/usr/share/munin/plugins/nfsd"
# [plugin.metrics.bind9]
# command = "mackerel-plugin-munin -plugin=/etc/munin/plugins/bind9 -plugin-conf-d=/etc/munin/plugin-conf.d"
# [plugin.metrics.postfix]
# command = "MUNIN_LIBDIR=/usr/share/munin mackerel-plugin-munin -plugin=/usr/share/munin/plugins/postfix_mailqueue -name=postfix.mailqueue"

# followings are other samples
# [plugin.metrics.vmstat]
# command = "ruby /etc/sensu/plugins/system/vmstat-metrics.rb"
# [plugin.metrics.curl]
# command = "ruby /etc/sensu/plugins/http/metrics-curl.rb"

プロセス監視の実装

今回は,Nginx のプロセスチェックを設定してみます.Nginx はコメントを外すだけで完了します.

# Plugin for Nginx
#   By default, the plugin accesses to http://localhost:8080/nginx_status
[plugin.metrics.nginx]
command = "mackerel-plugin-nginx"

Nginx のプラグインを使うために /etc/nginx/conf.d/default.conf を作成して以下の定義を追加しておく必要があります.併せてこのファイルを読むこむように Config を変更します.

server {
    listen 8080;
    server_name localhost;
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

Nginx の設定ファイルを読み込ませます.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo nginx -s reload

そして,mackerel-agent を再起動します.

$ sudo systemctl restart mackerel-agent

実装後のチェック

まず,Nginx のプラグインが機能しているとコマンドを打つと,各監視パラメーターのステータスを表示してくれます.また,Mackerel の監視画面上でもメトリックとして確認できます.この状態であればホストメトリック監視でプロセス数をアラートに設定していけます.

$ mackerel-plugin-nginx
nginx.connections.connections   1.000000  1603121705
2020/10/19 15:35:05 OutputValues:  Too long duration
2020/10/19 15:35:05 OutputValues:  Too long duration
2020/10/19 15:35:05 OutputValues:  Too long duration
nginx.queue.reading 0.000000  1603121705
nginx.queue.writing 1.000000  1603121705
nginx.queue.waiting 0.000000  1603121705
  • nginx.connections.connections
  • nginx.queue.reading
  • nginx.queue.waiting
  • nginx.queue.writing
  • nginx.requests.accepts
  • nginx.requests.handled
  • nginx.requests.requests

f:id:sadayoshi_tada:20201020003622p:plain

まとめ

Mackerel の公式プラグインを使って Nginx のメトリック監視を行ってみました.他にも監視できる物が既に揃っているので適宜使い分けて対応していきたいと思います.

20201020 追記

a-know さんより教えていただき,本記事の内容はホスト監視ではなくメトリック監視の検証記事に当たるので記事の内容を修正しました.本記事では扱えていないのですが,プロセス監視には check-procsを活用するのが良いです.実装ではメトリック監視とプロセス監視両軸でいきたいと思います! a-know さん,コメントいただきありがとうございます🙇‍♂️

関連記事

sadayoshi-tada.hatenablog.com