2017-10-10 42 views
0

我们在云中有几个外部应用程序(IBM Bluemix),它将应用程序syslog记录在内部使用ELK堆栈的bluemix logmet服务中。批量上传日志消息到本地Elasticsearch

现在我们定期从云端下载日志并将其上传到本地的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"} 

我在我们当地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' 

,但没有行动,这也引发了同样的错误

问题是,我们不能这样做为我们得到的数千条日志记录。是否有一种选择,即我们从Bluemix下载日志文件并上传文件而不添加操作。

注意我们不使用logstash的那一刻,但

  • 是有可能使用logstash,只需使用神交改造 日志,并添加必要的条目?
  • 我们如何通过Logstash批量上传文件?

  • 是logstash理想的解决方案,或者我们可以只编写一个程序来 改造和做

感谢

+1

Filebeat应该能够将json日志直接写入您的本地elasticsearch。 –

+0

谢谢@AlainCollins。我确实尝试了Filebeats,并能够将日志直接上传到ES中。 – Tatha

回答

1

正如@Alain柯林斯说,你应该能够直接filebeat使用。

对于logstash:

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

至于你的问题,我认为这将是很容易只使用一个小程序,添加缺少的行动路线或使用filebeat,除非你与logstash配置足够的尝试编写和logstash配置比快程序在文档的每个地方添加一行。

+0

感谢您提出多种解决方案。我尝试了两种选择(小脚本和filebeats),并能够用它上传日志。通过文件节拍,需要更多配置来支持相同的索引和类型,而不是使用filebeat的默认索引。我暂时决定使用脚本sed'h; s /.*/ {“index”:{“_index”:“commslog”,“_type”:“logs”}} /; G'logs .json在每行之前添加索引行。一旦我们计划自动化这个过程,我们可以使用filebeats。谢谢 – Tatha

+0

@塔塔不客气。虽然我同意可以使用logstash或filebeat,但我认为使用简单的解决方案会更好。 – baudsp