4. 4
Intro
Fluentd is a fully free and fully open-source log collector that instantly enables you to have a ‘Log Everything’
architecture with 125+ types of systems.
http://docs.fluentd.org
6. 6
Intro
Input plugin
Input plugin 은 외부로부터 이벤트를 받아오거나 외부의 파일을 읽어서 이벤트를 만들어 주는 역할을 한다.
fluentd 이외의 다른 log aggregator들이 가장 취약한 부분으로, 반대로 말하면 fluentd의 최고 장점이 되는 부분
이기도 하다.
fluentd의 경우는 이미 많은 plugin들이 만들어져 있어서 필요한 대부분의 plugin을 찾을 수 있고,
찾지 못하더라도 쉽게 plugin을 만들 수 있다.
Reference : http://blog.seulgi.kim/2014/04/fluentd-pluggable-log-collector.html
7. 7
Intro
Output plugin
Output plugin 은 읽은 이벤트를 외부 저장소 또는 외부 서비스로 전달하는 역할을 한다
입력은 input plugin 을 통해서 들어와 engine을 거쳐서 buffer plugin을 거치지 않고 output plugin으로 나간다.
buffer는 engine에서 사용되는 것이 아니라 output plugin 내부에서 사용된다.
왜냐하면, output의 종류에 따라서 buffer가 필요하지 않은 경우가 있어, buffer의 사용 여부를 output plugin이 결정해야 하기 때문이다.
buffer plugin 을 사용하지 않는 output plugin을 non-buffered output plugin 이라고 부르며, 대표적인 예가 out_null과 out_stdout
plugin이다. out_null의 경우 들어오는 입력을 전부 버리는 plugin이고, out_stdout은 들어오는 입력을 커맨드 창에 띄워주는 plugin이다.
또 다른 경우는 out_copy 다. 이 plugin은 하나의 fluentd로 들어온 event를 2개 이상의 output으로 보낼 때 쓰인다.
따라서 뒤에 다른 output plugin이 있고, 이 output plugin이 적절한 buffer를 사용하기 때문에 자체적으로 buffer를 이용할 이유가 없다.
평범하게 buffer plugin을 사용하는 plugin들은 buffered output plugin이라고 부르는데 이 중 일부는 time sliced output plugin이라고
불린다. time sliced output plugin은 buffer를 사용하지만, chunk의 key로 tag가 아닌 시간을 사용한다는 것만이 다르다.
8. 8
Intro
Buffer plugin
1. Output 을 효율적으로 내보내는 기능
log aggregator는 실시간으로 로그를 모아주지만, 모은 로그를 바로 바로 output 으로 보낼 이유는 없다. 그래서 fluentd 를 비롯한 대부분의 aggregator는 서버에서 일정량의 로그를
모았다가 처리하도록 해준다. fluentd에서는 이 단위를 chunk라고 부르며, chunk는 log의 tag 별로 분류되어 저장된다.
output plugin은 우선 chunk를 queue에 집어넣지 않고 들어오는 log를 chunk에 적는다. 그러다가 chunk의 크기가 일정 이상 커지거나, chunk가 생긴지 일정 시간 이상 지나면
queue에 들어간다. chunk는 tag를 key로 하므로 buffer에 들어가지 않고 있는 chunk가 한 개 이상일 수도 있다.
queue의 크기를 일정 이상 키우지 않기 위해 queue에 chunk를 집어넣을 때, queue에서 chunk를 1개 빼서 output으로 내보낸다.
2. Collector 서버 장애시 log 유실 최소화하는 기능
Buffer 를 사용한다고 해도 메모리가 무한한 것이 아니므로 서버에 오랫동안 문제 있으면 버려지는 데이터가 생기게 된다. fluentd 는 재시도를 하고 그래도 안 되면 버리는 것을
정책으로 삼고 있다. 즉, output으로 나가야 하는 data가 나가지 못했을 때 일정 시간이 지난 후 다시 시도하며, 그래도 실패한다면, 기다렸던 시간의 2배만큼 더 기다리고
다시 시도하기를 반복한다. 일정 횟수를 기다려도 보내는 것에 실패하면 이 데이터는 다음으로 보내지지 않고 버려진다. (재시도시간: retry_wait, 재시도횟수: retry_limit로 설정)
fluentd 자체가 문제가 생겨서 꺼지는 경우도 있는데, 이것도 buffer의 plugin으로 원하는 종류를 써서 해결할 수 있다.
기본적으로 fluentd가 buffer에 사용하는 것은 buf_memory 라는 plugin으로 chunk를 memory에 기록하는 plugin 이다. 하지만 서버가 죽었다 살아날 때도 보장하고 싶다면
buf_file plugin을 이용하면 된다. buf_file plugin을 사용하면 chunk의 내용을 file에 보관해 주기 때문에 서버가 다시 켜질 때 file을 읽어와 buffer 내용을 복구해준다.
file에 쓰는 만큼 속도가 느려지지만, 안정성이 증가하기도 하고, 사용할 수 있는 buffer의 크기도 커진다.
9. 9
Index
1. Intro
2. Install
3. Test
MySQL slowquery logging
MySQL process list logging
Game Log Data Collect
Log Server 구축
4. QnA
18. 18
Index
1. Intro
2. Install
3. Test
MySQL slowquery logging
MySQL process list logging
Game Log Data Collect
Log Server 구축
4. QnA
19. 19
Step.1 수집대상 서버로 접속하여 Fluentd plugin 설치
https://github.com/y-ken/fluent-plugin-mysql-query
https://github.com/shunwen/fluent-plugin-rename-key
# yum -y install ruby-rdoc ruby-devel rubygems
find / -name fluent-gem
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysql-query
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-rename-key
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Test.2 MySQL process list logging
20. 20
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Test.2 MySQL process list logging
Step.2 수집기(Collector) 서버로 접속하여 Log 테이블 생성
$ mysql -u root -p
use test;
drop table if exists test.t_mysql_process;
create table test.t_mysql_process (
log_date datetime default current_timestamp
, hostname varchar(100)
, id bigint
, user varchar(100)
, host varchar(100)
, db varchar(64)
, command varchar(50)
, duration_time bigint
, state varchar(4000)
, info varchar(10000)
);
21. 21
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Test.2 MySQL process list logging
Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정
$ sudo vi /etc/td-agent/td-agent.conf
…
…
<source>
type mysql_query
host ec-ldb-s2
port 19336
database test
username root
password 433dlxjsjf12!@!
interval 1m
tag ec-ldb-s2.processlist
query show full processlist;
record_hostname yes
nest_result no
nest_key data
#row_count yes
#row_count_key row_count
</source>
<match ec-ldb-s2.processlist>
type rename_key
remove_tag_prefix ec-ldb-s2.
append_tag ec-ldb-s2
rename_rule1 Time duration_time
</match>
<match processlist.ec-ldb-s2>
type copy
<store>
type stdout
</store>
<store>
type mysql_bulk
host ec-ldb-m2
port 19336
database test
username root
password testpasswd12#$
column_names hostname,Id,User,Host,db,Command,State,Info,duration_time
key_names hostname,Id,User,Host,db,Command,State,Info,duration_time
table t_mysql_process
flush_interval 5s
</store>
</match>
24. 24
Index
1. Intro
2. Install
3. Test
MySQL slowquery logging
MySQL process list logging
Game Log Data Collect
Log Server 구축
4. QnA
25. 25
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Step.1 수집기(Collector) 서버로 접속하여 Log 테이블 생성
$ mysql -u root -p
use test;
drop table if exists test.log_game_play;
create table test.log_game_play (
log_date datetime default current_timestamp
, useridx bigint
, play_time bigint
, char_type varchar(1)
, result varchar(1)
);
alter table log_game_play add primary key(log_date,useridx);
Test.3 Game Log Collect
26. 26
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Step.2 수집대상 서버로 접속하여 Log 테이블 생성 및 데이터 입력
$ mysql -u root -p
use test;
drop table if exists test.log_game_play;
create table test.log_game_play (
log_date datetime default current_timestamp
, useridx bigint
, play_time bigint
, char_type varchar(1)
, result varchar(1)
);
alter table log_game_play add primary key(log_date,useridx);
Test.3 Game Log Collect
set @i=0;
insert ignore into test.log_game_play
select date_sub(now(), interval @i:=@i+1 minute) as log_date
, @i
, rand()*10000
, mod(@i,10)
, mod(@i,3)
from mysql.proc
limit 100;
select date_format(log_date, '%Y%m%d%h') as dt, count(*) from
test.log_game_play group by date_format(log_date, '%Y%m%d%h');
select * from test.log_game_play where log_date between date_sub(now(),
interval 10 minute) and now();
27. 27
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정
$ sudo vi /etc/td-agent/td-agent.conf
…
…
<source>
type mysql_query
host ec-ldb-s2
port 19336
database test
username root
password 433dlxjsjf12!@!
interval 10s
tag ec-ldb-s2.log_game_play
query select * from test.log_game_play where log_date
between date_sub(now(), interval 10 minute) and now();
record_hostname yes
nest_result no
nest_key data
#row_count yes
#row_count_key row_count
</source>
<match ec-ldb-s2.log_game_play>
type copy
<store>
type stdout
</store>
<store>
type mysql_bulk
host ec-ldb-m2
port 19336
database test
username root
password 433dlxjsjf12!@!
column_names log_date,useridx,play_time,char_type,result
key_names log_date,useridx,play_time,char_type,result
table log_game_play
on_duplicate_key_update true
on_duplicate_update_keys log_date,useridx
flush_interval 15s
</store>
</match>
Test.3 Game Log Collect
30. 30
Index
1. Intro
2. Install
3. Test
MySQL slowquery logging
MySQL process list logging
Game Log Data Collect
Log Server 구축
4. QnA
31. 31
Step.1 수집대상 서버로 접속하여 Fluentd plugin 설치
https://github.com/tagomoris/fluent-plugin-mysql
# yum -y install ruby-rdoc ruby-devel rubygems
find / -name fluent-gem
/opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-mysql
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Test.4 Log Server 구축
Client
Client
Client
Server
Server
HAProxy(L4)
32. 32
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Step.2 수집기(Collector) 서버로 접속하여 Log 테이블 생성
$ mysql -u root -p
use test;
drop table if exists test.t_log_connect;
create table test.t_log_connect (
log_date datetime default current_timestamp
, jsondata text
);
drop table if exists test.t_log_money;
create table test.t_log_money (
log_date datetime default current_timestamp
, jsondata text
);
Test.4 Log Server 구축
33. 33
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Step.3 수집대상(DB) 서버로 접속하여 td-agent.conf 설정
$ sudo vi /etc/td-agent/td-agent.conf
…
…
<source>
type http
port 8888
body_size_limit 1mb
keepalive_timeout 10s
</source>
<match ec-ldb-s2.t_log_connect>
type copy
<store>
type stdout
</store>
<store>
type mysql
host ec-ldb-m2
port 19336
database test
username root
password testpasswd12#$
table t_log_connect
columns jsondata
format json
flush_interval 5s
</store>
</match>
<match ec-ldb-s2.t_log_money>
type copy
<store>
type stdout
</store>
<store>
type mysql
host ec-ldb-m2
port 19336
database test
username root
password testpasswd12#$
table t_log_money
columns jsondata
format json
flush_interval 5s
</store>
</match>
Test.4 Log Server 구축
34. 34
Collector
[ec-ldb-m2]
Service DB
[ec-ldb-s2]
Step.4 td-agent 재시작
$ sudo /etc/init.d/td-agent stop
sudo /etc/init.d/td-agent start
-- 로그 발생
curl -X POST -d 'json={"ver":"1.0","action":"login","user":1}' http://localhost:8888/ec-ldb-s2.t_log_connect
curl -X POST -d 'json={"ver":"1.0","action":"login","user":1}' http://localhost:8888/ec-ldb-s2.t_log_money
tail -f /var/log/td-agent/td-agent.log
Test.4 Log Server 구축