로그 분석 시스템 세팅 관련
ES를 이용한 로그 분석 시스템 세팅 과정
1. 서버에 td_agent 설치
$ /usr/sbin/td-agent-gem install elasticsearch excon minitest rake test-unit webmock fluentd fluent-plugin-elasticsearch
$ /usr/sbin/td-agent-gem install elasticsearch excon minitest rake test-unit webmock fluentd fluent-plugin-elasticsearch'
2. 서버에 ElasticSearch 설치
3. 레일즈 프로젝트에 lograge 설정
- Gemfile에 lograge 추가
- lograge 젬 설치
- lograge 설정 ({project_path}/config/initializers/lograge.rb)
config = WheelAdmin::Application.config
# lograge : 로그를 특정 기간동안으로 잘라서 기록
config.lograge.enabled = true
# 기본 rails 로그도 남겨 놓도록 설정
config.lograge.keep_original_rails_log = true
# lograge 결과 파일명
config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"
# json 으로 로그 남기도록 formatting
module LogrageFormatter
module_function
def call(data)
result = data.inject({}) { |h, (k, v)| h[k] = v; h }
result.to_json
end
end
# lograge 에 json format 으로 남기기 위해서
config.lograge.formatter = LogrageFormatter
4. 레일즈 프로젝트 lograge 로그 파일 생성되는지 확인
- 서버 접속해서 {projectpath}/log/lograge{Ralis.env}.log 파일 로그 쌓이는지 확인
5. td_agent 데몬 권한 수정
DAEMON_ARGS=${DAEMON_ARGS---user root} 로 변경해서 root 로 실행되도록 수정
6. td_agent 파일 수정
- wheel-deploy/projects/wheel-admin/files/td_agent/alpha/td-agent
DAEMON_ARGS="--user root"
TD_AGENT_ARGS="/usr/sbin/td-agent --group root --log /var/log/td-agent/td-agent.log"
7. td_agent 파일 수정
DAEMON_ARGS="--user root"
TD_AGENT_ARGS="/usr/sbin/td-agent --group root --log /var/log/td-agent/td-agent.log"
8. td_agent 설정 파일 수정
- /etc/td-agent/td-agent.conf
## file input
<source>
type tail
path {project_path}/log/lograge_{Ralis.env}.log
pos_file /var/log/td-agent/wheel.pos
tag public.log_to_es
format json
time_key key10
</source>
# Listen HTTP for monitoring
# http://localhost:24220/api/plugins
# http://localhost:24220/api/plugins?type=TYPE
# http://localhost:24220/api/plugins?tag=MYTAG
<source>
type monitor_agent
port 24220
</source>
<match debug.**>
type stdout
</match>
<match public.log_to_es>
type copy
<store>
type elasticsearch
host es.test.io # ES 서버 호스트
port 9200
logstash_format true
logstash_prefix server-log
type_name server-log
flush_interval 5s
expire_dns_cache 0
</store>
</match>
9. td_agent 데몬 재시작
$ sudo "service td-agent restart"
10. ES에 로그 쌓이는지 확인
- es.test.io (ES 서버 호스트) 에 로그 쌓이는지 확인
댓글
댓글 쓰기