これは、なにをしたくて書いたもの?
Fluentdを使って、ひとつのinputから複数のoutputに出力する練習に、と。
こちらのエントリの変形版です。
Fluentdで、Apacheのアクセスログを読み込んで、内容によって出力先(output)を振り分けることを考える - CLOVER🍀
お題
Fluentdを使って、Apacheのアクセスログをtailして読み込み、複数の出力先に対して出力することを考えます。
出力先は、Elasticsearchと標準出力(Fluentdのログ)とします。
環境
Ubuntu Linux 18.04 LTS上で行い、Fluentdのバージョンは以下とします。
2019-03-10 16:47:02 +0000 [info]: starting fluentd-1.3.3 pid=1125 ruby="2.4.5"
元ネタと同じく、Apacheは同じ環境上でインストール。Apacheで公開するコンテンツは、Apache自身のドキュメントとしています。
$ sudo apt install apache2 -y $ sudo systemctl start apache2.service $ cd /var/www/html/ $ sudo wget https://www-us.apache.org/dist//httpd/docs/httpd-docs-2.4.33.en.zip $ sudo unzip httpd-docs-2.4.33.en.zip $ sudo mv httpd-docs-2.4.33.en/* ./.
Apacheのバージョンは、こちらです。
[Sun Mar 10 14:07:04.729072 2019] [mpm_event:notice] [pid 600:tid 140156732517312] AH00489: Apache/2.4.29 (Ubuntu) configured -- resuming normal operations
FluentdがApacheのアクセスログを読み込めるように、ディレクトリおよびファイルの権限変更。
$ sudo chmod 755 /var/log/apache2 $ sudo chmod 544 /var/log/apache2/*
Elasticsearchは、6.6.1で別ホスト(172.17.0.3)で用意。
Fluentdの設定をする
では、Fluentdの設定を行います。
Apacheのアクセスログの読み込みにはtail、パースにはapache2プラグインを使用しています。
Output Pluginには、まずはcopyを使用。
copyプラグインは、複数の出力先にイベントをコピーできるプラグインです。
/etc/td-agent/td-agent.conf
<source> @type tail @id input_tail <parse> @type apache2 </parse> path /var/log/apache2/access.log pos_file /var/log/td-agent/apache2-access.log.pos tag apache.access </source> <match apache.access> @type copy @id access_log_copy <store> @type stdout @id output_access_log_stdout </store> <store> @type elasticsearch @id output_access_log_elasticsearch host 172.17.0.3 port 9200 index_name access-log </store> </match>
出力先はstore(複数可)内に書くことができ、今回はstdoutとelasticsearchを使用しています。
<match apache.access> @type copy @id access_log_copy <store> @type stdout @id output_access_log_stdout </store> <store> @type elasticsearch @id output_access_log_elasticsearch host 172.17.0.3 port 9200 index_name access-log </store> </match>
storeの中には、Output Pluginの設定をそのまま書くようです。
stdout Output Plugin | Fluentd
Elasticsearch Output Plugin | Fluentd
ドキュメントを見ていると、formatやbufferも書くことができるようですね。
また、routeプラグインやラベルと組み合わせた例も。
この設定で、Fluentdを再起動します。
確認
あとは、Apacheにアクセスして確認してみます。
Fluentdのログファイルには、こんな感じでログが出力されます。
2019-03-10 17:15:49.000000000 +0000 apache.access: {"host":"172.17.0.1","user":null,"method":"GET","path":"/","code":200,"size":2502,"referer":null,"agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"} 2019-03-10 17:15:49.000000000 +0000 apache.access: {"host":"172.17.0.1","user":null,"method":"GET","path":"/style/css/manual-zip.css","code":200,"size":862,"referer":"http://172.17.0.2/","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"} 2019-03-10 17:15:49.000000000 +0000 apache.access: {"host":"172.17.0.1","user":null,"method":"GET","path":"/style/css/prettify.css","code":200,"size":1443,"referer":"http://172.17.0.2/","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"} 2019-03-10 17:15:49.000000000 +0000 apache.access: {"host":"172.17.0.1","user":null,"method":"GET","path":"/images/feather.png","code":200,"size":21433,"referer":"http://172.17.0.2/","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"} 〜省略〜
Elasticsearchにも、(Bufferがフラッシュされた後に)同じようにログが入ります。
$ curl '172.17.0.3:9200/access-log/_search?q=*&pretty' { "took" : 4, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 11, "max_score" : 1.0, "hits" : [ { "_index" : "access-log", "_type" : "fluentd", "_id" : "WFCbaGkBfPGZ7ujxf7Ep", "_score" : 1.0, "_source" : { "host" : "172.17.0.1", "user" : null, "method" : "GET", "path" : "/", "code" : 200, "size" : 2502, "referer" : null, "agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" } }, { "_index" : "access-log", "_type" : "fluentd", "_id" : "XVCbaGkBfPGZ7ujxf7Ep", "_score" : 1.0, "_source" : { "host" : "172.17.0.1", "user" : null, "method" : "GET", "path" : "/style/scripts/prettify.min.js", "code" : 200, "size" : 13428, "referer" : "http://172.17.0.2/", "agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" } }, 〜省略〜 } ] } }
これで、ひとつのログから複数のOutputへ出力できることを確認できました、と。