# Logstash로 특정 파일을 Elastic Search에 색인 및 Elastic Search 삭제/복사 방법 - 최초 작성일: 2021년 8월 26일(목) ## 목차 [TOC] ## Logstash로 특정 파일을 Elastic Search에 색인 - local pc가 보이스봇 Elastic Search을 대상으로 방화벽 해제가 되어 있다면, 아래 curl 명령어를 참조하여 postman과 같은 툴을 사용하여 Elastic Search API를 호출해도 된다. - local pc가 방화벽이 해제되어 있지 않다면, 보이스봇 Elastic Search 서버로 접근하여 아래 명령어를 수행해도 된다. ### Logstash 파일 작성 - logstash config 파일을 아래와 같이 생성한다. ```bash input { file { path => ["로그파일_위치"] start_position => "beginning" sincedb_path => "null" } } filter { dissect { mapping => { "message" => "%{timestamp} | %{data}" } remove_field => ["message"] } date { match => ["timestamp", "ISO8601", "YYYY-MM-dd HH:mm:ss,SSS"] } json { source => "data" remove_field => ["data"] } ruby { init => "require 'time'" code => "event.set('indexDate', Time.now.utc.getlocal.strftime('%Y.%m.%d'))" } ruby { init => "require 'securerandom'" code => "event.set('uuid', SecureRandom.uuid)" } } output { elasticsearch { codec => json hosts => ["http://10.220.202.113:9200", "http://10.220.202.203:9200", "http://10.220.202.162:9200"] index => "색인될_인덱스명" template => "템플릿파일_위치" template_name => "voicebot_personal_log" template_overwrite => "false" document_id => "%{uuid}" # TB에서는 user, password가 필요 없습니다. user => "personal_logstash" password => "personal1234!" } if ("parsefailure" in [tags]) { file { codec => json_lines path => "/data/logstash-6.4.2/logs/aicc_voicebot_pp11/logstash-pp-fail-%{indexDate}.log" } } stdout { codec => rubydebug } } # 샘플 (상용) input { file { path => ["/data/logs/aicc_voicebot_pp11/personal_engine_lamp.log"] start_position => "beginning" sincedb_path => "null" } } filter { dissect { mapping => { "message" => "%{timestamp} | %{data}" } remove_field => ["message"] } date { match => ["timestamp", "ISO8601", "YYYY-MM-dd HH:mm:ss,SSS"] } json { source => "data" remove_field => ["data"] } ruby { init => "require 'time'" code => "event.set('indexDate', Time.now.utc.getlocal.strftime('%Y.%m.%d'))" } ruby { init => "require 'securerandom'" code => "event.set('uuid', SecureRandom.uuid)" } } output { elasticsearch { codec => json hosts => ["http://10.220.202.113:9200", "http://10.220.202.203:9200", "http://10.220.202.162:9200"] index => "voicebot_personal_log_%{indexDate}" template => "/data/logstash-6.4.2/config/templates/personal-log.json" template_name => "voicebot_personal_log" template_overwrite => "false" user => "personal_logstash" password => "personal1234!" document_id => "%{uuid}" } if ("parsefailure" in [tags]) { file { codec => json_lines path => "/data/logstash-6.4.2/logs/aicc_voicebot_pp11/logstash-pp-fail-%{indexDate}.log" } } stdout { codec => rubydebug } } ``` ### Logstash 실행 - logstash가 설치된 폴더로 이동이 필요하다. - TB: /install/logstash-6.4.2 - 상용: /data/logstash-6.4.2 ```bash cd logstash_폴더 bin/logstash -f config_파일위치 # 샘플 cd /data/logstash-6.4.2 bin/logstash -f ./config/logstash-personal.conf ``` ### Elastic Search 내 인덱스 색인 확인 - 색인한 인덱스가 생성되었는지 확인 ```bash curl --location --request GET 'http://10.220.202.113:9200/_cat/indices/인덱스명_년.월.일' \ --header 'Authorization: Basic ZWxhc3RpYzpuZXcxMjM0IQ==' # 샘플 curl --location --request GET 'http://10.220.202.113:9200/_cat/indices/voicebot_personal_log_2021.07.29' \ --header 'Authorization: Basic ZWxhc3RpYzpuZXcxMjM0IQ==' ``` ## Elastic Search 삭제/복사 방법 - local pc가 보이스봇 Elastic Search을 대상으로 방화벽 해제가 되어 있다면, 아래 curl 명령어를 참조하여 postman과 같은 툴을 사용하여 Elastic Search API를 호출해도 된다. - local pc가 방화벽이 해제되어 있지 않다면, 보이스봇 Elastic Search 서버로 접근하여 아래 명령어를 수행해도 된다. - 아래 curl 명령어를 참조하여 customizing 필요한 부분은 수정하여 사용하면 된다. - 인덱스명, 년.월.일 => 이부분은 상황에 맞게 수정 - TB에서는 Authorization 헤더가 필요 없다. ### Elastic Search 내 인덱스 삭제 - local pc가 Elastic Search를 대상으로 방화벽이 해제되어 있어야 합니다. ```bash curl --location --request DELETE 'http://10.220.202.113:9200/인덱스명_년.월.일' \ --header 'Authorization: Basic ZWxhc3RpYzpuZXcxMjM0IQ==' # 샘플 curl --location --request DELETE 'http://10.220.202.113:9200/voicebot_personal_log_2021.07.29' \ --header 'Authorization: Basic ZWxhc3RpYzpuZXcxMjM0IQ==' ``` ### Elastic Search 내 인덱스 복사 - 원본 인덱스의 복사가 필욜한 경우 활용 ```bash curl --location --request POST 'http://10.220.202.113:9200/_reindex' \ --header 'Authorization: Basic ZWxhc3RpYzpuZXcxMjM0IQ==' \ --header 'Content-Type: application/json' \ -d '{ "source": { "index": "인덱스명_년.월.일" }, "dest": { "index": "임시_인덱스명_년.월.일" } }' # 샘플 curl --location --request POST 'http://10.220.202.113:9200/_reindex' \ --header 'Authorization: Basic ZWxhc3RpYzpuZXcxMjM0IQ==' \ --header 'Content-Type: application/json' \ -d '{ "source": { "index": "voicebot_personal_log_2021.07.29" }, "dest": { "index": "temp_voicebot_personal_log_2021.07.29" } }' ```