Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 510. Logstash - FileBeat을 이용한 IIS 로그 처리 [링크 복사], [링크+제목 복사],
조회: 12235
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

Logstash - FileBeat을 이용한 IIS 로그 처리

이제 제법 Logstash에 익숙해졌으니,

Logstash 기본 사용법
; https://www.sysnet.pe.kr/2/0/12312

Logstash - 사용자 정의 grok 패턴 추가를 이용한 IIS 로그 처리
; https://www.sysnet.pe.kr/2/0/12313

근래에 더 현실적으로 사용되는 Beat -> Logstash -> Elasticsearch (또는, Beat -> Elasticsearch) 구조를 살펴보겠습니다. 그나저나 Beat는 "수집"만을 전용으로 담당하는 경량화된 모듈이라고 보면 되는데, 데이터의 성격상 여러 Beat 제품으로 나뉘어 있습니다.

The Beats family
; https://www.elastic.co/downloads/beats

  • FileBeat
  • Packetbeat
  • Winlogbeat
  • Metricbeat
  • Heartbeat
  • Auditbeat
  • Functionbeat
  • Journalbeat

모든 걸 여기서 다룰 수는 없고, 이 글에서는 IIS 로그가 File로 남겨지기 때문에 이를 위한 Filebeat를 사용해 실습을 해보겠습니다.




자, 그럼 먼저 웹 서버 측에 Filebeat를 다운로드해야겠지요.

Download Filebeat (약 24MB)
; https://www.elastic.co/downloads/beats/filebeat

압축 해제 후, 시작은 .\filebeat.exe를 실행하면 되는데 그전에 yml 파일을 통해 설정을 좀 해야 합니다.

  • fields.yml
  • filebeat.yml
  • filebeat.reference.yml

이 중에서 filebeat.reference.yml, fields.yml은 일종의 레퍼런스 문서 역할을 하니 나중에 천천히 보시고, 실질적인 설정을 담당하는 filebeat.yml을 열어 봅니다. (아래는 기본 설치된 filebeat.yml의 주석을 제거한 설정값들을 정리한 것입니다.)

filebeat.inputs:

