【问题标题】:Bulk upload log messages to local Elasticsearch将日志消息批量上传到本地 Elasticsearch
【发布时间】:2017-10-10 22:50:13
【问题描述】:

我们在云 (IBM Bluemix) 中有一些外部应用程序,它们将其应用程序系统日志记录在内部使用 ELK 堆栈的 bluemix logmet 服务中。

现在,我们希望定期从云端下载日志并将其上传到本地 Elastic/Kibana 实例。这是因为如果我们想通过 Kibana 搜索日志,将日志存储在云服务中会产生成本和额外成本。本地弹性实例可以删除/刷新我们不需要的旧日志。

下载的日志如下所示

{"instance_id_str":"0","source_id_str":"APP/PROC/WEB","app_name_str":"ABC","message":"Hello","type":"syslog","event_uuid":"474b78aa-6012-44f3-8692-09bd667c5822","origin_str":"rep","ALCH_TENANT_ID":"3213cd20-63cc-4592-b3ee-6a204769ce16","logmet_cluster":"topic3-elasticsearch_3","org_name_str":"123","@timestamp":"2017-09-29T02:30:15.598Z","message_type_str":"OUT","@version":"1","space_name_str":"prod","application_id_str":"3104b522-aba8-48e0-aef6-6291fc6f9250","ALCH_ACCOUNT_ID_str":"","org_id_str":"d728d5da-5346-4614-b092-e17be0f9b820","timestamp":"2017-09-29T02:30:15.598Z"}

{"instance_id_str":"0","source_id_str":"APP/PROC/WEB","app_name_str":"ABC","message":"EFG","type":"syslog","event_uuid":"d902dddb-afb7-4f55-b472-211f1d370837","origin_str":"rep","ALCH_TENANT_ID":"3213cd20-63cc-4592-b3ee-6a204769ce16","logmet_cluster":"topic3-elasticsearch_3","org_name_str":"123","@timestamp":"2017-09-29T02:30:28.636Z","message_type_str":"OUT","@version":"1","space_name_str":"prod","application_id_str":"dcd9f975-3be3-4451-a9db-6bed1d906ae8","ALCH_ACCOUNT_ID_str":"","org_id_str":"d728d5da-5346-4614-b092-e17be0f9b820","timestamp":"2017-09-29T02:30:28.636Z"}

我在我们的本地弹性搜索中创建了一个索引

curl -XPUT 'localhost:9200/commslog?pretty' -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "logs" : {
            "properties" : {
                "instance_id_str" : { "type" : "text" },
                "source_id_str" : { "type" : "text" },
                "app_name_str" : { "type" : "text" },
                "message" : { "type" : "text" },
                "type" : { "type" : "text" },
                "event_uuid" : { "type" : "text" },
                "ALCH_TENANT_ID" : { "type" : "text" },
                "logmet_cluster" : { "type" : "text" },
                "org_name_str" : { "type" : "text" },
                "@timestamp" : { "type" : "date" },
                "message_type_str" : { "type" : "text" },
                "@version" : { "type" : "text" },
                "space_name_str" : { "type" : "text" },
                "application_id_str" : { "type" : "text" },
                "ALCH_ACCOUNT_ID_str" : { "type" : "text" },
                "org_id_str" : { "type" : "text" },
                "timestamp" : { "type" : "date" }
            }
        }
    }
}'

现在要批量上传文件,使用命令

curl -XPOST -H 'Content-Type: application/x-ndjson' http://localhost:9200/commslog/logs/_bulk --data-binary '@commslogs.json'

以上命令报错

动作/元数据行 [1] 格式错误,应为 START_OBJECT 或 END_OBJECT,但找到 [VALUE_STRING]

解决办法是按照批量上传规则

https://discuss.elastic.co/t/bulk-insert-file-having-many-json-entries-into-elasticsearch/46470/2

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

所以我通过在每一行之前添加操作手动更改了一些日志语句

{ "index" : { "_index" : "commslog", "_type" : "logs" } } 

这行得通!。

另一个选项是调用 curl 命令,在路径中提供 _idex 和 _type

curl -XPOST -H 'Content-Type: application/x-ndjson' http://localhost:9200/commslog/logs/_bulk --data-binary '@commslogs.json'

但如果没有该操作,这也会引发相同的错误

问题是我们无法对我们获得的数千条日志记录执行此操作。是否有一个选项,一旦我们从 Bluemix 下载日志文件并上传文件而不添加操作。

注意我们目前没有使用logstash,但是

  • 是否可以使用 logstash 并只使用 grok 来转换 记录并添加必要的条目?
  • 我们如何通过 Logstash 批量上传文档?

  • logstash 是理想的解决方案还是我们可以编写一个程序 转换并做到这一点

谢谢

【问题讨论】:

  • Filebeat 应该能够将 json 日志直接写入本地 elasticsearch。
  • 谢谢@AlainCollins。我确实尝试过 Filebeats,并且能够将日志直接上传到 ES。

标签: elasticsearch logstash ibm-cloud kibana elastic-stack


【解决方案1】:

正如@Alain Collins 所说,您应该可以直接使用 filebeat。

对于logstash:

  • 应该可以使用logstash,但你应该使用json编解码器/过滤器,而不是使用grok,它会容易。
  • 您可以使用带有logstash 的文件输入来处理许多文件并等待它完成(要知道它何时完成,请使用文件/标准输出,可能带有点编解码器,然后等待它停止写入)。
  • 您应该直接上传到elasticsearch(使用elasticsearch 输出),而不是仅仅使用logstash 转换文件。

至于您的问题,我认为只使用一个小程序来添加缺少的操作行或使用 filebeat 会容易得多,除非您对 logstash 配置进行了足够的试验,以比添加一个程序更快地编写和配置 logstash在文档中的任何地方都行。

【讨论】:

  • 感谢您提出多种解决方案。我尝试了这两个选项(小脚本和文件节拍)并且能够使用它上传日志。对于文件节拍,需要更多配置来支持相同的索引和类型,而不是使用文件节拍的默认索引。我暂时决定使用脚本 sed 'h;s/.*/{ "index" : { "_index" : "commslog", "_type" : "logs" } }/;G' logs .json在每行之前添加索引行。一旦我们计划自动化这个过程,我们就可以使用 filebeats。谢谢
  • @Tatha 不客气。虽然我同意可以使用 logstash 或 filebeat,但我认为您最好使用简单的解决方案。
猜你喜欢
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
相关资源
最近更新 更多