본문 바로가기
Opensource 모니터링 구축/ELK

ElasticSearch, Logstash - 이벤트 알림 시스템 구축

by Infralabs 2024. 3. 20.
반응형

시스템 특이사항 발생시 로그 감시와 카카오 알림톡을 발송하는 알림 시스템이 필요하여 기술 검토와 구축을 진행하였다.

Logstash를 통해 로그 파일의 감시를 수행하고, 해당 결과를 ElasticSearch에 쌓는 방식으로 검토하였다.

 

1. 로그 수집

- input file : Logstash 를 활용하여 /var/log/errorlog 로그 수집

  (ElasticSearch 부하 최소화를 위한 /var/log/errorlog 로그 파일 신규 생성)

 

1-1. /etc/rsyslog.conf 설정

vi /etc/rsyslog.conf

*.error;*.crit;*.emerg;*.panic;cron.none;authpriv.none /var/log/errorlog

 

2. 전송

- 전송 방법 : Logstash → ElasticSearch 에 json format으로 전송

- 설치 경로 : /app/logstash

- config 경로 : /app/logstash/config/logstash.conf

- ElasticSearch 부하 최소화를 위한 logstash.conf에서 filter 사용

 

2-1. Logstash 설정 

vi /app/logstash/config/logstash.conf

- filter 에 불필요한 데이터는 drop하도록 설정

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
	file{
               path => "/var/log/errorlog"
               start_position => beginning
        }
}

filter {
        if "Route" in [message] {
                drop { }
        }
        if "dnf" in [message] {
                drop { }
        }
        if "drift" in [message] {
                drop { }
        }
	if "elasticsearch" in [message] {
		drop { }
	}
        mutate {
                replace  => { "sendmsg" => "not" }
        }
        ruby {
                init => "require 'securerandom'"
                code => "event.set('uuid', SecureRandom.uuid)"
        }
}

output{
        elasticsearch{
                hosts => "https://{ElasticSearch 서버ip:Port}"
                ssl_enabled => true
                ssl_truststore_path => "{인증서 경로}"
                ssl_truststore_password => "{password}"
                ssl_keystore_path => "{인증서 경로}"
                ssl_keystore_password => "{password}"
                user => "{계정}"
                password => "{패스워드}"
                index => "syslog"
                document_id => "%{uuid}"
        }
}

 

3. 알림 서버 수집

- ElasticSearch 에 쌓인 걸 조회하는 방식으로 구축하였고, 메세지를 발송한 경우 flag 값(코드 내 sendmsg)을 이용하여 구분하였다.

- output exec 플러그인 내 command를 통해 메세지를 보내는 스크립트를 수행하도록 하였다. 

  (현재 우리는 알림톡 발송을 위한 에이전트가 있고, 해당 에이전트를 사용하도록 스크립트를 작성했다.)

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.
input {
        elasticsearch {
                hosts => "https://{ElasticSearch 서버 IP:Port}"
                ssl_enabled => true
                ssl_truststore_path => "{인증서 경로}"
                ssl_truststore_password => "{password}"
                ssl_keystore_path => "{인증서 경로}"
                ssl_keystore_password => "{password}"
                index => "syslog"
                query => '{"query": {"bool": {"must": [{"match": {"message": "IR OR WB OR AP OR DB" } }, {"match": {"sendmsg": "not" } } ] } } }'
                size => 100
                scroll => "1m"
                user => "{계정}"
                password => "{패스워드}"
		schedule => "* * * * *"
        }
}
filter {
	if "not" in [sendmsg] {
		mutate {
                	replace  => [ "sendmsg","ok" ]
		}
        }
}
output{
        exec {
                codec => "json"
                command => "sh /app/logstash-server/script/sendMsg.sh %{message}"
        }
        elasticsearch{
                hosts => "https://{ElasticSearch 서버 IP:Port}"
                ssl_enabled => true
                ssl_truststore_path => "{인증서 경로}"
                ssl_truststore_password => "{password}"
                ssl_keystore_path => "{인증서 경로}"
                ssl_keystore_password => "{password}"
                user => "{계정}"
                password => "{패스워드}"
		action => "update"
                index => "syslog"
		document_id => "%{uuid}"		
        }
}
반응형