LoginSignup
6
11

More than 5 years have passed since last update.

Mackerel Agent に MySQL, Nginx, Apache, PHP-FPM & Uptime の監視項目を加えてみる

Last updated at Posted at 2017-06-22

CentOS での記述です。
Mackerel は CentOS7 /CentOS6 にインストール済み。

参考:エージェントをインストールする
https://mackerel.io/ja/docs/entry/howto/install-agent

Mackere は uptime などのチェック機能をプラグインとして別にしているみたいです。
いろいろチェックできる項目が増えているみたいなので、設定してみました。

Mackerel Plugin をインストール

Yum でインストール

$ sudo yum install mackerel-agent-plugins

Uptime を設定してみる

Uptime 情報を送る

$ sudo vi /etc/mackerel-agent/mackerel-agent.conf

Mackerel の conf に 設定し Uptime 情報を送れるるようにする。

/etc/mackerel-agent/mackerel-agent.conf
[plugin.checks.uptime]
command = "/usr/bin/mackerel-plugin-uptime"

MySQL Metrics を送る

$ sudo vi /etc/mackerel-agent/mackerel-agent.conf

Mackerel の conf に MySQL の root ユーザーを入れる。

/etc/mackerel-agent/mackerel-agent.conf
[plugin.metrics.mysql]
command = "/usr/bin/mackerel-plugin-mysql -host=127.0.0.1 -port=3306 -username=root -password=PASSWORD"

Apache2 Status を送る

Apache の Mod Status が必要。
httpd.conf などの conf ファイルに、下記の記述が入っていればインストール済み。

/etc/httpd/httpd.conf
LoadModule status_module modules/mod_status.so

Mod Status が入っていなかったらインストールしましょう。
そして、httpd.conf に下記を追加。

/etc/httpd/httpd.conf
ExtendedStatus On
<VirtualHost 127.0.0.1:1080>
    <Location /server-status>
        SetHandler server-status
        Order deny,allow
        Deny from all
        Allow from localhost
    </Location>
</VirtualHost>

保存して、Apache を再起動。

$ sudo apachectl configtest
$ sudo apachectl restart

Mackerel の Config に Apache の設定を追加。

$ sudo vi /etc/mackerel-agent/mackerel-agent.conf
/etc/mackerel-agent/mackerel-agent.conf
[plugin.metrics.apache2]
command = "/usr/bin/mackerel-plugin-apache2 -p 1080"
type = "metric"

Nginx のステータスチェック

Nginx で stub_status が使えることを確認。
使えなかったら使えるように Nginx を再インストール。

Nginx の conf の編集が必要です。(ファイル保存場所・名前はサーバーによって変わります。)

$ sudo vi /etc/nginx/conf.d/default.conf

Nginx の config に下記を加える

/etc/nginx/conf.d/default.conf
server {
    listen 8080;
    server_name localhost;
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

Conf のテストをし、OKだったら再起動。

$ sudo nginx -t
$ sudo nginx -s reload

まず、そもそも Nginx の stub status が動いているかどうかテスト

$ curl http://localhost:8080/nginx_status

下記のような結果が出てきたら成功

Active connections: 6 
server accepts handled requests
 3508 3508 6026 
Reading: 0 Writing: 2 Waiting: 4 

Mackerel に実装

sudo vi /etc/mackerel-agent/mackerel-agent.conf

デフォルトの config をそのままコメントアウト

/etc/mackerel-agent/mackerel-agent.conf
[plugin.metrics.nginx]
command = "/usr/bin/mackerel-plugin-nginx"

PHP-FPM のステータスチェック

PHP-FPM の設定

PHP-FPM のステータスチェックを有効にするため config を書き換え。

sudo vi /etc/php-fpm.d/www.conf

下記の1文のコメントアウトを解除し有効に。

/etc/php-fpm.d/www.conf
pm.status_path = /status
#CentOS6
sudo /etc/init.d/php-fpm configtest
sudo /etc/init.d/php-fpm restart

Nginx での設定

Nginx で php-fpm をしている場合は下記のように、Nginx の conf ファイルに追加。(ファイル保存場所・名前はサーバーによって変わります。)

sudo vi /etc/nginx/conf.d/default.conf

前述の Nginx のステータスチェックを追加した箇所に php-fpm モニターをするための設定を追加します。

/etc/nginx/conf.d/default.conf
server {
    listen 8080;
    server_name localhost;
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }

    location /status {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffers 256 128k;
        fastcgi_buffer_size 128k;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 120s;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

保存して Nginx を適用

sudo nginx -t
sudo nginx -s reload

Apache での設定

Apache で php-fpm を設定している場合は、このようにします。

ExtendedStatus On
<VirtualHost 127.0.0.1:1080>
    <Location /server-status>
        SetHandler server-status
        Order deny,allow
        Deny from all
        Allow from localhost
    </Location>
  <Location /status>
    SetHandler "proxy:unix:/var/run/php-fpm/www.sock|fcgi://localhost/"
    Order deny,allow
    Deny from all
    Allow from localhost
  </Location>
</VirtualHost>

保存して Apache を再起動

sudo nginx -t
sudo nginx -s reload

Mackerel Conf 書き換え

Mackerel の conf を追加

sudo vi /etc/mackerel-agent/mackerel-agent.conf

下記のコマンドを追加。Plugin for PHP APCPlugin for Plackの間ぐらいに配置

/etc/mackerel-agent/mackerel-agent.conf
# Plugin for php-fpm
[plugin.metrics.php-fpm]
command = "/usr/bin/mackerel-plugin-php-fpm -url http://localhost:8080/status?json"

Mackerel Agent 再起動

以上の設定が完了したら、Mackerel Agent を再起動。

# CentOS6
sudo /etc/init.d/mackerel-agent restart
# CentOS7
sudo systemctl restart mackerel-agent

あとは Mackerel の管理画面でモニター設定をしましょう。

参考

https://mackerel.io/ja/docs/entry/howto/mackerel-agent-plugins
http://ariarijp.hatenablog.com/entry/2016/04/25/080000

クレジット

この記事が役に立ったら、「 CMS は concrete5 が一番」と頭の中で10回唱えてください。

6
11
1

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
6
11