To content | To menu | To search

FreeBSD で fluentd を試した話

良くある、apache のログを fluentd で他のサーバーに飛ばして mongoDB に保存するところまで

参考。

柔軟なログ収集を可能にする「fluentd」入門
Installing Fluentd Using Ruby Gem
Store Apache Logs into MongoDB

とりあえず VM でまっさらな FreeBSD 2つ用意。

fluentd は gem を pkgng で管理するのが怖かったので(更新とか依存関係とか)、ruby と gem と rake だけ pkgng でインストールして、あとは gem でインストールという方針。

クライアント側。

まずは Apache 。

# pkg install apache24
# rehash
/etc/rc.conf に apache24_enable="yes" を追加
# /usr/local/etc/rc.d/apache24 start

It Works! をみる。

ruby から fluentd まで。pkg search ruby2 で ruby22 が見つからなかったけれど依存関係から呼び出し。

# pkg install devel/ruby-gems devel/rubygem-rake
# rehash
# ruby -v
ruby 2.2.5p319 (2016-04-26 revision 54774) [amd64-freebsd10]
# gem update --system
# gem install td ← いらなそう。
# rehash
# td --version
0.14.1
# gem update td
# gem install fluentd
# rehash
# mkdir fluent
# fluentd --setup ./fluent ← 正直、作り直したのでいらなかった。
Installed ./fluent/fluent.conf.
# fluentd -c ./fluent/fluent.conf -vv &
# echo '{"json":"message"}' | fluent-cat debug.test

そしてメッセージを探す。
そのあと走りっぱなしになるので、

ps -A | grep fluent

でプロセス探して kill した。

コンフィグレーションは元々 fluentd から出力されたものをいろいろいじっていたけれど、使ってないものがあるとやはりややこしいので、新規でコピペで作った。
サーバー側のポート番号はデフォルト。

<source>
  @type tail
  format apache2
  path /var/log/httpd-access.log
  pos_file /var/log/httpd-access.log.pos
  tag apache.access
</source>

<match apache.access>
  type forward
  <server>
    host サーバー側のIP
    port 24224
  </server>
</match>

サーバー側。

# pkg install mongodb
# rehash
/etc/rc.conf に mongod_enable="YES" 追加
# /usr/local/etc/rc.d/mongod start
# fluent-gem install fluent-plugin-mongo
# rehash

で、mongoDB とプラグインをインストール。

コンフィグレーションは

<source>
  type forward
  port 24224
</source>

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

  # mongodb db + collection
  database apache
  collection access

  # mongodb host + port
  host localhost
  port 27017

  # interval
  flush_interval 10s

  # make sure to include the time key
  include_time_key true
</match>

mongoDB にはユーザーやパスワードを設定してない。ポート番号もデフォルトのまま。

最終的に fluentd の起動は

# fluentd -c ./fluent/fluent.conf --daemon /var/run/fluentd.pid -o ./fluent.log

としてデーモン化。ちゃんと起動スクリプトほしい。

# mongo
MongoDB shell version: 2.6.12
connecting to: test
> use apache
switched to db apache
> db["access"].findOne();
{
        "_id" : ObjectId("57bfd1645a770eef17000001"),
        "host" : "クライアントのIP",
        "user" : null,
        "method" : "GET",
        "path" : "/",
        "code" : 200,
        "size" : 45,
        "referer" : null,
        "agent" : null,
        "time" : ISODate("時刻")
}
> db["access"].find();
{ "_id" : ObjectId("57bfd1645a770eef17000001"), "host" : "クライアントのIP", "user" : null, "method" : "GET", "path" : "/", "code" : 200, "size" : 45, "referer" : null, "agent" : null, "time" : ISODate("時刻") }
{ "_id" : ObjectId("57bfd1645a770eef17000002"), "host" : "クライアントのIP", "user" : null, "method" : "GET", "path" : "/favicon.ico", "code" : 404, "size" : 209, "referer" : null, "agent" : null, "time" : ISODate("時刻") }
{ "_id" : ObjectId("57bfd1645a770eef17000003"), "host" : "クライアントのIP", "user" : null, "method" : "GET", "path" : "/", "code" : 200, "size" : 45, "referer" : null, "agent" : null, "time" : ISODate("時刻") }
> exit
bye

といったところ。

Add a comment

HTML code is displayed as text and web addresses are automatically converted.

They posted on the same topic

Trackback URL : https://www.pseudomoon.jp/dotclear/index.php?trackback/66

This post's comments feed