fluentdを試してみた

クレジットカード現金化詐欺【業界人が教える口コミ情報】

僕は行ってないんですがTwitterUstream、スライド、ブログなどを見る限りだいぶ盛り上がったようですねー。僕自身が仕事で使う予定は今のところ無いんですがログ解析関連の仕事をしていることもあるので素振りしてみようと思います。

環境はVirtualBox上のCenOS 5.7(x86_64)を使いました。

fluentdはRuby 1.9上で動くんですがCentOS 5.7に入っているのはRuby 1.8.5です。Ruby 1.9のインストールから始めるとはまりそうなのでyumでインストールできるtd-agentを使います。td-agentはfluentdの安定版パッケージという位置付けのようです。

試したのは下記3つです。


最初のfluent-catでログを送るで参考にしたサイトはこちらです。
http://d.hatena.ne.jp/erukiti/20120206#1328533889

Yumリポジトリの設定をしてインストールします。

[root@localhost ~]# vim /etc/yum.repos.d/td.repo
[root@localhost ~]# cat /etc/yum.repos.d/td.repo 
[treasuredata]
name=TreasureData
baseurl=http://packages.treasure-data.com/redhat/$basearch
gpgcheck=0
[root@localhost ~]# yum install td-agent

設定ファイル/etc/td-agent/td-agent.confは下記のようにします。

<source>
  type tcp
</source>

<match debug.**>
  type file
  path /var/log/fluent/debug
</match>

あとは事前準備です。

[root@localhost ~]# mkdir /var/log/fluent
[root@localhost ~]# chown td-agent:td-agent /var/log/fluent

そして起動します。

[root@localhost ~]# service td-agent start

td-agentのログが /var/log/td-agent/td-agent.logに出ます。下記のような感じ。エラーがでないことを確認します。ありがちなのはAddress already in use とかPermission deniedかな。

2012-02-07 22:31:24 +0900: reading config file path="/etc/td-agent/td-agent.conf"
2012-02-07 22:31:24 +0900: adding source type="tcp"
2012-02-07 22:31:24 +0900: adding match pattern="debug.**" type="file"
2012-02-07 22:31:24 +0900: running fluent-0.10.6
2012-02-07 22:31:25 +0900: listening fluent socket on 0.0.0.0:24224

fluent-catを使ってtd-agentにログを送ります。

[root@localhost ~]# echo '{"hoge":"fuga"}' | /usr/lib64/fluent/ruby/bin/fluent-cat debug.test

結果を確認するとちゃんと時間、タグ、JSONが出てますね。

[root@localhost ~]# cat /var/log/fluent/debug.20120207.b4b85fcecacf316bc 
2012-02-07T22:32:17+09:00       debug.test      {"hoge":"fuga"}


では次にApacheアクセスログを収集してみましょう。

参考にしたのはこのサイトです。
イベントログ収集ツール fluent を試しに使ってみる - 文::字 - はてな自習室

td-agent.confを下記のように設定します。

<source>
  type tail
  format apache
  path /var/log/httpd/access_log
  tag apache.access
</source>

<match apache.access>
  type file
  path /var/log/fluent/access_log
</match>

このまま起動すると/var/log/httpdパーミッションが700でroot:rootという所有権なのでtd-agentだと読み込めずに下記のようにPermission deniedになります。

2012-02-07 22:47:49 +0900: unexpected error error="Permission denied - /var/log/httpd/
access_log"
  2012-02-07 22:47:49 +0900: /usr/lib64/fluent/ruby/lib/ruby/gems/1.9.1/gems/fluentd-0
.10.6/lib/fluent/plugin/in_tail.rb:97:in `stat'

なので

[root@localhost ~]# chmod g+rx /var/log/httpd

とするとうまくいきます。o+rxでうまくいかないのはよくわからんです。。。td-agentユーザってrootグループじゃないのに。rootLinux弱者だ。。。

ともあれ、service httpd startして下記のようにHTTPアクセスします。

[root@localhost ~]# curl http://localhost
|<<

そうすると下記のように構造化されたログが出力されます。
>||
[root@localhost ~]# cat /var/log/fluent/access_log.20120207.b4b860095492522d2 
2012-02-07T22:48:39+09:00       apache.access   {"host":"127.0.0.1","user":"-","method":"GET","path":"/","code":"403","size":"5043","referer":"-","agent":"curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5"}


最後にMongoDBにApacheアクセスログを出力してみましょう。

参考にしたのはこちら
http://blog.treasure-data.com/post/13766262632/real-time-log-collection-with-fluentd-and-mongodb

まずはMongoDBをインストールします。

[root@localhost ~]# vim /etc/yum.repos.d/10gen.repo
[root@localhost ~]# cat /etc/yum.repos.d/10gen.repo
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0
[root@localhost ~]# yum install mongo-10gen-server
[root@localhost ~]# service mongod start

この時点ではデータは入っていません。

[wyukawa@localhost ~]$ mongo
MongoDB shell version: 2.0.2
connecting to: test
> show dbs;
local   (empty)

次にfluent-plugin-mongoをインストールします。

[root@localhost ~]# /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-mongo

td-agent.confを下記のようにします。

<source>
  type tail
  format apache
  path /var/log/httpd/access_log
  tag mongo.apache
</source>

<match mongo.**>
  # plugin type
  type mongo

  # mongodb db + collection
  database apache
  collection access

  # mongodb host + port
  host localhost
  port 27017

  # interval
  flush_interval 10s
</match>

この状態でtd-agentを起動すると下記のメッセージができます。

Starting td-agent: Able to load bson_ext version 1.4.0, but >= 1.5.2 is required.

**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance.
  You can install the extension as follows:
  gem install bson_ext

  If you continue to receive this message after installing, make sure that the
  bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.

bson_extをインストールしろといっているのでそうします。

[root@localhost ~]# /usr/lib64/fluent/ruby/bin/gem install bson_ext

td-agent起動後にHTTPアクセスします。

[root@localhost ~]# curl http://localhost/

その後MongoDBの中を見ているとデータが入ってます。

> show dbs;
apache  0.015625GB
local   (empty)
> use apache;
switched to db apache
> show collections;
access
system.indexes
> db.access.find();
{ "_id" : ObjectId("4f313256e138231825000001"), "host" : "127.0.0.1", "user" : "-", "method" : "GET", "path" : "/", "code" : "403", "size" : "5043", "referer" : "-", "agent" : "curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5", "time" : ISODate("2012-02-07T14:16:42Z") }

以下のように100アクセスするとdb.access.count();の値が100づつ増えます。

[root@localhost ~]# ab -n 100 -c 10 http://localhost/


td-agent使えばかなり手軽にログ解析できそうですね。wktkしてきました。

fluentdについて調査する場合は下記からいろいろとたどれそうですね。

fluentdのブログ記事などまとめメモ - oranie's blog

いじょ。