OSS編~Fluentdでrewrite_tag_filterを試してみる~

こんにちは! JQです。

前回は『 OSS編~Fluentdでexec_filterを試してみる~ 』と題して、Fluentdでexec_filterを試してみました。

今回は『OSS編~Fluentdでrewritetagfilterを試してみる~』と題して、Fluentdでrewritetagfilterを試してみたいと思います。

rewrite_tag_filter

1. Fluetnd設定

今回はrewritetagfilterを利用してステータスコードをタグに設定して分けてみます。

1<source>2  type tail3  path /var/log/httpd/access_log4  format apache25  pos_file /var/log/td-agent/apache.pos6  tag apache.access7</source>89<match apache.**>10  type rewrite_tag_filter11  rewriterule1 code (.*) $1.${tag}12</match>1314<match 200.**>15  type file16  path /var/log/td-agent/ok__17  time_slice_format %Y%m%d%H%M18  time_slice_wait 60s19  time_format %Y%m%dT%H%M%S%z20  compress gzip21  utc22</match>2324<match **.**>25  type file26  path /var/log/td-agent/error_27  time_slice_format %Y%m%d%H%M28  time_slice_wait 60s29  time_format %Y%m%dT%H%M%S%z30  compress gzip31  utc32</match>

2. 再起動

再起動をして設定を反映させます。

1$ sudo /etc/rc.d/init.d/td-agent restart2Shutting down td-agent:                                    [  OK  ]3Starting td-agent:                                         [  OK  ]

3. 確認

それでは確認してみましょう。

実際に200のステータスが出るページと404等が出るページにアクセスしてみます。

正しければ下記のようにログが別れた状態で出力されます。

1$ ls -l2total 803-rw-r--r-- 1 td-agent td-agent    52 Jul 30 17:07 apache.pos4drwxrwxrwx 2 td-agent td-agent  4096 Jul 24 16:13 buffer5-rw-r--r-- 1 td-agent td-agent   295 Jul 30 17:08 error_.201407300806_0.log.gz6-rw-r--r-- 1 td-agent td-agent   222 Jul 30 17:09 error_.201407300807_0.log.gz7-rw-r--r-- 1 td-agent td-agent   207 Jul 30 17:09 ok__.201407300807_0.log.gz8-rw-r----- 1 td-agent td-agent 43945 Jul 30 17:06 td-agent.log9-rw-r--r-- 1 td-agent td-agent  4405 Jul 30 14:12 td-agent.log-20140728.gz10-rw-r----- 1 td-agent td-agent  2381 Jul 30 14:12 td-agent.log-20140730

中身も正しいか見てみます。

1$ sudo gunzip ok__.201407300807_0.log.gz2$ tail ok__.201407300807_0.log3420140730T080720+0000    200.apache.access       {"host":"192.168.0.4","user":null,"method":"GET","path":"/test.html","code":200,"size":null,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"}

こちらも確認してみます。

codeが200以外がまとめられてます。

1$ sudo gunzip error_.201407300806_0.log.gz2$ sudo tail error_.201407300806_0.log3420140730T080645+0000    403.apache.access       {"host":"192.168.0.4","user":null,"method":"GET","path":"/","code":403,"size":3839,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"}520140730T080645+0000    304.apache.access       {"host":"192.168.0.4","user":null,"method":"GET","path":"/icons/apache_pb2.gif","code":304,"size":null,"referer":"http://54.210.121.125/","agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"}620140730T080646+0000    403.apache.access       {"host":"192.168.0.4","user":null,"method":"GET","path":"/","code":403,"size":3839,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"}720140730T080647+0000    304.apache.access       {"host":"192.168.0.4","user":null,"method":"GET","path":"/icons/apache_pb2.gif","code":304,"size":null,"referer":"http://54.210.121.125/","agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"}820140730T080651+0000    404.apache.access       {"host":"192.168.0.4","user":null,"method":"GET","path":"/index.html","code":404,"size":288,"referer":null,"agent":"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0"}

いかがでしたでしょうか?

次回もお楽しみに!!!

この記事を書いた人

aws-recipe-user