Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 510. Logstash - FileBeat을 이용한 IIS 로그 처리 [링크 복사], [링크+제목 복사],
조회: 12334
글쓴 사람
정성태 (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-..."가 됩니다.
정성태

... 46  47  48  49  [50]  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12388정성태10/29/202010142오류 유형: 674. 어느 순간부터 닷넷 응용 프로그램 실행 시 System.Configuration.ConfigurationErrorsException 예외가 발생한다면?
12387정성태10/28/202010906.NET Framework: 957. C# - static 필드의 정보가 GC Heap에 저장될까요? [3]파일 다운로드1
12386정성태10/28/202011135Linux: 34. 사용자 정보를 함께 출력하는 리눅스의 ps 명령어 사용 방법
12385정성태10/28/20208970오류 유형: 673. openssl - req: No value provided for Subject Attribute CN, skipped
12384정성태10/27/202010171오류 유형: 672. AllowPartiallyTrustedCallers 특성이 적용된 어셈블리의 struct 멤버 메서드를 재정의하면 System.Security.VerificationException 예외 발생
12383정성태10/27/202011051.NET Framework: 956. C# 9.0 - (7) 패턴 일치 개선 사항(Pattern matching enhancements) [3]파일 다운로드1
12382정성태10/26/20208722오류 유형: 671. dotnet build - The local source '...' doesn't exist
12381정성태10/26/202010407VC++: 137. C++ stl map의 사용자 정의 타입을 key로 사용하는 방법 [1]파일 다운로드1
12380정성태10/26/20207857오류 유형: 670. Visual Studio - Squash_FailureCommitsReset
12379정성태10/21/202010836.NET Framework: 955. .NET 메서드의 Signature 바이트 코드 분석 [1]파일 다운로드2
12378정성태10/15/202010299.NET Framework: 954. C# - x86/x64 환경에 따라 달라지는 P/Invoke 함수의 export 이름파일 다운로드1
12377정성태10/15/202011603디버깅 기술: 172. windbg - 파일 열기 시점에 bp를 걸어 파일명 알아내는 방법(Managed/Unmanaged)
12376정성태10/15/20208318오류 유형: 669. windbg - sos의 name2ee 명령어 실행 시 "Failed to request module list." 오류
12375정성태10/15/20209649Windows: 177. 윈도우 탐색기에서 띄우는 cmd.exe 창의 디렉터리 구분 문자가 'Yen(¥)' 기호로 나오는 경우 [1]
12374정성태10/14/202014271.NET Framework: 953. C# 9.0 - (6) 함수 포인터(Function pointers) [1]파일 다운로드2
12373정성태10/14/20209528.NET Framework: 952. OpCodes.Box와 관련해 IL 형식으로 직접 코딩 시 유의할 점
12372정성태10/13/202011429.NET Framework: 951. C# 9.0 - (5) 로컬 함수에 특성 지정 가능(Attributes on local functions)파일 다운로드1
12371정성태10/13/202010189개발 환경 구성: 519. Visual Studio의 Ctrl+Shift+U (Edit.MakeUppercase) 단축키가 동작하지 않는 경우
12370정성태10/13/202011061Linux: 33. Linux - nmcli를 이용한 고정 IP 설정
12369정성태10/12/202013855Windows: 176. Raymond Chen이 한글날에 밝히는 윈도우의 한글 자모 분리 현상 [3]
12368정성태10/12/20209925오류 유형: 668. VSIX 확장 빌드 - The "GetDeploymentPathFromVsixManifest" task failed unexpectedly.
12367정성태10/12/202022699오류 유형: 667. Ubuntu - Temporary failure resolving 'kr.archive.ubuntu.com' [2]
12366정성태10/12/202011677.NET Framework: 950. C# 9.0 - (4) 원시 크기 정수(Native ints) [1]파일 다운로드1
12365정성태10/12/202010599.NET Framework: 949. C# 9.0 - (3) 람다 메서드의 매개 변수 무시(Lambda discard parameters)파일 다운로드1
12364정성태10/11/202011796.NET Framework: 948. C# 9.0 - (2) localsinit 플래그 내보내기 무시(Suppress emitting localsinit flag)파일 다운로드1
12363정성태10/11/202012695.NET Framework: 947. C# 9.0 - (1) 대상으로 형식화된 new 식(Target-typed new expressions) [2]파일 다운로드1
... 46  47  48  49  [50]  51  52  53  54  55  56  57  58  59  60  ...