- type: log

  enabled: false
  paths:
    - /var/log/*.log

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

setup.template.settings:
  index.number_of_shards: 1

setup.kibana:

output.elasticsearch:
  hosts: ["localhost:9200"]

processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

우선 "filebeat.config.modules"의 "path"인 "${path.config}/modules.d" 폴더를 보면 Filebeat이 제공하는 다양한 모듈들에 대한 개별 설정을 담고 있지만 확장자가 ".disabled"로 끝나기 때문에 filebeat이 로드해야 할 모듈(*.yml)에 포함되지 않습니다. 따라서, IIS 모듈이 동작하려면 "${path.config}/modules.d/iis.yml.disabled" 파일의 확장자인 ".disabled"를 제거하고 그 안의 내용을,
# Module: iis
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-module-iis.html

- module: iis
  # Access logs
  access:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

  # Error logs
  error:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    #var.paths:

문서에 따라 "access"의 "var.paths"와 "error"의 "var.paths"를 각각 여러분들의 환경에 맞게 설정/저장해야 합니다. 가령 제 경우에는 다음과 같이 변경했습니다.

- module: iis
  access:
    enabled: true
    var.paths: [ "C:\\inetpub\\logs\\LogFiles\\*\\*.log" ]

  error:
    enabled: true
    var.paths: [ "C:\\Windows\\System32\\LogFiles\\HTTPERR\\*.log" ]

여기까지 설정하고 filebeat.exe을 실행하면 "localhost:9200"의 elasticsearch로 IIS 로그를 보내게 됩니다.




약간의 디버깅 용도라고 하면, elasticsearch에 어떤 데이터를 넘기는지 확인하기 위해 logstash를 사용할 수 있습니다. "Logstash 기본 사용법" 글의 설명에 따라, logstash의 파이프라인을 다음과 같이 설정하고,

input {
    beats {
        port => "5044"
    }
}

output {
    stdout { }
}

Filebeats의 iis.yml에 설정된 출력을 Logstash 쪽으로 바꿔주면,

output.logstash:
  hosts: ["localhost:5044"]

예를 들어, C:\Windows\System32\LogFiles\HTTPERR 경로에 에러 항목이 다음과 같은 유형으로 포함되어 있는 경우,

#Software: Microsoft HTTP API 2.0
#Version: 1.0
#Date: 2020-09-02 12:52:59
#Fields: date time c-ip c-port s-ip s-port cs-version cs-method cs-uri streamid sc-status s-siteid s-reason s-queuename
2020-09-07 04:34:46 192.168.0.7 53858 192.168.100.50 8020 - - - - - - Timer_MinBytesPerSecond -

Logstash 콘솔 출력에 이런 내용을 확인할 수 있습니다.

{
    "@timestamp" => 2020-09-07T04:34:55.340Z,
          "host" => {
        "architecture" => "x86_64",
                  "ip" => [
            [0] "fe80::38cd:62c9:a8a4:d20a",
            [1] "192.168.100.50"
        ],
            "hostname" => "testpc",
                 "mac" => [
            [0] "00:15:5d:00:05:2f"
        ],
                  "id" => "102f9250-5ebd-465d-b42b-b4cd8c09f0f3",
                  "os" => {
               "build" => "17763.1397",
              "kernel" => "10.0.17763.1397 (WinBuild.160101.0800)",
              "family" => "windows",
            "platform" => "windows",
             "version" => "10.0",
                "name" => "Windows Server 2019 Standard"
        },
                "name" => "testpc"
    },
         "event" => {
         "module" => "iis",
        "dataset" => "iis.error"
    },
       "fileset" => {
        "name" => "error"
    },
      "@version" => "1",
         "agent" => {
        "ephemeral_id" => "a533d7f7-b4b8-4ee8-abe1-426091f010ca",
                "type" => "filebeat",
            "hostname" => "testpc",
                  "id" => "c16e0429-6ffa-4f17-afa4-32be234ffdbb",
             "version" => "7.9.1",
                "name" => "testpc"
    },
          "tags" => [
        [0] "beats_input_codec_plain_applied"
    ],
       "service" => {
        "type" => "iis"
    },
           "ecs" => {
        "version" => "1.5.0"
    },
         "input" => {
        "type" => "log"
    },
       "message" => "2020-09-07 04:34:46 192.168.0.7 53858 192.168.100.50 8020 - - - - - - Timer_MinBytesPerSecond -",
           "log" => {
        "offset" => 370319,
          "file" => {
            "path" => "C:\\Windows\\System32\\LogFiles\\HTTPERR\\httperr13.log"
        }
    }
}

그런데, 보는 바와 같이 beats가 보내주는 내용이 꽤 많으므로 만약 이 내용을 그대로 Elasticsearch에 보내면 용량이 꽤나 부담스러울 것입니다. (대부분 mutate/remove_field의 조합으로 잘라내고 싶겠죠. ^^ 그래서 현실적인 이유로 봤을 때 "Beats -> Logstash -> Elasticsearch" 구조가 주로 사용될 것입니다.)




그나저나, logstash가 Filebeat로 받은 데이터를 보면 중간에 beats_input_codec_plain_applied 항목을 담은 "tags"가 있습니다.

{
    "@timestamp" => 2020-09-07T04:37:39.988Z,
         "event" => {
         "module" => "iis",
        "dataset" => "iis.access"
    },
         ...[생략]...
    },
          "tags" => [
        [0] "beats_input_codec_plain_applied"]
    ],
       "service" => {
        "type" => "iis"
    },
         ...[생략]...
}

이전의 _grokparsefailure, _geoip_lookup_failure 경험을 보면 tags에 뭔가 메시지가 있다는 것은 부정적인 의미로 비치는데요, 이에 대해 검색을 해보면,

The tag “beats_input_codec_plain_applied” present in every document in Kibana
; https://stackoverflow.com/questions/61782692/the-tag-beats-input-codec-plain-applied-present-in-every-document-in-kibana

다행히도, Beats 구성 요소가 생성하는 메시지는 전부 저 항목을 포함하고 있다고 합니다. 따라서, (그냥 무시해도 되지만) 이 항목을 (elasticsearch 서버의 디스크 공간을 절약하기 위해) 없애려면 mutate/remove_field로 잘라내거나,

if "beats_input_codec_plain_applied" in [tags] {
    mutate {
        remove_tag => ["beats_input_codec_plain_applied"]
    }
}

아니면, input 단계에서 include_codec_tag 옵션을 추가할 수 있습니다.

input {
    beats {
        port => "5044"
        include_codec_tag => false
    }
}




참고로, filebeat 실행 시 다음과 같은 식의 오류가 발생한다면?

C:\beat\filebeat> filebeat.exe
Exiting: Failed to start crawler: creating module reloader failed: 1 error: invalid config: yaml: line 11: found unknown escape character
loading configs
github.com/elastic/beats/v7/libbeat/cfgfile.(*Reloader).Check
        /go/src/github.com/elastic/beats/libbeat/cfgfile/reload.go:143
github.com/elastic/beats/v7/filebeat/beater.(*crawler).Start
        /go/src/github.com/elastic/beats/filebeat/beater/crawler.go:91
github.com/elastic/beats/v7/filebeat/beater.(*Filebeat).Run
        /go/src/github.com/elastic/beats/filebeat/beater/filebeat.go:438
github.com/elastic/beats/v7/libbeat/cmd/instance.(*Beat).launch
        /go/src/github.com/elastic/beats/libbeat/cmd/instance/beat.go:456
github.com/elastic/beats/v7/libbeat/cmd/instance.Run.func1
        /go/src/github.com/elastic/beats/libbeat/cmd/instance/beat.go:189
github.com/elastic/beats/v7/libbeat/cmd/instance.Run
        /go/src/github.com/elastic/beats/libbeat/cmd/instance/beat.go:190
github.com/elastic/beats/v7/libbeat/cmd.genRunCmd.func1
        /go/src/github.com/elastic/beats/libbeat/cmd/run.go:36
github.com/spf13/cobra.(*Command).execute
        /go/pkg/mod/github.com/spf13/cobra@v0.0.3/command.go:766
github.com/spf13/cobra.(*Command).ExecuteC
        /go/pkg/mod/github.com/spf13/cobra@v0.0.3/command.go:852
github.com/spf13/cobra.(*Command).Execute
        /go/pkg/mod/github.com/spf13/cobra@v0.0.3/command.go:800
main.main
        /go/src/github.com/elastic/beats/x-pack/filebeat/main.go:22
runtime.main
        /usr/local/go/src/runtime/proc.go:203
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1373

filebeat.yml이나 각 module의 yml 설정에서 파일 경로에 대한 구분자로 단일 '\' 문자를 사용했는지 확인해 봅니다.

var.paths: [ "C:\inetpub\logs\LogFiles\*\*.log" ]

저렇게 하면 escape 문자로 취급하기 때문에 반드시 2개 연속으로 디렉터리 구분자를 사용해야 합니다.

var.paths: [ "C:\\inetpub\\logs\\LogFiles\\*\\*.log" ]

또는, *nix 식 구분자를 사용하거나.

var.paths: [ "C:/inetpub/logs/LogFiles/*/*.log" ]




