elasticsearch - 将日志消息批量上传到本地Elasticsearch

标签 elasticsearch logstash ibm-cloud kibana elastic-stack

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

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

下载的日志如下所示

{"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"}

我在我们的本地 Elasticsearch 中创建了一个索引
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'

但是如果没有 Action ,也会引发相同的错误

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

注意目前我们不使用logstash,但是
  • 是否可以使用logstash并仅使用grok来转换
    日志并添加必要的条目?
  • 如何通过Logstash批量上传文档?
  • 是logstash的理想解决方案,还是我们可以编写一个程序来
    转换并执行

  • 谢谢

    最佳答案

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

    对于logstash:

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

  • 至于您的问题,我认为仅使用一个小程序来添加缺少的 Action 行或使用filebeat就会容易得多,除非您对logstash config进行了足够的实验,以使其比在其中各处添加一行的程序更快地编写和logstash配置文件。

    关于elasticsearch - 将日志消息批量上传到本地Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46677179/

    相关文章:

    Android Native Mobile First 依赖太多库?

    amazon-web-services - AWS Elasticsearch 集群磁盘空间在数据实例之间不平衡

    elasticsearch - ELK数据插入由于类型映射失败而失败,因为实际数据类型发生了更改。

    ruby-on-rails - 多个 Elasticsearch 索引

    performance - Logstash-JDBC 插件 : Importing millions of records from Oracle to ElasticSearch

    mysql - Elasticsearch : "reason": "failed to find geo_point field"

    machine-learning - Watson 机器学习 api - token 刷新 400 错误

    node.js - 在 IBM Bluemix 上执行 Node.js 应用程序期间,我偶尔会收到 502 Bad Gateway

    elasticsearch - 如何将弹性数据从一台服务器传输到另一台服务器

    java - 如何解决[类型删除],不建议在搜索请求中指定类型。“](elasticsearch 7.4.0,spring boot 2.2.6)?