ruby, rubygems, mongodbのセットアップは省略。
Ubuntu 12.04ですが他の環境でも基本的には変わらないと思われます。
Install
sudo gem install fluentd
sudo fluent-gem install fluent-plugin-mongo
Config
設定ファイルの生成
fluentd --setup ./fluent
vim fluent/fluent.conf
末尾などにコピペで追記する
fluent.conf
...
<match sample.**>
type mongo
host localhost
database fluentd
collection sample
<match sample.**>
...
Start
フォアグラウンドで起動
fluentd -c ./fluent/fluent.conf -vv
止める時はctrl+cで
Check
別のターミナルから
echo '{"json":"message"}' | fluent-cat sample.test
echo '{"json":"message 123"}' | fluent-cat sample.test
echo '{"json":"message text"}' | fluent-cat sample.test
さらに別のターミナルから
mongo
でmongoshellを起動
db.sample.find({}).sort({$natural:-1})
で挿入されたログが見れる
TODO
- マシンの起動時に立ち上がるように設定
- 実際の運用においてはfluentdのstable版をrmp/debパッケージ化したtd-agentを使ったほうが良い
- apache, ruby, python, node.jsなどのログをmongodbに貯めてみる
- 異常検知のプラグインを試したい