마지막으로, 혹시 docker나 k8s에 대한 Beats의 활용이 궁금하신 분은 다음의 영상 강좌를 참고해 보는 것도 좋겠습니다.

Beats 및 Elasticsearch를 이용한 Docker & Kubernetes 로그 수집 및 모니터링
; https://www.elastic.co/kr/webinars/elasticsearch-log-collection-with-kubernetes-docker-and-containers/?view=1





[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 9/8/2020]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2020-09-09 11시22분
참고로, FileBeat를 NT 서비스로 구동하고 싶다거나 한다면?

cd "C:\Program Files\Filebeat"
.\install-service-filebeat.ps1
정성태
2020-09-10 10시23분
Filebeat로 Logstash를 경유해 Elasticsearch에 저장하는 경우 인덱스 이름이 Logstash-...로 시작하지만, Logstash를 경유하지 않으면 "filebeat-..."가 됩니다.
정성태

... 16  17  18  19  20  21  22  23  24  25  26  27  28  [29]  30  ...
NoWriterDateCnt.TitleFile(s)
12909정성태1/10/20228150오류 유형: 782. Visual Studio 2022 설치 시 "Couldn't install Microsoft.VisualCpp.Redist.14.Latest"
12908정성태1/10/20225986.NET Framework: 1132. C# - ref/out 매개변수의 IL 코드 처리
12907정성태1/9/20226513오류 유형: 781. (youtube-dl.exe) 실행 시 "This app can't run on your PC" / "Access is denied." 오류 발생
12906정성태1/9/20227126.NET Framework: 1131. C# - 네임스페이스까지 동일한 타입을 2개의 DLL에서 제공하는 경우 충돌을 우회하는 방법 [1]파일 다운로드1
12905정성태1/8/20226792오류 유형: 780. Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.VSHost.15.0, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
12904정성태1/8/20228780개발 환경 구성: 623. Visual Studio 2022 빌드 환경을 위한 github Actions 설정 [1]
12903정성태1/7/20227377.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예
12902정성태1/7/20227432오류 유형: 779. SQL 서버 로그인 에러 - provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.
12901정성태1/5/20227459오류 유형: 778. C# - .NET 5+에서 warning CA1416: This call site is reachable on all platforms. '...' is only supported on: 'windows' 경고 발생
12900정성태1/5/20229154개발 환경 구성: 622. vcpkg로 ffmpeg를 빌드하는 경우 생성될 구성 요소 제어하는 방법
12899정성태1/3/20228646개발 환경 구성: 621. windbg에서 python 스크립트 실행하는 방법 - pykd (2)
12898정성태1/2/20229215.NET Framework: 1129. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 비디오 인코딩 예제(encode_video.c) [1]파일 다운로드1
12897정성태1/2/20228043.NET Framework: 1128. C# - 화면 캡처한 이미지를 ffmpeg(FFmpeg.AutoGen)로 동영상 처리 [4]파일 다운로드1
12896정성태1/1/202210950.NET Framework: 1127. C# - FFmpeg.AutoGen 라이브러리를 이용한 기본 프로젝트 구성파일 다운로드1
12895정성태12/31/20219405.NET Framework: 1126. C# - snagit처럼 화면 캡처를 연속으로 수행해 동영상 제작 [1]파일 다운로드1
12894정성태12/30/20217369.NET Framework: 1125. C# - DefaultObjectPool<T>의 IDisposable 개체에 대한 풀링 문제 [3]파일 다운로드1
12893정성태12/27/20218964.NET Framework: 1124. C# - .NET Platform Extension의 ObjectPool<T> 사용법 소개파일 다운로드1
12892정성태12/26/20216966기타: 83. unsigned 형의 이전 값이 최댓값을 넘어 0을 지난 경우, 값의 차이를 계산하는 방법
12891정성태12/23/20216887스크립트: 38. 파이썬 - uwsgi의 --master 옵션
12890정성태12/23/20217015VC++: 152. Golang - (문자가 아닌) 바이트 위치를 반환하는 strings.IndexRune 함수
12889정성태12/22/20219427.NET Framework: 1123. C# - (SharpDX + DXGI) 화면 캡처한 이미지를 빠르게 JPG로 변환하는 방법파일 다운로드1
12888정성태12/21/20217546.NET Framework: 1122. C# - ImageCodecInfo 사용 시 System.Drawing.Image와 System.Drawing.Bitmap에 따른 Save 성능 차이파일 다운로드1
12887정성태12/21/20219660오류 유형: 777. OpenCVSharp4를 사용한 프로그램 실행 시 "The type initializer for 'OpenCvSharp.Internal.NativeMethods' threw an exception." 예외 발생
12886정성태12/20/20217551스크립트: 37. 파이썬 - uwsgi의 --enable-threads 옵션 [2]
12885정성태12/20/20217808오류 유형: 776. uwsgi-plugin-python3 환경에서 MySQLdb 사용 환경
12884정성태12/20/20216856개발 환경 구성: 620. Windows 10+에서 WMI root/Microsoft/Windows/WindowsUpdate 네임스페이스 제거
... 16  17  18  19  20  21  22  23  24  25  26  27  28  [29]  30  ...