fluentdでログ収集

fluentd http://fluentd.org/

・複数のアプリケーションサーバーのログを集約、分散等も可能
・イベント毎の振り分けが簡単
・出力先

基本的にやりたいことは全部出来るし、みなさん利用しているようなので使ってみる。
自分で作った変なのをやめて全部こちらに移行する。

> syslogdの話
> http://www.atmarkit.co.jp/flinux/rensai/newlog01/newlog01a.html

| APP Server | | Log Server
| app → fluentd(td-agent) ⇒A⇒ fluentd → 好きな出力B

APP server
input socket <> output forward
A. ログの転送 app to log server

http://docs.fluentd.org/articles/out_forward

分散、失敗した時はファイルに書き出す。

td-agent.conf
include conf.d/*.conf

include conf.d/*.conf

<source>
 type unix
 path /var/run/td-agent/td-agent.sock
</source>

<match **>
 type file
 path /var/log/td-agent/other.log
</match>

conf.d/appname.conf

<match app.appname.*.*>
 type forward
 send_timeout 60s
 recover_wait 10s
 heartbeat_interval 1s
 phi_threshold 8
 hard_timeout 60s

 <server>
 host 192.168.0.11
 port 20011
 </server>
 # secondary host is optional
 <secondary>
   type file
   path /var/log/fluent/foward-app.appname
 </secondary>
</match>

B. ファイルに書き出す。
input http <> output file

http://docs.fluentd.org/articles/out_file

ローテーションなど

# app用フィルター
<match app.appname.**>
 type file
 path /var/log/td-agent/appname
 time_slice_format %Y%m%d
 time_slice_wait 10m
 time_format %Y%m%dT%H%M%S%z
 compress gzip
 #utc
</match>
# httpで受ける
<source>
 type forward
 port 20011
 bind 0.0.0.0
</source>
## live debugging agent
<source>
 type debug_agent
 bind 127.0.0.1
 port 20012
</source>

* source は http ではなく forward

これだけでとりあえずOK

・受け側を起動。
(出力出来ない状態だとエラーが出ずに?失敗します。
2013-01-22 22:49:25 +0900: process finished code=0
2013-01-22 22:49:25 +0900: process died within 1 second. exit.

・送り側を起動

phpから利用する。

http://docs.fluentd.org/articles/php

* autoloaderはオリジナルです。
直接書き込む

include(dirname(__FILE__).’/../autoloader.ini’);
$logger = Fluent\Logger\HttpLogger::open(“192.168.0.11″,”20011″);
$ret = $logger->post(“debug.test”,array(“hello”=>”world”));
var_dump($ret);

OK

forwardする

include(dirname(__FILE__).’/../autoloader.ini’);
$logger = new Fluent\Logger\FluentLogger(“unix:///var/run/td-agent/td-agent.sock”);
$ret = $logger->post(“app.appname.test.follow”, array(“from”=>”userA”, “to”=>”userB”));

fluent.warn {“message”:”temporarily failed to flush the buffer, next retry will be at 2013-01-22 23:57:25 +0900.”,”error”:”no nodes are available”,”instance”:70342469766660}

forward失敗。

port: The port number of the host. The default is 24224. Note that both TCP packets (event stream) and UDP packets (heartbeat message) are sent to this port.

udpの20011(設定したポート)を開きました。

OKです。

This entry was posted on 火曜日, 1月 22nd, 2013 at 5:04 PM and is filed under fluentd. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Unityによるゲームを公開しております iOS/Android
https://itunes.apple.com/jp/app/lost-knight-3d-action/id900917032
https://play.google.com/store/apps/details?id=com.groundroad.runknight

各種開発支援・機能開発等小さいお仕事などもお請けしております。
unity開発支援, PHPシステム開発, javascript/html5 フロント開発, titanium mobileアプリ開発
お気軽にお問い合わせください
大崎・五反田近郊での対面でのお打ち合わせはいつでも可能です。