[ELK] logstash
๊ฐ์
- ์๋ฐ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ํ ๋ก๊ทธ๋ค์ ์์งํ๊ณ ์ฒ๋ฆฌํด์ ๋ด๋ณด๋ผ์ ์์.
- ๋ค์ํ ํ๋ฌ๊ทธ์ธ(input, filter, output) ์ ์ ๊ณตํ๊ณ ์๋ ๊ฒ์ด ์ต๋์ ์ฅ์ .
๊ธฐ๋ณธ ์คํ
logstash -f <conf file>
Conf ํ์ผ ๋ด ๊ตฌ์กฐ
- input, output ์ ํ์ํ๋ผ๋ฏธํฐ, filter ๋ ์ต์
- input ์ ๋ฐ์ดํฐ์์ค์์ ๊ฐ์ ธ์ค๋ ํ๋ฌ๊ทธ์ธ
- filter๋ ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ํ๋ ๋๋ก ๋ณ๊ฒฝํ๋ ํ๋ฌ๊ทธ์ธ
- output ์ Data Destination ์ผ๋ก ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ ํ๋ฌ๊ทธ์ธ

๊ฐ๋จํ๊ฒ Apache acesslog ๋ฅผ ๊ฐ์ ธ์์ JSON์ผ๋ก ๋ณํํ๋ ์์ ์ ํด๋ณด์.
first-pipeline.conf
input {
file {
path => "/home/system/logs/test/access-2016-02-03.log"
start_position => beginning
}
}
filter {
grok {
match => { "message" => "%{COMMONAPACHELOG}"}
}
geoip{
source => "clientip"
}
}
output {
stdout{ codec => json }
elasticsearch{}
}
- file input plugin ์ ๊ธฐ๋ณธ ๋์์ beginning ์ผ๋ก ์ง์
- unix์ tail -f์ ๋์ผ. ํน์ ์์น๋ฅผ ์ง์ ํด์ logstash ์ ํ์ผ์ ๋ํ ์ฒ๋ฆฌ ์์ ์์น๋ฅผ ์ง์ ํ ์ ์๋ค.
grok filter plugin
- ์ธ์ ๋๋ ๋ฐ์ดํฐ์ ํจํด์ ์ฐพ์์ ํน์ ํ๋๋ค๋ก ๋งตํ ๋ณํ
- ํจํด์ ์ค์ ํด์ผ ํ๋ค.
- Apache accesslog ํจํด ์ง์ :
{%COMBINEDAPACHELOG%}
geoip filter plugin
- IP ํ๋๋ฅผ ์ง์ ํ๋ฉด, ํด๋น IP๋ฅผ ํตํด์ ์ง์ญ ์ ๋ณด๋ฅผ ์ถ์ถํด ์ฃผ๋ ์์
Check conf file
./logstash -f ./first-pipeline.conf --configtest
- conf file ์ ๊ธฐ๋ฐ์ผ๋ก ์์ ์ด ์ํ๋๊ธฐ ๋๋ฌธ์ ์ ํจํ์ง๋ฅผ ๊ฒ์ฌํด์ผ ํ๋ค.
๊ฒฐ๊ณผ
- ์๋์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, apache accesslog ๋ก ๋ค์ด์ค๋ raw string ์ ํน์ ํ๋๋ก ๋งตํ๋๊ฒ์ ๋ณผ ์ ์๋ค.
- ๋ํ geoip ํญ๋ชฉ์ ๋ณด๋ฉด, ์ด๋์์ ์ ์ํ๋์ง์ ๋ํ ์์น์ ๋ณด๋ฅผ ๊ฐ์ ธ์์ ๋ณด์ฌ์ฃผ๊ณ ์๋ค.
- ์๋์ ์์ ๋ output ํญ๋ชฉ์ stdout ์ผ๋ก ์ค์ ํ๊ณ codec์ json์ผ๋ก ์ค์ ํด์ ์๋์ ๊ฐ์ด json ํ์์ผ๋ก ๋ณด์ฌ์ง๊ณ ์๋ค.
- ๋ค์ํ output plugin์ ์ง์ ํด์ ๋ค๋ฅธ ๋ถ์ ์์คํ ์ด๋, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ๋ก๊ทธ๋ฅผ ๋ณด๋ผ ์ ์๋ค.
{
"message": "127.0.0.1 - - [03/Feb/2016:14:43:58 +0900] \"POST /test/web HTTP/1.1\" 200 22410",
"@version": "1",
"@timestamp": "2016-02-03T05:43:59.585Z",
"path": "/home/system/logs/test/access-2016-02-03.log",
"host": "0.0.0.0",
"clientip": "127.0.0.1",
"ident": "-",
"auth": "-",
"timestamp": "03/Feb/2016:14:43:58 +0900",
"verb": "POST",
"request": "/test/web",
"httpversion": "1.1",
"response": "200",
"bytes": "22410",
"geoip": {
"ip": "127.0.0.1",
"country_code2": "KR",
"country_code3": "KOR",
"country_name": "Korea, Republic of",
"continent_code": "AS",
"region_name": "13",
"city_name": "Seongnam",
"latitude": 37.43860000000001,
"longitude": 127.13780000000003,
"timezone": "Asia/Seoul",
"real_region_name": "Kyonggi-do",
"location": [127.13780000000003, 37.43860000000001]
}
}
References
#dev
#logstash
#elk