• Beats: 使用 Filebeat 进行日志结构化


    文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/106688240

    生产一个叫做 json_logs 的文件:

    {"user_name": "arthur", "id": 42, "verified": false, "event": "logged_in"}
    {"user_name": "arthur", "id": 42, "verified": true, "event": "changed_state"}
    

    这种 JSON 格式的文件人眼难以理解,但是具有的优点是,数据已经按照 Elasticsearch 喜欢的格式进行了结构化。

    Filebeat 是一个用Go语言编写的开源日志传送器,可以将日志行发送到Logstash和Elasticsearch。 它提供了“至少一次”保证的数据传输,因此你永远不会丢失日志行,并且它使用了背压敏感协议,因此不会使你的管道过载。 还包括基本过滤和多行关联。

    如果你的日志就像上面的示例一样,Filebeat 每行存储一个JSON对象,它还可以本地解码 JSON 对象。

    这是一个示例配置文件,该文件配置Filebeat来拾取文件并将 JSON 对象发送到 Elasticsearch:

    filebeat_json.yml

    filebeat.inputs:
    - type: log
      enabled: true
      tags: ["i", "love", "json"]
      json.message_key: event
      json.keys_under_root: true
      json.add_error_key: true
      fields:
        planet: liuxg
      paths:
        - /Users/liuxg/python/logs/json_logs
     
    output.elasticsearch:
      hosts: ["localhost:9200"]
      index: "json_logs1"
     
    setup.ilm.enabled: false
    setup.template.name: json_logs1
    setup.template.pattern: json_logs1
    

    在运行 Filebeat 之前,我们可以在 Kibana 中执行如下的命令:

    PUT json_logs1
    {
      "mappings": {
        "properties": {
          "event": {
            "type": "keyword"
          },
          "id": {
            "type": "long"
          },
          "user_name": {
            "type": "keyword"
          },
          "verified": {
            "type": "boolean"
          }
        }
      }
    }  
    

    在这里,我们定义这个索引的 mapping。

    我们接着执行运行 Filebeat:

    ./filebeat -e -c ~/python/logs/filebeat_json.yml

    那么在我们的 Kibana 中,我们可以查询到最新生成的文档:

    GET json_logs1/_search

    {
      "took" : 0,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "json_logs1",
            "_type" : "_doc",
            "_id" : "uw-jonIBB4HethT_mOIZ",
            "_score" : 1.0,
            "_source" : {
              "@timestamp" : "2020-06-11T09:08:48.550Z",
              "user_name" : "arthur",
              "verified" : false,
              "tags" : [
                "i",
                "love",
                "json"
              ],
              "fields" : {
                "planet" : "liuxg"
              },
              "ecs" : {
                "version" : "1.5.0"
              },
              "agent" : {
                "ephemeral_id" : "0c9b96dd-76c8-45c5-96ef-00859f9e12dc",
                "hostname" : "liuxg",
                "id" : "be15712c-94be-41f4-9974-0b049dc95750",
                "version" : "7.7.0",
                "type" : "filebeat"
              },
              "id" : 42,
              "event" : "logged_in",
              "log" : {
                "offset" : 0,
                "file" : {
                  "path" : "/Users/liuxg/python/logs/json_logs"
                }
              },
              "input" : {
                "type" : "log"
              },
              "host" : {
                "name" : "liuxg"
              }
            }
          },
          {
            "_index" : "json_logs1",
            "_type" : "_doc",
            "_id" : "vA-jonIBB4HethT_mOIZ",
            "_score" : 1.0,
            "_source" : {
              "@timestamp" : "2020-06-11T09:08:48.551Z",
              "event" : "changed_state",
              "input" : {
                "type" : "log"
              },
              "log" : {
                "offset" : 75,
                "file" : {
                  "path" : "/Users/liuxg/python/logs/json_logs"
                }
              },
              "user_name" : "arthur",
              "id" : 42,
              "verified" : true,
              "tags" : [
                "i",
                "love",
                "json"
              ],
              "fields" : {
                "planet" : "liuxg"
              },
              "ecs" : {
                "version" : "1.5.0"
              },
              "host" : {
                "name" : "liuxg"
              },
              "agent" : {
                "version" : "7.7.0",
                "type" : "filebeat",
                "ephemeral_id" : "0c9b96dd-76c8-45c5-96ef-00859f9e12dc",
                "hostname" : "liuxg",
                "id" : "be15712c-94be-41f4-9974-0b049dc95750"
              }
            }
          }
        ]
      }
    }
    

    如你所见,Filebeat 自动添加一个时间戳。 请注意,这是读取日志行的时间,可能与应用程序写入日志行的时间不同。 如果需要更好的准确性,可以设置 structlog 库以生成时间戳。

    Filebeat 还会自动添加一些元数据(例如主机名),并使其易于通过配置文件添加自定义字段和标签。 这意味着应用程序不必担心从环境中添加元数据。

  • 相关阅读:
    ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
    大div套多个小div,怎样设置外div的高度自适应?
    搭建高可用rabbitmq集群及spring boot实现集群配置
    linux signal 处理
    freemarker null异常详解及兼容模式
    蓝绿部署、A/B测试以及灰度发布(金丝雀发布)
    动态BGP与静态BGP
    axios与ajax的区别及中文用户指南
    java poi excel给单元格增加批注(包含SXSSF)及设置列类型
    java图片处理(加水印、生成缩略图)等之Thumbnailator库
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/13139613.html
Copyright © 2020-2023  润新知