logstash Grok Pattern 적용(Apache Access log) - 기본편(1)

    Grok Debugger를 사용하여 Apache Access Log Parsing 해보자.

     

    1. Combined 로그 형식

    access_log 출력 예시

    #log/access_log
    
    1.2.3.4 - - [28/Apr/2019:00:01:58 +0900] "GET /assets/css/reset.css HTTP/1.1" 200 1075 "https://www.example.com/login/form.do" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"

     

    httpd.conf 설정 값 확인

    # httpd.conf
    
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    CustomLog log/access_log combined
    
    
    %h: 서버에 요청을 한 클라이언트(원격 호스트)의 IP 주소이다.
    %l: 출력에서 "빼기기호"는 요청한 정보가 없음을 나타낸다. 
        이 경우 여기에 나올 정보는 클라이언트 컴퓨터의 identd가 제공할 클라이언트의 RFC 1413 신원이다.
    %u: HTTP 인증으로 알아낸 문서를 요청한 사용자의 userid이다.
    %t: 서버가 요청처리를 마친 시간
    \"%r\": 클라이언트의 요청줄이 쌍따옴표로 묶여있다.
    %>s: 서버가 클라이언트에게 보내는 상태코드이다. 
    %b: 응답 헤더를 제외하고 클라이언트에게 보내는 내용의 크기를 나타낸다. 
    \"%{Referer}i\" : 클라이언트가 참조했다고 서버에게 알린 사이트
    \"%{User-agent}i\" : 클라이언트 브라우저가 자신에 대해 알리는 식별정보이다.

    2. Grok Debugger 사용

    1) 로그 패턴 확인

    어떤 패턴을 적용해야할지 모를때 간단히 확인 가능함
    ① URL 접속( http://grokdebug.herokuapp.com/discover )
    ② 로그 내용 입력 > Discover 클릭
    ③ 하단에 Pattern 출력됨(내장 Pettern)

      http://grokdebug.herokuapp.com/discover  
    #NOTE
    Logstash 내장된 Pettern 정보 확인
    참고 URL : https://github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns

    2) Debugger 테스트

    "1)" 에서 확인한 Pattern으로 정상적으로 parsing 되는지 확인
    ① URL 접속( http://grokdebug.herokuapp.com )
    ② 로그 내용 입력
    ③ Pattern 입력 및 확인

    일치하는 Pettern 인 경우 Key:Value 형식으로 출력
    일치하는 Pettern이 없는 경우 No Matches 출력

     

     

    3. Logstash 적용 및 확인

    [참고1] ELK Stack 설치 확인하여 Logstash 기동( https://dragon1.tistory.com/78 )
    [참고2] 윈도우 환경의 Logstash 인 경우 input file path 주의 (역슬래시 사용시 기동안됨 )     

    # access_log
    1.2.3.4 - - [28/Apr/2019:00:01:58 +0900] "GET /assets/css/reset.css HTTP/1.1" 200 1075 "https://www.example.com/login/form.do" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
    # logstash-apache-access-log.conf
    
    input {
      file {
        path => "D:/apps/elk/data/access.log"
        start_position => "beginning"
        codec => plain { charset => "CP949" }
      }
    }
    
    filter {
    	grok { match => { "message" => [ "%{COMBINEDAPACHELOG}" ] } }
    	
    	mutate { remove_field => [ "host" ] }
    }
    
    
    output {
      stdout {
        codec => rubydebug 
      }
    }

    logstash 결과 값

     

    다음회) logstash Grok Pattern 적용(Apache Access log) - 기본편(2)
    사용자 패턴을 만들어 원하는 key:value 만 Parsing

     

    댓글

    Designed by JB FACTORY