LoginSignup
11
10

More than 5 years have passed since last update.

みんなでやるRiak Advent Calendar 8日目。
一週間で基本操作を終えたので、どんどん進もう!
今回は初の応用編。
大人気のFluentd連携だ。

今回やりたいこと

一台の開発環境で、apacheのアクセスログをriakに入れる。
つまりこれを試したい。
riak advent calendar day7資料.png

RubyとFluentdとfluent-plugin-riakのインストール

ちなみに今までずっと書き忘れていたが、CentOS 6.4。

[vagrant@localhost ~]$ wget http://www.dnsbalance.ring.gr.jp/archives/lang/ruby/2.0/ruby-2.0.0-p353.tar.gz
[vagrant@localhost ~]$ tar zxvf ruby-2.0.0-p353.tar.gz
[vagrant@localhost ~]$ cd ruby-2.0.0-p353
[vagrant@localhost ruby-2.0.0-p353]$ /configure
[vagrant@localhost ruby-2.0.0-p353]$ make
[vagrant@localhost ruby-2.0.0-p353]$ sudo make install
[vagrant@localhost ~]$ curl -L http://toolbelt.treasuredata.com/sh/install-redhat.sh | sh
[vagrant@localhost ~]$ sudo service td-agent start
Starting td-agent:                                         [  OK  ]
[vagrant@localhost ~]$ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-riak

riakとfluentdの設定をする。
fluent-plugin-riakの設定はREADMEを参照。
また、plugin的にRiakのバックエンドはeLeveldbに限定される。
その設定も併せて行う。
設定変更後はriakの再起動を忘れないこと。

[vagrant@localhost ~]$ cat /etc/td-agent/td-agent.conf
<source>
      type tail
      format apache
      path /var/log/httpd/access_log
      tag riak.apache
</source>
<match riak.**>
      type riak
      buffer_type memory
      flush_interval 10s
      retry_limit 5
      retry_wait 1s
      buffer_chunk_limit 256m
      buffer_queue_limit 8096
      nodes 127.0.0.1:8087
</match>

[vagrant@localhost riak]$ sed -e 's/storage_backend = bitcask/storage_backend = eleveldb/' \
> -i rel/riak/etc/riak.conf

[vagrant@localhost riak]$ rel/riak/bin/riak restart
ok

そしてlocalhostにcurlし、riakでそれを見ることができれば、fluentdからriakへのログの保存ができていることになる。
fluent-plugin-riakによってfluentlogというbucketにuuidをkeyにして格納される。
よって、まず有効なkeyを見つけて、valueを見てみる。

[vagrant@localhost]$ sudo chmod g+rx /var/log/httpd
[vagrant@localhost]$ curl localhost

[vagrant@localhost]$ curl http://localhost:8098/buckets/fluentlog/keys?keys=true
{"keys":["2013-12-08-02db1782-ea02-4549-addf-94816f42acb9"]}

[vagrant@localhost ~]$ curl http://localhost:8098/buckets/fluentlog/keys/2013-12-08-02db1782-ea02-4549-addf-94816f42acb9
[{"host":"::1","user":"-","method":"GET","path":"/","code":"403","size":"5039","referer":"-","agent":"curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2","time":"2013-12-08T13:14:30Z","tag":"riak.apache"}]

今後の展望

これでapacheへのログデータをriakに入れることができた。
もちろん入れて終わりではなく、次は解析を考える。
これには幾つか方法が考えられ、例えば

  • Yokozuna
  • 2iを使ってMapReduce

があるだろう。
Yokozunaは先日のアドベントカレンダーでも触れた、Riak2.0の新しい全文検索機能。
fluentdからのログはJSONになってriakに入るが、そのJSONにsolr風なレンジクエリをかけることもできるらしい。
これが手っ取り早く強力な操作ができる方法だろう。

2iとはセカンダリ・インデックスのことで、バックエンドにeLevelDBを指定することで使用可能になるインデックスだ。
機会があれば、今後のエントリでRiakにおけるMapReduceに触れるかもしれないので乞うご期待!
またはあなたが書くのだ!

まとめ

Riakはデータベースだ。
データを入れて活用して初めて用を成す。
どんどん入れよう!どんどん検索しよう!
面倒なことは全てRiakに任せて、僕たちは本質に集中しよう!

追伸

ちなみに今週の12/11は、Riak SCRが開催される。
まだ若干の枠があるので、興味がある人は気軽に参加してみよう!

謝罪

実は…8月に僕がブログに書いたネタだったりする…
加筆修正版ということでお願いします…

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10