https://www.elastic.co/blog/a-practical-introduction-to-logstash

 

A Practical Introduction to Logstash

Elasticsearch에 대한 액세스 로그를 구문 분석하고 수집하기 위한 구성을 개발할 때 어떻게 Logstash를 사용할 수 있는지 보여주는 Logstash에 대한 소개.

www.elastic.co

((위의 공식 홈페이지 글을 보고 정리한 글입니다.))


logstash를 통해 es로 데이터를 수집하고 싶은데, es-logstash에 대해 간단히 살펴보자.
filebeat는 logstash module을 제공해서 filebeat로 log파일을 logstash로 쏘고, logstash에서 정제하여 es에 수집도 가능!

나는 logstash-es 부분만 일단 확인해본다.

# logstash 간단한 개요
logstash는 플러그인 기반의 데이터 수집 및 처리 엔진.
광범위한 플러그인이 구비됨 -> 다양한 아키텍처에서 손쉽게 데이터 수집,처리,전달이 가능하다.
프로세싱은 하나 이상의 파이프라인으로 구성된다.
각 파이프라인에서 하나 이상의 입력 플러그인이 내부 큐에 배치된 데이터를 수신하거나 수집한다.
  * 이 큐는 기본적으로 작아서 메모리에 보관되지만, 안정성과 복원력을 향상시키도록 디스크에서 더 크고 영구적으로 구성 가능. 

프로세싱 스레드는 마이크로 배치에 있는 큐로 부터 데이터를 읽고, 순서대로 설정된 filter plugin을 처리해준다.
logstash는 많은 수의 plugin을 제공하여 데이터를 파싱하고, 처리하고, 강화할수있게 해준다.

데이터가 처리되면 프로세싱 스레드는 데이터를 output plugin에 넣어준다. formatting하고 데이터를 계속 보내주는 역할을 하는 것이다. 가령  es 로 데이터를 보내는 것 처럼.


logstash pipeline은 한개 이상의 config파일로 생성된다.

1. 파이프라인 지정.

1)  single pipeline using a single configuration file.
     *  logstatsh를 시작하는 가장 쉬운 방법
     * -f 파라미터로  conf파일을 명시하여 실행시키는 방법.

2) single pipeline using multiple configuration files
     * 특정 directory의 모든 파일을 사용하여 설정될수도 있음.
     * logstash.yml에 기술하거나 -f 옵션으로 실행시킬수있음.
     * directory로 주어지면 그 directory의 모든 파일들을 사전식 순서로 연결한 다음 단일 구성 파일로 분석이됨.
     * 따로 conditinals를 사용해서 flow를 컨트롤하지 않는다면,
       모든 input으로부터의 데이터가 모든 filter로 처리되고, 모든 output으로 보내지게 됨.

3) using multiple pipelines
    * multiple pipelines를 사용하려면 pipeliens.yml 파일을 수정해야함.
    * multiple pipelines은 서로 다른 로직의 flow를 분리해서 복잡성을 낮추고, 많은 양의 conditionals를 사용하지 않게 함.
       * 그만큼 유지보수도 쉬워짐
    * 병렬 실행 될 것이기에 퍼포먼스도 향상됨. 효율적임.

2. 첫번째 구성 만들어보기
    * input, output은 필수, filter는 optional

input {
 file {
   path => ["/home/logstash/testdata.log"]
   sincedb_path => "/dev/null"
   start_position => "beginning"
  }
}
filter {
}
output {
  stdout {
    codec => rubydebug
  }
}

input file path를 주고, 
start_position : 시작점. one of ["beginning", "end"]
sincedb : input plugin은 현재 위치를 기록하기 위해 를 사용한다.
   * 다시말해 각각의 input file에 데이터를 계속 트레킹하기 위해 sincedb를 사용하는 것.

sincedb_path
Value type is string
There is no default value for this setting.
Path of the sincedb database file (keeps track of the current position of monitored log files) that will be written to disk. The default will write sincedb files to <path.data>/plugins/inputs/file NOTE: it must be a file path and not a directory path


   * 아래 path가서 보면 sincedb가 생성되어있음.
     그래서 es로 한번 넣었던 파일 다시 넣으니까..안되는구나

sudo cd /usr/share/logstash/data/plugins/inputs/file 
ls -al

 

stdout output plugin은 콘솔에 데이터를 쓰고, rubydebug codec은 구조를 보여줌으로써 config 개발 중에 debugging을 단순화해줌.

3. logstash 시작하기.

 logstash -r -f “/home/logstash/test.conf”

-r 옵션은 구성이 변경되었음을 식별할때마다 자동으로 구성을 다시 로드함.

{
 "message" => "Hello Logstash!",
 "@version" => "1",
 "path" => "/home/logstash/testdata.log",
 "@timestamp" => 2018-04-24T12:40:09.105Z,
 "host" => "localhost"
}

logstash에 들어간 모습. 데이터 포함하여 몇몇 메타데이터도 넣어줌.


+ Recent posts