DevOps/ELK

Logstash Tutorial, 로그스태시 튜토리얼

게임이 더 좋아 2022. 12. 29. 23:29
반응형
728x170

Logstash Tutorial

 

설치

(https://www.elastic.co/kr/downloads/logstash)

패키지 매니저로 다운 받거나

직접 wget, curl ( https://www.elastic.co/downloads.) 으로 binaries 다운 가능

 

예시

curl -O https://artifacts.elastic.co/downloads/logstash/logstash-5.1.1.tar.gz
tar -xvzf logstash-5.1.1.tar.gz

#링크를 설정하여 유용하게 이용 가능
ln -s logstash-5.1.1 logstash

 

 

 

+필요한 패턴

(grok 패턴 추가 등)

특정 디렉토리에 해당 패턴 파일을 추가

mkdir /{user}/service/logstash/patterns

"PATTERN_NAME PATTERN" 형식을 가짐

패턴 파일 예시

 
#CUSTOM_PATTERN 이란 이름을 가진 패턴은
#NUMBER 를 field에 매칭함
CUSTOM_PATTERN %{NUMBER:field}

 

+Logstash configuration 세팅

(JSON format을 주로 사용) - https://www.elastic.co/guide/en/logstash/current/configuration.html

input(로그 위치) → 파일로부터 로그를 가져옴(extract)

filter(로그 필터링) → 로그를 필터링함(transform)

output(필터링 된 데이터) → 정제된 로그들을 저장함(load)

++codec을 쓰기도 함(https://www.elastic.co/guide/en/logstash/current/codec-plugins.html)

 

 

input {
  file {
    #path -> 로그 파일이 있는 경로
    path => "/var/log/messages"
  }
}

filter {
  #grok 패턴을 이용한 로그 필터링
  grok {
    match => { "message" => "%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST:host} %{DATA:syslog_message}" }
    #외부 패턴, 사용자 정의 패턴을 사용 가능
    patterns_dir => "/{user}/service/logstash/patterns"
  }
}

output {

  #Logstash로 정제된 로그들을 어디에 적재할 것인지 -> 다른 솔루션 이용 가능
  #host:port 를 지정해야함
  elasticsearch {
    hosts => ["localhost:9200"]
    #index를 지정하여 load 가능
    index => "logstash-%{+YYYY.MM.dd}"
  }
}

 

추가 예시 - https://www.elastic.co/guide/en/logstash/current/config-examples.html

 

 

+Setting file

yaml 형식을 가지는 설정 파일- https://www.elastic.co/guide/en/logstash/current/config-setting-files.html

실행할 때 옵션을 선택하는 것이 아닌 미리 지정된 세팅 (--path.settings /{path}/{xxx}.yml 을 사용하여 지정) 설정 파일 사용 가능

 

 

예) logstash.yml 파일 - https://www.elastic.co/guide/en/logstash/current/logstash-settings-file.html

# Set the number of pipeline workers
pipeline.workers: 8

# Set the batch size for the pipeline
pipeline.batch.size: 1024

# Set the path to the configuration file
path.config: /path/to/config.conf

# Set the path to the data directory
path.data: /path/to/data

# Set the path to the logs directory
path.logs: /path/to/logs

# Set the path to the plugins directory
path.plugins: /path/to/plugins

# Set the path to the patterns directory
path.patterns: /path/to/patterns

# Set the path to the queue data directory
queue.path: /path/to/queue

# Set the queue type
queue.type: memory

# Set the maximum size of the queue
queue.max_bytes: 1073741824

# Set the maximum number of events in the queue
queue.max_events: 10000

# Set the queue page capacity
queue.page_capacity: 262144

# Set the queue poll interval
queue.polling_interval: 1

# Set the queue type
queue.type: persisted

# Set the path to the persisted queue data directory
queue.persisted.path: /path/to/persisted_queue

# Set the maximum size of the persisted queue
queue.persisted.max_bytes: 1073741824

# Set the maximum number of events in the persisted queue
queue.persisted.max_events: 10000

# Set the persisted queue page capacity
queue.persisted.page_capacity: 262144

# Set the persisted queue poll interval
queue.persisted.polling_interval: 1

# Set the persisted queue checkpoint interval
queue.persisted.checkpoint.interval: 1000

# Set the persisted queue checkpoint acknowledge interval
queue.persisted.checkpoint.acks: 100

# Set the persisted queue checkpoint flush interval
queue.persisted.checkpoint.writes: 100

# Set the persisted queue checkpoint cleanup interval
queue.persisted.checkpoint.cleanup: 3600

# Set the persisted queue checkpoint delete interval
queue.persisted.checkpoint.delete: 86400

 

 

 

실행

 

bin/logstash -f /{user_path}/config.conf

관련 옵션

  • -e: Run Logstash in the command-line interface (CLI) and enter the configuration directly in the command prompt.
  • -w: Set the number of worker threads to use.
  • -b: Set the size of the event backlog.
  • --config.reload.automatic: Automatically reload the configuration file if it is modified.

 

옵션을 쓰면 

bin/logstash -f /path/to/config.conf -w 8 -b 1024 --config.reload.automatic
→ 8개의 워커 스레드를 이용해 Logstash를 실행하며 backlog size는 1024 이벤트이고 자동으로 configuration이 불러옴

--config.reload.automatic 라는 이 옵션은 실제로 Logstash 설정이 바뀌어도 계속 동작할 수 있게 하는 것과 같음

 

 

실제로 실행은 Shell script로 많이 실행

#!/bin/bash

# Set the path to the Logstash installation directory
LOGSTASH_HOME="/usr/share/logstash"

# Set the path to the Logstash configuration file
CONFIG_FILE="/path/to/config.conf"

# Set the number of worker threads
WORKER_THREADS=8

# Set the size of the event backlog
BACKLOG_SIZE=1024

# Run Logstash
# 다만 실행 시에 Shell 연결이 꺼져도 실행하기 위해 nohup을 사용함
# 또는 daemonize를 하기도 함 - bin/logstash -f /path/to/config.conf --config.reload.automatic --daemonize
# 또는 screen을 사용하기도 함 - screen -d -m bin/logstash -f /path/to/config.conf --config.reload.automatic

nohup "$LOGSTASH_HOME/bin/logstash" -f "$CONFIG_FILE" -w "$WORKER_THREADS" -b "$BACKLOG_SIZE" --config.reload.automatic &

 

 

반응형
그리드형

'DevOps > ELK' 카테고리의 다른 글

Elasticsearch  (2) 2023.04.01
Logstash vs Filebeat  (0) 2023.01.04
Filebeat Tutorial, 파일비트 튜토리얼  (0) 2022.12.29
ELK, Elasticsearch+Logstash+Kibana  (0) 2022.12.29
grok  (0) 2022.12.29