logstash-codec-collectd_json 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/CONTRIBUTORS +17 -0
- data/Gemfile +2 -0
- data/LICENSE +13 -0
- data/NOTICE.TXT +5 -0
- data/README.md +86 -0
- data/lib/logstash/codecs/json.rb +73 -0
- data/logstash-codec-collectd_json.gemspec +27 -0
- data/spec/codecs/json_spec.rb +166 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cee57e57cbf93ea7d22f91958996b349a4ee07ff
|
4
|
+
data.tar.gz: 1ad6b98c9bb2549c0d42184cf69b64e46fab647a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b77bc42a06fc6af434a2b1cc2f7a773b665e723c44a5d7fa8b479bdddaa5343660d10a3527a0dda86ad412b00da86e9fcf637b66d2b7f9541c719c455bc2115d
|
7
|
+
data.tar.gz: cbd1663a3dfc9d298d066c7ca34f9a96c2c0a25d595a4f32db97aa96fabf7faba976750ad9ce36f2aed48ffa81663fd7cc5c00aa3054287a89f18c51ec3c744b
|
data/CHANGELOG.md
ADDED
data/CONTRIBUTORS
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
The following is a list of people who have contributed ideas, code, bug
|
2
|
+
reports, or in general have helped logstash along its way.
|
3
|
+
|
4
|
+
Contributors:
|
5
|
+
* Colin Surprenant (colinsurprenant)
|
6
|
+
* Jordan Sissel (jordansissel)
|
7
|
+
* João Duarte (jsvd)
|
8
|
+
* Kurt Hurtado (kurtado)
|
9
|
+
* Nick Ethier (nickethier)
|
10
|
+
* Pier-Hugues Pellerin (ph)
|
11
|
+
* Richard Pijnenburg (electrical)
|
12
|
+
* Tal Levy (talevy)
|
13
|
+
|
14
|
+
Note: If you've sent us patches, bug reports, or otherwise contributed to
|
15
|
+
Logstash, and you aren't on the list above and want to be, please let us know
|
16
|
+
and we'll make sure you're here. Contributions from folks like you are what make
|
17
|
+
open source awesome.
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/NOTICE.TXT
ADDED
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Logstash Plugin
|
2
|
+
|
3
|
+
This is a plugin for [Logstash](https://github.com/elastic/logstash).
|
4
|
+
|
5
|
+
It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
|
6
|
+
|
7
|
+
## Documentation
|
8
|
+
|
9
|
+
Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
|
10
|
+
|
11
|
+
- For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
|
12
|
+
- For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
|
13
|
+
|
14
|
+
## Need Help?
|
15
|
+
|
16
|
+
Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
|
17
|
+
|
18
|
+
## Developing
|
19
|
+
|
20
|
+
### 1. Plugin Developement and Testing
|
21
|
+
|
22
|
+
#### Code
|
23
|
+
- To get started, you'll need JRuby with the Bundler gem installed.
|
24
|
+
|
25
|
+
- Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
|
26
|
+
|
27
|
+
- Install dependencies
|
28
|
+
```sh
|
29
|
+
bundle install
|
30
|
+
```
|
31
|
+
|
32
|
+
#### Test
|
33
|
+
|
34
|
+
- Update your dependencies
|
35
|
+
|
36
|
+
```sh
|
37
|
+
bundle install
|
38
|
+
```
|
39
|
+
|
40
|
+
- Run tests
|
41
|
+
|
42
|
+
```sh
|
43
|
+
bundle exec rspec
|
44
|
+
```
|
45
|
+
|
46
|
+
### 2. Running your unpublished Plugin in Logstash
|
47
|
+
|
48
|
+
#### 2.1 Run in a local Logstash clone
|
49
|
+
|
50
|
+
- Edit Logstash `Gemfile` and add the local plugin path, for example:
|
51
|
+
```ruby
|
52
|
+
gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
|
53
|
+
```
|
54
|
+
- Install plugin
|
55
|
+
```sh
|
56
|
+
bin/plugin install --no-verify
|
57
|
+
```
|
58
|
+
- Run Logstash with your plugin
|
59
|
+
```sh
|
60
|
+
bin/logstash -e 'filter {awesome {}}'
|
61
|
+
```
|
62
|
+
At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
|
63
|
+
|
64
|
+
#### 2.2 Run in an installed Logstash
|
65
|
+
|
66
|
+
You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
|
67
|
+
|
68
|
+
- Build your plugin gem
|
69
|
+
```sh
|
70
|
+
gem build logstash-filter-awesome.gemspec
|
71
|
+
```
|
72
|
+
- Install the plugin from the Logstash home
|
73
|
+
```sh
|
74
|
+
bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
|
75
|
+
```
|
76
|
+
- Start Logstash and proceed to test the plugin
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
|
81
|
+
|
82
|
+
Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
|
83
|
+
|
84
|
+
It is more important to the community that you are able to contribute.
|
85
|
+
|
86
|
+
For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "logstash/codecs/base"
|
3
|
+
require "logstash/util/charset"
|
4
|
+
require "logstash/json"
|
5
|
+
|
6
|
+
# This codec may be used to decode (via inputs) and encode (via outputs)
|
7
|
+
# full JSON messages. If the data being sent is a JSON array at its root multiple events will be created (one per element).
|
8
|
+
#
|
9
|
+
# If you are streaming JSON messages delimited
|
10
|
+
# by '\n' then see the `json_lines` codec.
|
11
|
+
#
|
12
|
+
# Encoding will result in a compact JSON representation (no line terminators or indentation)
|
13
|
+
#
|
14
|
+
# If this codec recieves a payload from an input that is not valid JSON, then
|
15
|
+
# it will fall back to plain text and add a tag `_jsonparsefailure`. Upon a JSON
|
16
|
+
# failure, the payload will be stored in the `message` field.
|
17
|
+
class LogStash::Codecs::COLLECTD_JSON < LogStash::Codecs::Base
|
18
|
+
config_name "collectd_json"
|
19
|
+
|
20
|
+
|
21
|
+
# The character encoding used in this codec. Examples include "UTF-8" and
|
22
|
+
# "CP1252".
|
23
|
+
#
|
24
|
+
# JSON requires valid UTF-8 strings, but in some cases, software that
|
25
|
+
# emits JSON does so in another encoding (nxlog, for example). In
|
26
|
+
# weird cases like this, you can set the `charset` setting to the
|
27
|
+
# actual encoding of the text and Logstash will convert it for you.
|
28
|
+
#
|
29
|
+
# For nxlog users, you may to set this to "CP1252".
|
30
|
+
config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"
|
31
|
+
|
32
|
+
public
|
33
|
+
def register
|
34
|
+
@converter = LogStash::Util::Charset.new(@charset)
|
35
|
+
@converter.logger = @logger
|
36
|
+
end
|
37
|
+
|
38
|
+
public
|
39
|
+
def decode(data)
|
40
|
+
data = @converter.convert(data)
|
41
|
+
# FIX collectd -> AMQP -> LogStash bug with json format!
|
42
|
+
data.first! if data.is_a?(Array)
|
43
|
+
begin
|
44
|
+
decoded = LogStash::Json.load(data)
|
45
|
+
if decoded.is_a?(Array)
|
46
|
+
decoded.each {|item| yield(LogStash::Event.new(item)) }
|
47
|
+
elsif decoded.is_a?(Hash)
|
48
|
+
yield LogStash::Event.new(decoded)
|
49
|
+
else
|
50
|
+
@logger.info? && @logger.info("JSON codec received a scalar instead of an Arary or Object!", :data => data)
|
51
|
+
yield LogStash::Event.new("message" => data, "tags" => ["_jsonparsefailure"])
|
52
|
+
end
|
53
|
+
|
54
|
+
rescue LogStash::Json::ParserError => e
|
55
|
+
@logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => data)
|
56
|
+
yield LogStash::Event.new("message" => data, "tags" => ["_jsonparsefailure"])
|
57
|
+
rescue StandardError => e
|
58
|
+
# This should NEVER happen. But hubris has been the cause of many pipeline breaking things
|
59
|
+
# If something bad should happen we just don't want to crash logstash here.
|
60
|
+
@logger.warn("An unexpected error occurred parsing input to JSON",
|
61
|
+
:input => data,
|
62
|
+
:message => e.message,
|
63
|
+
:class => e.class.name,
|
64
|
+
:backtrace => e.backtrace)
|
65
|
+
end
|
66
|
+
end # def decode
|
67
|
+
|
68
|
+
public
|
69
|
+
def encode(event)
|
70
|
+
@on_event.call(event, event.to_json)
|
71
|
+
end # def encode
|
72
|
+
|
73
|
+
end # class LogStash::Codecs::COLLECTD_JSON
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
|
3
|
+
s.name = 'logstash-codec-collectd_json'
|
4
|
+
s.version = '2.0.0'
|
5
|
+
s.licenses = ['Apache License (2.0)']
|
6
|
+
s.summary = "This codec may be used to decode (via inputs) and encode (via outputs) full JSON messages"
|
7
|
+
s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
|
8
|
+
s.authors = ["Elastic"]
|
9
|
+
s.email = 'info@elastic.co'
|
10
|
+
s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
|
11
|
+
s.require_paths = ["lib"]
|
12
|
+
|
13
|
+
# Files
|
14
|
+
s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
|
15
|
+
|
16
|
+
# Tests
|
17
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
18
|
+
|
19
|
+
# Special flag to let us know this is actually a logstash plugin
|
20
|
+
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "codec" }
|
21
|
+
|
22
|
+
# Gem dependencies
|
23
|
+
s.add_runtime_dependency "logstash-core", "~> 2.0.0.snapshot"
|
24
|
+
|
25
|
+
s.add_development_dependency 'logstash-devutils'
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require "logstash/devutils/rspec/spec_helper"
|
2
|
+
require "logstash/codecs/json"
|
3
|
+
require "logstash/event"
|
4
|
+
require "logstash/json"
|
5
|
+
require "insist"
|
6
|
+
|
7
|
+
describe LogStash::Codecs::COLLECTD_JSON do
|
8
|
+
subject do
|
9
|
+
next LogStash::Codecs::COLLECTD_JSON.new
|
10
|
+
end
|
11
|
+
|
12
|
+
context "#decode" do
|
13
|
+
it "should return an event from json data" do
|
14
|
+
data = {"foo" => "bar", "baz" => {"bah" => ["a","b","c"]}}
|
15
|
+
subject.decode(LogStash::Json.dump(data)) do |event|
|
16
|
+
insist { event.is_a? LogStash::Event }
|
17
|
+
insist { event["foo"] } == data["foo"]
|
18
|
+
insist { event["baz"] } == data["baz"]
|
19
|
+
insist { event["bah"] } == data["bah"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be fast", :performance => true do
|
24
|
+
json = '{"message":"Hello world!","@timestamp":"2013-12-21T07:01:25.616Z","@version":"1","host":"Macintosh.local","sequence":1572456}'
|
25
|
+
iterations = 500000
|
26
|
+
count = 0
|
27
|
+
|
28
|
+
# Warmup
|
29
|
+
10000.times { subject.decode(json) { } }
|
30
|
+
|
31
|
+
start = Time.now
|
32
|
+
iterations.times do
|
33
|
+
subject.decode(json) do |event|
|
34
|
+
count += 1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
duration = Time.now - start
|
38
|
+
insist { count } == iterations
|
39
|
+
puts "codecs/json rate: #{"%02.0f/sec" % (iterations / duration)}, elapsed: #{duration}s"
|
40
|
+
end
|
41
|
+
|
42
|
+
context "processing plain text" do
|
43
|
+
it "falls back to plain text" do
|
44
|
+
decoded = false
|
45
|
+
subject.decode("something that isn't json") do |event|
|
46
|
+
decoded = true
|
47
|
+
insist { event.is_a?(LogStash::Event) }
|
48
|
+
insist { event["message"] } == "something that isn't json"
|
49
|
+
insist { event["tags"] }.include?("_jsonparsefailure")
|
50
|
+
end
|
51
|
+
insist { decoded } == true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "scalar values" do
|
56
|
+
shared_examples "given a value" do |value_arg|
|
57
|
+
context "where value is #{value_arg}" do
|
58
|
+
let(:value) { value_arg }
|
59
|
+
let(:event) { LogStash::Event.new(value) }
|
60
|
+
let(:value_json) { LogStash::Json.dump(value)}
|
61
|
+
let(:event) do
|
62
|
+
e = nil
|
63
|
+
subject.decode(value_json) do |decoded|
|
64
|
+
e = decoded
|
65
|
+
end
|
66
|
+
e
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should store the value in 'message'" do
|
70
|
+
expect(event["message"]).to eql(value_json)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should have the json parse failure tag" do
|
74
|
+
expect(event["tags"]).to include("_jsonparsefailure")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
include_examples "given a value", 123
|
80
|
+
include_examples "given a value", "hello"
|
81
|
+
include_examples "given a value", "-1"
|
82
|
+
include_examples "given a value", " "
|
83
|
+
end
|
84
|
+
|
85
|
+
context "processing JSON with an array root" do
|
86
|
+
let(:data) {
|
87
|
+
[
|
88
|
+
{"foo" => "bar"},
|
89
|
+
{"foo" => "baz"}
|
90
|
+
]
|
91
|
+
}
|
92
|
+
let(:data_json) {
|
93
|
+
LogStash::Json.dump(data)
|
94
|
+
}
|
95
|
+
|
96
|
+
it "should yield multiple events" do
|
97
|
+
count = 0
|
98
|
+
subject.decode(data_json) do |event|
|
99
|
+
count += 1
|
100
|
+
end
|
101
|
+
expect(count).to eql(data.count)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should yield the correct objects" do
|
105
|
+
index = 0
|
106
|
+
subject.decode(data_json) do |event|
|
107
|
+
expect(event.to_hash).to include(data[index])
|
108
|
+
index += 1
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "processing weird binary blobs" do
|
114
|
+
it "falls back to plain text and doesn't crash (LOGSTASH-1595)" do
|
115
|
+
decoded = false
|
116
|
+
blob = (128..255).to_a.pack("C*").force_encoding("ASCII-8BIT")
|
117
|
+
subject.decode(blob) do |event|
|
118
|
+
decoded = true
|
119
|
+
insist { event.is_a?(LogStash::Event) }
|
120
|
+
insist { event["message"].encoding.to_s } == "UTF-8"
|
121
|
+
end
|
122
|
+
insist { decoded } == true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "when json could not be parsed" do
|
127
|
+
|
128
|
+
let(:message) { "random_message" }
|
129
|
+
|
130
|
+
it "add the failure tag" do
|
131
|
+
subject.decode(message) do |event|
|
132
|
+
expect(event).to include "tags"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
it "uses an array to store the tags" do
|
137
|
+
subject.decode(message) do |event|
|
138
|
+
expect(event['tags']).to be_a Array
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
it "add a json parser failure tag" do
|
143
|
+
subject.decode(message) do |event|
|
144
|
+
expect(event['tags']).to include "_jsonparsefailure"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "#encode" do
|
151
|
+
it "should return json data" do
|
152
|
+
data = {"foo" => "bar", "baz" => {"bah" => ["a","b","c"]}}
|
153
|
+
event = LogStash::Event.new(data)
|
154
|
+
got_event = false
|
155
|
+
subject.on_event do |e, d|
|
156
|
+
insist { d.chomp } == LogStash::Event.new(data).to_json
|
157
|
+
insist { LogStash::Json.load(d)["foo"] } == data["foo"]
|
158
|
+
insist { LogStash::Json.load(d)["baz"] } == data["baz"]
|
159
|
+
insist { LogStash::Json.load(d)["bah"] } == data["bah"]
|
160
|
+
got_event = true
|
161
|
+
end
|
162
|
+
subject.encode(event)
|
163
|
+
insist { got_event }
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logstash-codec-collectd_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elastic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: logstash-core
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.0.snapshot
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 2.0.0.snapshot
|
25
|
+
prerelease: false
|
26
|
+
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: logstash-devutils
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
prerelease: false
|
40
|
+
type: :development
|
41
|
+
description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
|
42
|
+
email: info@elastic.co
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- CHANGELOG.md
|
48
|
+
- CONTRIBUTORS
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- NOTICE.TXT
|
52
|
+
- README.md
|
53
|
+
- lib/logstash/codecs/json.rb
|
54
|
+
- logstash-codec-collectd_json.gemspec
|
55
|
+
- spec/codecs/json_spec.rb
|
56
|
+
homepage: http://www.elastic.co/guide/en/logstash/current/index.html
|
57
|
+
licenses:
|
58
|
+
- Apache License (2.0)
|
59
|
+
metadata:
|
60
|
+
logstash_plugin: 'true'
|
61
|
+
logstash_group: codec
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: This codec may be used to decode (via inputs) and encode (via outputs) full JSON messages
|
82
|
+
test_files:
|
83
|
+
- spec/codecs/json_spec.rb
|