【发布时间】: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