最近ログ周りの解析が多いので、何やらよく聞き出したfluentdを使ってみることにしました。
#Rubyの1.9.2以上が必要らしいので要注意
まずはインストール。これはgemでやってしまいました。
# gem install fluentd /usr/local/ruby1.9/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>': It seems your ruby installation is missing psych (for YAML output). To eliminate this warning, please install libyaml and reinstall your ruby. Fetching: msgpack-0.4.6.gem (100%) Building native extensions. This could take a while... Fetching: yajl-ruby-1.1.0.gem (100%) Building native extensions. This could take a while... Fetching: iobuffer-1.1.2.gem (100%) Building native extensions. This could take a while... Fetching: cool.io-1.1.0.gem (100%) Building native extensions. This could take a while... Fetching: http_parser.rb-0.5.3.gem (100%) Building native extensions. This could take a while... Fetching: fluentd-0.10.17.gem (100%) Successfully installed msgpack-0.4.6 Successfully installed yajl-ruby-1.1.0 Successfully installed iobuffer-1.1.2 Successfully installed cool.io-1.1.0 Successfully installed http_parser.rb-0.5.3 Successfully installed fluentd-0.10.17 6 gems installed Installing ri documentation for msgpack-0.4.6... Installing ri documentation for yajl-ruby-1.1.0... Installing ri documentation for iobuffer-1.1.2... Installing ri documentation for cool.io-1.1.0... Installing ri documentation for http_parser.rb-0.5.3... Installing ri documentation for fluentd-0.10.17... Installing RDoc documentation for msgpack-0.4.6... Installing RDoc documentation for yajl-ruby-1.1.0... Installing RDoc documentation for iobuffer-1.1.2... Installing RDoc documentation for cool.io-1.1.0... Installing RDoc documentation for http_parser.rb-0.5.3... Installing RDoc documentation for fluentd-0.10.17...
YAMLがどうたらこうたら言われているが今は気にしないでおくことにしました。
この後、3つのコマンドがインストールされました。
- fluentd
- fluent-cat
- fluent-gem
ここでfluentdの初期設定ファイルを作成します。今回は/etc/fluentd配下に設定ファイルを作成することにします。
# fluentd --setup /etc/fluentd Installed /etc/fluentd/fluent.conf.
出来たようなので、実際の設定ファイルの中身を見てみましょう。
## built-in TCP input ## $ echo <json> | fluent-cat <tag> <source> type forward </source> ## built-in UNIX socket input #<source> # type unix #</source> # HTTP input # http://localhost:8888/<tag>?json=<json> <source> type http port 8888 </source> ## File input ## read apache logs with tag=apache.access #<source> # type tail # format apache # path /var/log/httpd-access.log # tag apache.access #</source> ## match tag=apache.access and write to file #<match apache.access> # type file # path /var/log/fluent/access #</match> ## match tag=debug.** and dump to console <match debug.**> type stdout </match> ## match tag=system.** and forward to another fluent server #<match system.**> # type forward # host 192.168.0.11 # <secondary> # host 192.168.0.12 # </secondary> #</match> ## match tag=myapp.** and forward and write to file #<match myapp.**> # type copy # <store> # type forward # host 192.168.0.13 # buffer_type file # buffer_path /var/log/fluent/myapp-forward # retry_limit 50 # flush_interval 10s # </store> # <store> # type file # path /var/log/fluent/myapp # </store> #</match> ## match fluent's internal events #<match fluent.**> # type null #</match> ## match not matched logs and write to file #<match **> # type file # path /var/log/fluent/else # compress gz #</match>
とりあえず、Apacheのログをfluentdに送って、ファイルに書き出してみます。
Apacheのログファイルを読み出す際の設定はこんな感じで。
web_server.conf
<source> type tail format apache path /usr/local/apache2/logs/access_log tag apache.access </source> <match apache.access> type tcp host localhost </match>
受け取るfluetd側の設定はこんな感じ。
log_server.conf
<source> type tcp </source> <match apache.access> type file path /var/log/fluent/access_log </match>
基本的には
これで起動してみましょう。
# fluentd -c /etc/fluentd/web_server.conf & # fluentd -c /etc/fluentd/log_server.conf &
これでApacheにアクセスをしてみます。
Apacheのaccess_logにはこのように出力されます。
172.16.173.1 - - [10/Apr/2012:16:46:20 +0900] "GET / HTTP/1.1" 304 -
fluentd側にはこのようにJSONでパースされて出力されます。
2012-04-10T16:46:20+09:00 apache.access {"host":"172.16.173.1","user":"-","method":"GET","path":"/","code":"304","size":"-"}
中々、便利そうですね。apache以外にもいろいろ試せるか調べてみたいと思います。











