LOG/Fluentd

Fluentd 예제(log파일을 읽어들여 POST API로 전송)

마시멜로를찾아서 2021. 12. 24. 11:44
반응형

Fluentd 예제입니다.

 

대상서버에 td-agent(Fluentd)를 실행시켜 수집된 데이터를 대상서버 POST API로 저장하는 예제입니다.

 

예제 프로세스

 

Fluentd의 conf의 구성은 아래와 같습니다.

Fluentd Configuration

 

코드 ex)

 

<source>
  # 다양한 Input Plugins 중에서 tail을 사용했습니다.
  @type tail
  # 실제 물리적 파일 위치
  # 파일 타입에 따라 *를 쓰거나 %Y%m%d(20211224)로 해당 폴더에서 특정 패턴의 파일을 지정할 수 있다.
  path /test/test_%Y%m%d*.log
  # tag를 이용하여 동일한 conf파일에서 각각의 filter와 match를 구성할 수 있다.
  tag logFile.*
  # 현재 추적하고 있는 파일의 index를 저장하고 있는 파일
  pos_file /var/log/td-agent/logFile.log.pos
  # Parse Plugin 중 정규식 패턴을 이용 @type regexp
  <parse>
    @type regexp
    # 해당 표현의 정합성을 테스트하기 위해서 https://rubular.com/ 를 이용하시면 됩니다.
    # ?<year> 는 year가 키값이 되고 정규식 패턴에 맞는 내용이 value가 됨
    # test string
    # 2021-11-13|error|NullpointError
    expression /^(?<year>[^-]*)-(?<month>[^-]*)-(?<day>[^|]*)[|](?<loglevel>[^|]*)[|](?<data>.*)$/
    time_format %d/%b/%Y:%H:%M:%S %z
  </parse>
</source>

# 대상 source는 여러개가 되어도 된다.
# 동일한 fliter를 탄다면 tag명도 동일하게 셋팅 가능하다.
#<source>
  # 두번째 파일 또는 다른 타입의 input plugin 셋팅 가능
#</source>

# 첫번째 filter 
<filter logFile.**>
  # grep plugin을 사용
  @type grep

  <regexp>
  	# loglevel에서 error와 warn만 보내도록 filter를 걸겠다.
    key loglevel
    pattern /(error|warn)/
  </regexp>

  <exclude>
  	# loglevel에서 pattern not matched는 제외시키겠다
    key loglevel
    pattern /(pattern not matched)/
  </exclude>

</filter>

<filter logFile.**>
  @type grep

  <regexp>
  	# 두번째 fliter data의 데이터중에서 Null인 데이터를 filter 걸겠다.
    key data
    pattern /(Null)/
  </regexp>

  <exclude>
  	# data pattern not matched는 제외시키겠다
    key data
    pattern /(pattern not matched)/
  </exclude>

</filter>

<filter logFile.**>
  # record_transformer 를 이용하여 year, month, day , loglevel, data에서 새로운 Key값을 추가하는 filter
  # 아래 예제는 tail하고 있는 파일 명을 보내고 있다. 
  @type record_transformer
  <record>
    worker_name fluentd_multi
    tag ${tag}
  </record>
  <record>
    hostname ${hostname}
    file ${tag_suffix[-1]}
  </record>
</filter>

#<match logFile.**>
  # http://docs.fluentd.org/articles/out_stdout
#  @type stdout
#</match>

<match logtail.**>
  @type http

  endpoint https://대상api호출 url
  open_timeout 2

  <format>
    @type json 
  </format>
  <buffer>
    flush_interval 10s
  </buffer>
   json_array true
</match>

 

반응형

'LOG > Fluentd' 카테고리의 다른 글

Fluentd(td-agent) linux(ubuntu)설치 가이드  (0) 2021.12.24
Fluentd(td-agent) Windows 설치 가이드  (0) 2021.12.24
Fluentd 시작하기  (0) 2021.12.24