2013年3月15日金曜日

nginx>fluentd>mongodb

スマートフォンからのアクセスをサービスのAPサーバに影響なく指標値として収集したくて、アクセスログやアプリケーションログを使おうと思って、まずは、nginxのアクセスログを保存する方法を検討して、fluentdmongodb を試してみました。

まず、fluentd に mongodb の plugin をインストールします。
[horiga@bin]: fluent-gem install fluent-plugin-mongo
Successfully installed fluent-plugin-mongo-0.6.13
1 gem installed
Installing ri documentation for fluent-plugin-mongo-0.6.13...
Installing RDoc documentation for fluent-plugin-mongo-0.6.13...
で、fluentd の設定ファイルを以下のようにしました。
<source>
 type tail
 path /usr/local/nginx/logs/access_heartbeat.log
 tag nginx.hb
 format /^(?<ipaddr>[^ ]*) \[(?<ts>[^\]]*)\] "(?<httpm>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<stat>[^ ]*) "(?<ua>[^\"]*)" (?<chid>[^ ]*)$/
 time_format %d/%b/%Y:%H:%M:%S %z
 pos_file /usr/local/fluent/logs/nginx_access_heartbeat.log.pos
</source>
<match nginx.**>
 type mongo
 database heartbeatdb
 collection heartbeat
 host localhost
 port 27017
 flush_interval 10s
</match>
そして、fluentd を起動しておきます。ext_bson とかいうpluginの警告ログがでますが、まぁBSONは使わないので無視しておきました。
次に、mongodb を起動します。とりあえずオプションなしのシングルモードで起動します。
defaultポートが27017で起動しました。fluent2mongo.conf に設定した port になりますね。
[horiga@bin]: ./mongod
./mongod --help for help and startup options
Fri Mar 15 21:03:56 [initandlisten] MongoDB starting : pid=51694 port=27017 dbpath=/data/db/ 64-bit host=hiroyuki-no-MacBook-Air.local
Fri Mar 15 21:03:56 [initandlisten] 
Fri Mar 15 21:03:56 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Fri Mar 15 21:03:56 [initandlisten] db version v2.2.2, pdfile version 4.5
Fri Mar 15 21:03:56 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Fri Mar 15 21:03:56 [initandlisten] build info: Darwin bs-osx-106-x86-64-1.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Mar 15 21:03:56 [initandlisten] options: {}
Fri Mar 15 21:03:56 [initandlisten] journal dir=/data/db/journal
Fri Mar 15 21:03:56 [initandlisten] recover : no journal files present, no recovery needed
Fri Mar 15 21:03:56 [websvr] admin web console waiting for connections on port 28017
Fri Mar 15 21:03:56 [initandlisten] waiting for connections on port 27017
nginx の設定はとりあえず静的ファイルを変換するように以下のように設定
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

 log_format fluent '$remote_addr [$time_local] "$request" $status "$http_user_agent" $http_x_test_chid'; 
    
 access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       9001;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

  location /hb/  {
    root html;
    index success.html;
    access_log logs/access_heartbeat.log fluent;
  }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
nginx を起動して、"http://localhost:9001/hb/?q=123" へアクセスしてみるとnginxのアクセスログに確かにアクセスログが残っていることを確認
[horiga@bin]: curl -H 'X-TEST-ChId:123456' http://localhost:9001/hb/?q=123
ok
[horiga@logs]: tail -F access_heartbeat.log 
127.0.0.1 [15/Mar/2013:21:15:25 +0900] "GET /hb/?q=123 HTTP/1.1" 200 "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5" 123456
で、問題のmongodbにはどうか?
> use heartbeatdb 
> db.heartbeat.find();
{ "_id" : ObjectId("514310dc0840f7cb14000001"), "ipaddr" : "127.0.0.1", "ts" : "15/Mar/2013:21:14:27 +0900", "httpm" : "GET", "path" : "/hb/?q=123", "stat" : "200", "ua" : "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5", "chid" : "123456", "time" : ISODate("2013-03-15T12:15:13Z") }
おおっ!!たしかにmongodbにも保存されてました。ここまで特にプログラミングなし、設定のみでできます。
あとは、このデータを使ってデータ分析だな。mongodb からデータを hdfs 上にもってきて分析結果をまた mongodb に格納するとか検討してみます。








0 件のコメント:

コメントを投稿