ãä½æ¥ã¡ã¢ãApacheãã°ãfluentd+MongoDBã§åéãRubyã§éè¨ãã¦ã¿ããâ¦
ãã®ãããæµè¡ã®æ³¢ã«ä¹ãé ããæãããã¾ããfluentdå ¥ãã¦ã¿ã¾ããã
fluentdã¨ã¯
ä½è
ã®å¤æ©ããã®ããã°ããå¼ç¨
http://d.hatena.ne.jp/viver/20110929/p1
-
-
-
-
-
-
- -
-
-
-
-
-
fluent 㯠syslogd ã®ãããªãã¼ã«ã§ãã¤ãã³ããã°ã®è»¢éãéç´ãããããã®ã³ã³ãã¯ããªãã¼ã«ã§ãã
ãã syslogd ã¨ã¯ç°ãªãããã°ã¡ãã»ã¼ã¸ã« ããã¹ãã§ã¯ãªã JSON ãªãã¸ã§ã¯ã ã使ãã¾ããã¾ã ãã©ã°ã¤ã³ã¢ã¼ããã¯ã㣠ãæ¡ç¨ãã¦ããããã°ã®å
¥åå
ãåºåå
ãç°¡åã«è¿½å ã§ãã¾ãã
-
-
-
-
-
-
- -
-
-
-
-
-
ãã°ãç´ æµãªæãã§å¥ãµã¼ãã«è»¢éãéç´ãããã¨ãå¯è½ã§ãã
ãã°ã¯JSONå½¢å¼ã§æ§é åããã¦ããã®ã§ãã°ãã¼ã¿ãå©ç¨ããããã®ãç¹å¾´ã§ãã
æé
â»åæã¨ãã¦apacheã稼åæ¸ã¿ã§ããã¨ãã¾ã
fluentdã¤ã³ã¹ãã¼ã«
yumã§ããã£ã¨
vim /etc/yum.repos.d/td.repo
[treasuredata] name=TreasureData baseurl=http://packages.treasure-data.com/redhat/$basearch gpgcheck=0
yum install td-agent
mongoDBã¤ã³ã¹ãã¼ã«
yumã§ããã£ã¨
vi /etc/yum.repos.d/10gen.repo [10gen] name=10gen Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64 gpgcheck=0
yum install mongo-10gen* --enablerepo=10gen
mongodbèµ·å
/etc/init.d/mongod start
td-agentã®é常åä½ã®ç¢ºèª
td-agentè¨å®å¤æ´
mkdir /var/log/fluent chown td-agent:td-agent /var/log/fluent
vim /etc/td-agent/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ã®ãã¼ããã·ã§ã³å¤æ´
chmod g+rx /var/log/httpd
td-agentèµ·å
service td-agent start
curlã§httpå©ãã¦ã¿ã
http://127.0.0.1
tailf /var/log/fluent/access_log.20120426.b4be8dbd2a283078c 2012-04-26T13:52:25+09:00 apache.access {"host":"127.0.0.1","user":"-","method":"GET","path":"/","code":"200","size":"5","referer":"-","agent":"curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3
åºãï¼ãããªæãã«ãã°ãæ§é åããã¦åºåããã¾ãã
ãã°ãMongoDBã«çªã£è¾¼ãããã«è¨å®
fluent-plugin-mongoã®ã¤ã³ã¹ãã¼ã«
/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.**> type mongo #DBåã¨ã³ã¬ã¯ã·ã§ã³å database apache collection access #MongoDBæ¥ç¶å ã¨ãã¼ã host localhost port 27017 #ã¤ã³ã¿ã¼ãã« flush_interval 10s </match>
td-agentåèµ·å
service td-agent restart
curlã§å©ãã¦ã¿ã
http://127.0.0.1
MongoDBã®ãã¼ã¿ç¢ºèª
mongo
> show dbs apache 0.203125GB > use apache switched to db apache > show collections access system.indexes > db.access.find(); { "_id" : ObjectId("4f98d6c1e138232aef000001"), "host" : "127.0.0.1", "user" : "-", "method" : "GET", "path" : "/", "code" : "200", "size" : "5", "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-04-26T05:01:42Z") }
è¨é²ããã¦ãï¼
ããã§ã¢ã¯ã»ã¹ãããã³ã«Mongoã«ãã¼ã¿ãçªã£è¾¼ãã§ããã¾ãã
rubyã§mongodbã«çªã£è¾¼ãã ãã¼ã¿ãåç §ãã
ã¹ãã¼ã¿ã¹ã³ã¼ã200以å¤ãã©ãã ãåºã¦ãã®ãã調ã¹ããã¨æã£ã¦ããã®ã§ããããªæãã«ã
å ¨ã¢ã¯ã»ã¹ãã°ä»¶æ°
#!/usr/lib64/fluent/ruby/bin/ruby # -*- encoding: utf-8 -*- require 'mongo' m = Mongo::Connection.new('localhost', 27017) db = m.db('apache') puts "å ¨ä»¶æ°" puts db['access'].count
HTTPã¹ãã¼ã¿ã¹ã³ã¼ãã200以å¤ã®ä»¶æ°
puts db['access'].find({:code => {"$ne" => '200'}}).count
hostãhogehogeããã¤HTTPã¹ãã¼ã¿ã¹ã³ã¼ãã200以å¤ã®ä»¶æ°
puts db['access'].find({:code => {"$ne" => '200'} , :host => '127.0.0.1'}).count
ä»ãã1é±é以å ãã¤ãhostãhogehogeãã¤ãHTTPã¹ãã¼ã¿ã¹ã³ã¼ãã200以å¤ã®ä»¶æ°
d = Time.now d = d-24*60*60*7 puts db['access'].find({:code => {"$ne" => '200'} , :host => 'hogehoge' , :time => {"$gt" => d}}).count
ããã§è²ã éè¨ããã°è¶ 便å©ããã¨æã£ã¦ãã¾ããã
ãã
ä»æ´ãfluent-plugin-datacounterãã®åå¨ã«æ°ãä»ãã¾ããã
ã©ãã¿ã¦ããã£ã¡ä½¿ã£ãã»ãã楽ï¼ã¹ãã¼ãã£ã½ãã®ã§ä»ããããã¾ãã
_(:3 ãâ )_
å®
åèãªã³ã¯
http://d.hatena.ne.jp/wyukawa/20120207/1328625443
http://d.hatena.ne.jp/oranie/20120206/1328513678
http://d.hatena.ne.jp/oranie/20120424/1335258767
http://www.mongodb.org/pages/viewpage.action?pageId=18907495