Contents
概要
前回までにクライアント側の fluentd のインストールと、nginx のアクセスログをパースするところまで完了しているため、今回は、fluentd サーバを構築してクライアント側の fluentd から TCP/IP 経由でログを受信する。
こうすることで、1台1台のログを個別に確認しなくても複数台から送信されたログを fluentd サーバで一元管理することが可能となります。
手順
fluentd サーバ用の端末を用意し、<source> タグで forward、<match> タグを指定の上、出力先を fluentd サーバ上の一次ファイルに出力できることを確認します。
集約されたログには既定ではクライアント側の情報がないため、クライアント側の fluentd に fluent-plugin-record-reformer というプラグインをインストールすることでホスト名を追加して fluentd サーバにログを渡すようにしています。
fluentd サーバ側の設定
fluentd サーバ側もクライアント側も fluentd のインストール方法そのものは同じであるため省略します。また、設定についてもクライアント側の動作確認で行ったものと同様であることが判ります。
タグの名前が hostname.nginx.access となっていますが、これはホスト名を付与するために利用するタグを指定しているためで問題はありません。設定が終わったら td-agent を再起動してエラーがないことを確認しておきます。
$ sudo vi /etc/td-agent/td-agent.conf
# fluentd server
<match hostname.nginx.access>
type file
path /tmp/test
</match>
※ Lubuntu 16.04 の場合、デフォルトで forward 設定になっているため、<source> タグの追加は不要です。また、複数の <source> タグで forward を指定する場合、ポート番号を変更しないとポートがバッティングして利用できないので注意が必要です。
fluentd クライアント側の設定
fluent-plugin-record-reformer のインストール
fluent-gem コマンドを利用してプラグインをインストールすることができます。fluent-gem は td-agent のパッケージ中にも含まれています。v0.82 がインストールされました。
$ dpkg -L td-agent | grep fluent-gem
/opt/td-agent/embedded/bin/fluent-gem
/opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/bin/fluent-gem
$ sudo /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-record-reformer
Fetching: fluent-plugin-record-reformer-0.8.2.gem (100%)
Successfully installed fluent-plugin-record-reformer-0.8.2
Parsing documentation for fluent-plugin-record-reformer-0.8.2
Installing ri documentation for fluent-plugin-record-reformer-0.8.2
Done installing documentation for fluent-plugin-record-reformer after 0 seconds
1 gem installed
ホスト名の付与と fluentd サーバ IP の指定
<match> タグ中に新しく <server> タグを追加して自ホスト名の付与設定とサーバ側 fluentd の IP の指定を行います。ポイントとしては <match> タグを 2 つ用意し、まず nginx.acces タグで tail したログを受け取りホスト名の付与をし、次に hostname.nginx.access タグで指定したサーバに forward するという流れを意識できるとよいと思われます。
$ sudo vi /etc/td-agent/td-agent.conf
# nginx access.log
<source>
type tail
tag nginx.access
path /var/log/nginx/access.log
pos_file /var/log/td-agent/nginx-access.log.pos
format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<forwarder>[^\"]*)")?/
time_format %d/%b/%Y:%H:%M:%S %z
</source>
<match nginx.access>
type record_reformer
tag hostname.nginx.access
<record>
hostname ${hostname}
</record>
</match>
<match hostname.nginx.access>
type forward
<server>
host 10.1.2.56
port 24224
</server>
</match>
$ sudo /etc/init.d/td-agent restart
動作確認
fluentd サーバ側で受信したログを確認します。参考までにホスト名なしの場合とありの場合での違いが判るように両方試してみると良いかと思います。ホスト名の情報が hostname という key で追加されていることが確認できると思います。
ホスト名なし
2016-10-29T22:29:46+09:00 nginx.access {“remote”:”111.222.333.444″,”host”:”-“,”user”:”-“,”method”:”GET”,”path”:”/static/css/bootstrap-responsive.min.css”,”code”:”301″,”size”:”194″}
ホスト名あり
2016-10-29T22:39:46+09:00 hostname.nginx.access {“remote”:”111.222.333.444″,”host”:”-“,”user”:”-“,”method”:”GET”,”path”:”/oss/fluentd/3089/?relatedposts=1″,”code”:”200″,”size”:”2166″,”hostname”:”vm-nfj-206″}
トラブルシューティング
ポートが開いていないなどの理由で拒否されている
クライアント側に以下のようなログが出力されます。
$ sudo less /var/log/td-agent/td-agent.log
2016-10-29 22:00:34 +0900 [info]: following tail of /var/log/nginx/access.log
2016-10-29 22:01:35 +0900 [warn]: temporarily failed to flush the buffer. next_retry=2016-10-29 22:01:36 +0900 error_class="Errno::ECONNREFUSED" error="Connection refused - connect(2) for \"10.1.2.56\" port 24224" plugin_id="object:3ff2c7c438cc"
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:378:in `initialize'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:378:in `new'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:378:in `connect'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:299:in `send_data'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:210:in `block in write_objects'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:204:in `times'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/plugin/out_forward.rb:204:in `write_objects'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/output.rb:480:in `write'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/buffer.rb:354:in `write_chunk'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/buffer.rb:333:in `pop'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/output.rb:338:in `try_flush'
2016-10-29 22:01:35 +0900 [warn]: /opt/td-agent/embedded/lib/ruby/gems/2.1.0/gems/fluentd-0.12.29/lib/fluent/output.rb:149:in `run'
2016-10-29 22:01:36 +0900 [warn]: retry succeeded. plugin_id="object:3ff2c7c438cc"
2016-10-29 22:03:17 +0900 [warn]: detached forwarding server '10.1.2.56:24224' host="10.1.2.56" port=24224 hard_timeout=true
サーバ側でポートのバッティングが発生している
サーバ側の /etc/td-agent/td-agent.conf で、複数の <source> タグで forward を指定すると、デフォルトのポート 24224 が利用できないため以下のようなログが出力されます。
$ sudo less /var/log/td-agent/td-agent.log
2016-10-29 22:06:24 +0900 [info]: listening fluent socket on 0.0.0.0:24224
2016-10-29 22:06:24 +0900 [info]: listening dRuby uri="druby://127.0.0.1:24230" object="Engine"
2016-10-29 22:06:24 +0900 [info]: listening fluent socket on 0.0.0.0:24224
2016-10-29 22:06:24 +0900 [error]: unexpected error error_class=Errno::EADDRINUSE error=#