logstash收集日志时,日志部分是由google的protobuf工具打印的,直接利用json解析会造成部分无法解析的问题
搜索后发现有个logstash的protobuf插件
在logstash中添加protobuf
首先需要下载一个用于解码protobuf的插件,
安装插件
从rubygems下载gemfile。在Logstash目录中,执行以下命令:
1
| bin/plugin install PATH_TO_DOWNLOADED FILE
|
创建protobuf定义的Ruby脚本
示例下面的unicorn.pb是您要解码消息的现有protobuf定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package Animal;
message Unicorn {
// colour of unicorn optional string colour = 1;
// horn length optional int32 horn_length = 2;
// unix timestamp for last observation optional int64 last_seen = 3;
}
|
需要下载uby-protocol编译器。然后运行
编译器将创建一个扩展名为.rb的新文件,如unicorn.rb.pb。它包含一个Ruby版本的定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #!/usr/bin/env ruby # Generated by the protocol buffer compiler. DO NOT EDIT!
require 'protocol_buffers'
module Animal # forward declarations class Unicorn < ::ProtocolBuffers::Message; end
class Unicorn < ::ProtocolBuffers::Message set_fully_qualified_name "animal.Unicorn"
optional :string, :colour, 1 optional :int32, :horn_length, 2 optional :int64, :last_seen, 3 end
end
|
现在,需要通过在配置中提供其位置来使该文件已知到Logstash。
Logstash配置
可以在Logstash的任何输入源中使用编解码器。在这个例子中,我们将使用Kafka作为数据源。用于读取protobuf类Unicorn消息的配置如下所示:
1 2 3 4 5 6 7 8 9 10
| kafka { zk_connect => "127.0.0.1" topic_id => "unicorns_protobuffed" codec => protobuf { class_name => "Animal::Unicorn" include_path => ['/path/to/compiled/protobuf/definitions/unicorn.pb.rb'] } }
|
在GitHub的文档中可以找到一个更复杂的例子。
这是一个官方给出的实例文件, 将这个配置直接应用到我处理的日志还是有点问题,还需要更改