示例:解析通用日志格式的日志

编辑

示例:解析通用日志格式的日志

编辑

在本示例教程中,您将使用一个摄取管道来解析通用日志格式的服务器日志,然后再进行索引。在开始之前,请查看摄取管道的先决条件

您要解析的日志看起来类似于以下内容

212.87.37.154 - - [05/May/2099:16:21:15 +0000] "GET /favicon.ico HTTP/1.1" 200 3638 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"

这些日志包含时间戳、IP地址和用户代理。您希望在 Elasticsearch 中为这三个项目分别创建字段,以便更快地进行搜索和可视化。您还想知道请求来自哪里。

  1. 在 Kibana 中,打开主菜单并点击堆栈管理 > 摄取管道

    Kibana’s Ingest Pipelines list view
  2. 点击创建管道 > 新建管道
  3. 名称设置为my-pipeline,并可选择为管道添加描述。
  4. 添加一个Grok 处理器来解析日志消息

    1. 点击添加处理器并选择Grok处理器类型。
    2. 字段设置为message,并将模式设置为以下Grok 模式

      %{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \[%{HTTPDATE:@timestamp}\] "%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}
    3. 点击添加以保存处理器。
    4. 将处理器描述设置为从“message”中提取字段
  5. 为时间戳、IP地址和用户代理字段添加处理器。按如下方式配置处理器

    处理器类型 字段 其他选项 描述

    日期

    @timestamp

    格式: dd/MMM/yyyy:HH:mm:ss Z

    将“@timestamp”格式化为“dd/MMM/yyyy:HH:mm:ss Z”

    GeoIP

    source.ip

    目标字段: source.geo

    为“source.ip”添加“source.geo”GeoIP 数据

    用户代理

    user_agent

    从“user_agent”中提取字段

    您的表单应类似于以下内容

    Processors for Ingest Pipelines

    这四个处理器将依次运行
    Grok > 日期 > GeoIP > 用户代理
    您可以使用箭头图标重新排序处理器。

    或者,您可以点击导入处理器链接并以 JSON 格式定义处理器

    {
      "processors": [
        {
          "grok": {
            "description": "Extract fields from 'message'",
            "field": "message",
            "patterns": ["%{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \\[%{HTTPDATE:@timestamp}\\] \"%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}\" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}"]
          }
        },
        {
          "date": {
            "description": "Format '@timestamp' as 'dd/MMM/yyyy:HH:mm:ss Z'",
            "field": "@timestamp",
            "formats": [ "dd/MMM/yyyy:HH:mm:ss Z" ]
          }
        },
        {
          "geoip": {
            "description": "Add 'source.geo' GeoIP data for 'source.ip'",
            "field": "source.ip",
            "target_field": "source.geo"
          }
        },
        {
          "user_agent": {
            "description": "Extract fields from 'user_agent'",
            "field": "user_agent"
          }
        }
      ]
    
    }
  6. 要测试管道,请点击添加文档
  7. 文档选项卡中,提供一个用于测试的示例文档

    [
      {
        "_source": {
          "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
        }
      }
    ]
  8. 点击运行管道并验证管道是否按预期工作。
  9. 如果一切看起来都正确,请关闭面板,然后点击创建管道

    您现在可以将日志数据索引到数据流中了。

  10. 创建一个索引模板,并启用数据流

    resp = client.indices.put_index_template(
        name="my-data-stream-template",
        index_patterns=[
            "my-data-stream*"
        ],
        data_stream={},
        priority=500,
    )
    print(resp)
    const response = await client.indices.putIndexTemplate({
      name: "my-data-stream-template",
      index_patterns: ["my-data-stream*"],
      data_stream: {},
      priority: 500,
    });
    console.log(response);
    PUT _index_template/my-data-stream-template
    {
      "index_patterns": [ "my-data-stream*" ],
      "data_stream": { },
      "priority": 500
    }
  11. 使用您创建的管道索引文档。

    resp = client.index(
        index="my-data-stream",
        pipeline="my-pipeline",
        document={
            "message": "89.160.20.128 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
        },
    )
    print(resp)
    const response = await client.index({
      index: "my-data-stream",
      pipeline: "my-pipeline",
      document: {
        message:
          '89.160.20.128 - - [05/May/2099:16:21:15 +0000] "GET /favicon.ico HTTP/1.1" 200 3638 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"',
      },
    });
    console.log(response);
    POST my-data-stream/_doc?pipeline=my-pipeline
    {
      "message": "89.160.20.128 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
    }
  12. 要进行验证,请搜索数据流以检索文档。以下搜索使用filter_path仅返回文档源

    resp = client.search(
        index="my-data-stream",
        filter_path="hits.hits._source",
    )
    print(resp)
    response = client.search(
      index: 'my-data-stream',
      filter_path: 'hits.hits._source'
    )
    puts response
    const response = await client.search({
      index: "my-data-stream",
      filter_path: "hits.hits._source",
    });
    console.log(response);
    GET my-data-stream/_search?filter_path=hits.hits._source

    API 返回

    {
      "hits": {
        "hits": [
          {
            "_source": {
              "@timestamp": "2099-05-05T16:21:15.000Z",
              "http": {
                "request": {
                  "referrer": "\"-\"",
                  "method": "GET"
                },
                "response": {
                  "status_code": 200,
                  "body": {
                    "bytes": 3638
                  }
                },
                "version": "1.1"
              },
              "source": {
                "ip": "89.160.20.128",
                "geo": {
                  "continent_name" : "Europe",
                  "country_name" : "Sweden",
                  "country_iso_code" : "SE",
                  "city_name" : "Linköping",
                  "region_iso_code" : "SE-E",
                  "region_name" : "Östergötland County",
                  "location" : {
                    "lon" : 15.6167,
                    "lat" : 58.4167
                  }
                }
              },
              "message": "89.160.20.128 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"",
              "url": {
                "original": "/favicon.ico"
              },
              "user": {
                "name": "-",
                "id": "-"
              },
              "user_agent": {
                "original": "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"",
                "os": {
                  "name": "Mac OS X",
                  "version": "10.11.6",
                  "full": "Mac OS X 10.11.6"
                },
                "name": "Chrome",
                "device": {
                  "name": "Mac"
                },
                "version": "52.0.2743.116"
              }
            }
          }
        ]
      }
    }