logstash-output-loginsight 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ef32e65e47b9329686f03a7200debf3a4af7a1e0
4
+ data.tar.gz: fcb94b9a3495ad68de22ddedb2860330588a7404
5
+ SHA512:
6
+ metadata.gz: f34e1b3a95651db377f134c88054186c9366ba23de94d6782e9b2cae9874f7c644e9f31541b8a3a71d51f7cbaa67df4e3b5329a12d8950d4fd669fff36eb7d06
7
+ data.tar.gz: dc8a429b68d009fdb582a8c7b8dc88868260061fa42475998b9d06c45a34b0e07eb336f818c1ca4e8faa3a5a727941c71edd544a337ef9f475f3064c4c427025
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
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
+ * Alan Castonguay - acastonguay@vmware.com
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # logstash-output-loginsight
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash), sending events to [VMware vRealize Log Insight](https://www.vmware.com/support/pubs/log-insight-pubs.html)
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/logstash-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-output-loginsight.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-loginsight.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+ ```sh
78
+ bin/logstash -e 'input { stdin { add_field => { "fieldname" => "10" } } } output { loginsight { host => "10.11.12.13" } }' --log.level=debug
79
+ ```
80
+ ## Contributing
81
+
82
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
@@ -0,0 +1,127 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+ require "stud/buffer"
5
+ require "manticore"
6
+ #require "logstash/agent"
7
+
8
+ # An output plugin that sends events to a VMware vRealize Log Insight cluster.
9
+ class LogStash::Outputs::Loginsight < LogStash::Outputs::Base
10
+ include Stud::Buffer
11
+
12
+ config_name "loginsight"
13
+
14
+ config :host, :validate => :string, :required => true
15
+ config :port, :validate => :number, :default => 9000
16
+ config :proto, :validate => :string, :default => "http"
17
+ config :uuid, :validate => :string, :default => nil
18
+
19
+ config :flush_size, :validate => :number, :default => 100
20
+ config :idle_flush_time, :validate => :number, :default => 1
21
+
22
+ # Fields that will be renamed or dropped.
23
+ config :adjusted_fields, :validate => :hash, :default => {
24
+ "hostname" => "host", # unlikely to be present, preserve anyway
25
+ "host" => "hostname", # desired change
26
+ "@version" => nil, # drop
27
+ "@timestamp" => nil, # drop, already mapped to "timestamp" in event_hash
28
+ "message" => nil, # drop, already mapped to "text" in event_hash
29
+ }
30
+
31
+ concurrency :single
32
+
33
+ public
34
+ def register
35
+ @uuid ||= ( @id or 0 ) # Default UUID
36
+ @logger.debug("Starting up agent #{@uuid}")
37
+ @url = "#{@proto}://#{@host}:#{@port}/api/v1/events/ingest/#{@uuid}"
38
+
39
+ @client = Manticore::Client.new(headers: {"Content-Type" => "application/json"})
40
+
41
+ #@client.use_ssl = true
42
+ #@client.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+ @logger.debug("Client", :client => @client)
44
+
45
+ buffer_initialize(
46
+ :max_items => @flush_size,
47
+ :max_interval => @idle_flush_time,
48
+ :logger => @logger
49
+ )
50
+ end # def register
51
+
52
+ public
53
+ def receive(event)
54
+ @logger.debug("Event received", :event => event)
55
+ buffer_receive(event)
56
+ end # def receive
57
+
58
+ public
59
+ def flush(events, database, teardown = false)
60
+ @logger.debug? and @logger.debug("Flushing #{events.size} events - Teardown? #{teardown}")
61
+
62
+ post(cfapi(events))
63
+ end
64
+
65
+ def timestamp_in_milliseconds(timestamp)
66
+ return (timestamp.to_f * 1000).to_i
67
+ end
68
+
69
+ # Frame the events in the hash-array structure required by Log Insight
70
+ def cfapi(events)
71
+ messages = []
72
+
73
+ # For each event
74
+ events.each do |event|
75
+ # Create an outbound event; this can be serialized to json and sent
76
+ event_hash = {
77
+ "timestamp" => timestamp_in_milliseconds(event.get("@timestamp")),
78
+ "text" => (event.get("message") or ""),
79
+ }
80
+
81
+ # Map fields from the event to the desired form
82
+ event_hash["fields"] = merge_hash(event.to_hash)
83
+ .reject { |key,value| @adjusted_fields.has_key?(key) and @adjusted_fields[key] == nil } # drop banned fields
84
+ .map {|k,v| [ @adjusted_fields.has_key?(k) ? @adjusted_fields[k] : k,v] } # rename fields
85
+ .map {|k,v| { "name" => (k), "content" => v } } # Convert a hashmap {k=>v, k2=>v2} to a list [{name=>k, content=>v}, {name=>k2, content=>v2}]
86
+
87
+ messages.push(event_hash)
88
+ end # events.each do
89
+
90
+ return { "events" => messages } # Framing required by CFAPI.
91
+ end # def flush
92
+
93
+ # Return a copy of the fieldname with non-alphanumeric characters removed.
94
+ def safefield(fieldname)
95
+ fieldname.gsub(/[^a-zA-Z0-9\_]/, '') # TODO: Correct pattern for a valid fieldname. Must deny leading numbers.
96
+ end
97
+
98
+ def post(messages)
99
+ @logger.debug("post(body)", :messages => messages)
100
+
101
+ body = LogStash::Json.dump(messages)
102
+ @logger.debug("json-dump", :body => body)
103
+
104
+ @logger.debug("attempting connection", :url => @url)
105
+ response = @client.post!(@url, :body => body)
106
+ @logger.debug("result", :response => response)
107
+
108
+ end # def post
109
+
110
+ # Recursively merge a nested dictionary into a flat dictionary with dotted keys.
111
+ def merge_hash(hash, prelude = nil)
112
+ hash.reduce({}) do |acc, kv|
113
+ k, v = kv
114
+ generated_key = prelude ? "#{prelude}_#{k}" : k.to_s
115
+ #puts("Generated key #{generated_key}")
116
+ if v.is_a?(Hash)
117
+ acc.merge!(merge_hash(v, generated_key))
118
+ elsif v.is_a?(Array)
119
+ acc[generated_key] = v.to_s
120
+ else
121
+ acc[generated_key] = v
122
+ end
123
+ acc
124
+ end
125
+ end
126
+
127
+ end # class LogStash::Outputs::Loginsight
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-loginsight'
3
+ s.version = '0.1.13'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Output events to a Log Insight server. This uses the Ingestion API protocol.'
6
+ s.description = 'This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install logstash-output-loginsight. This gem is not a stand-alone program.'
7
+ s.homepage = 'https://github.com/alanjcastonguay/logstash-output-loginsight'
8
+ s.authors = ['Alan Castonguay']
9
+ s.email = 'acastonguay@vmware.com'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
22
+ s.add_runtime_dependency "manticore", "~> 0.6", ">= 0.6.0"
23
+
24
+ s.add_development_dependency "logstash-devutils", "~> 0", ">= 1.3.1"
25
+ end
@@ -0,0 +1,146 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/loginsight"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+
7
+ describe LogStash::Outputs::Loginsight do
8
+
9
+ let(:epoch123) { "1970-01-01T00:00:00.123Z" }
10
+ let(:port) { 9543 }
11
+ let(:server) { subject.socket }
12
+ let(:sample_event) { LogStash::Event.new }
13
+
14
+ describe "safefield" do
15
+ let(:safefield) { LogStash::Outputs::Loginsight.new("host" => "localhost").send(:safefield, proposed) }
16
+ context "a simple name" do
17
+ let(:proposed) { "simple" }
18
+ it "should do nothing" do
19
+ expect(safefield).to eql("simple")
20
+ end
21
+ end
22
+
23
+ context "special characters" do
24
+ let(:proposed) { "a!_@{$%^&*()}[]|\\/.,;:'`~b" }
25
+ it "should be stripped" do
26
+ expect(safefield).to eql("a_b")
27
+ end
28
+ end
29
+
30
+ context "a name with spaces" do
31
+ let(:proposed) { "a name with spaces" }
32
+ it "should strip spaces" do
33
+ expect(safefield).to eql("anamewithspaces")
34
+ end
35
+ end
36
+
37
+ context "a name starting with a @" do
38
+ let(:proposed) { "@abc" }
39
+ it "should strip the @" do
40
+ expect(safefield).to eql("abc")
41
+ end
42
+ end
43
+
44
+ # TODO: Test for a fieldname starting with a number or underscore
45
+ end
46
+
47
+ describe "dotify" do
48
+ let(:event) { LogStash::Event.new() }
49
+ let(:merged) { LogStash::Outputs::Loginsight.new("host" => "localhost").send(:merge_hash, hash) }
50
+
51
+ context "a simple hash" do
52
+ let(:hash) { {:a => 2, 5 => 4} }
53
+ it "should do nothing more than stringify the keys" do
54
+ expect(merged).to eql("a" => 2, "5" => 4)
55
+ end
56
+ end
57
+
58
+ context "a complex hash" do
59
+ let(:hash) { {:a => 2, :b => {:c => 3, :d => 4, :e => {:f => 5}}} }
60
+ it "should dottify correctly" do
61
+ expect(merged).to eql({"a" => 2, "b_c" => 3, "b_d" => 4, "b_e_f" => 5})
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "timestamp" do
67
+ let(:t_in_ms) { LogStash::Outputs::Loginsight.new("host" => "localhost").send(:timestamp_in_milliseconds, ts) }
68
+ context "0.123Z in milliseconds" do
69
+ let(:ts) { LogStash::Timestamp.new("1970-01-01T00:00:00.123Z") }
70
+ it "should be 123" do
71
+ expect(t_in_ms).to eql(123)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "simple cfapi with" do
77
+ let(:cfapi) { LogStash::Outputs::Loginsight.new("host" => "localhost").send(:cfapi, events) }
78
+
79
+ context "no events" do
80
+ let(:events) { [] }
81
+ it "should produce an empty list of events" do
82
+ expect(cfapi).to eql({"events" => []})
83
+ end
84
+ end
85
+
86
+ context "two events with no content" do
87
+ let(:events) { [LogStash::Event.new, LogStash::Event.new] }
88
+ it "should have an events key with a list of two" do
89
+ expect(cfapi).to have_key("events")
90
+ expect((cfapi)["events"].size).to eq(2)
91
+ end
92
+ end
93
+
94
+ context "an event with no content" do
95
+ let(:events) { [LogStash::Event.new] }
96
+ it "should have an events key with a non-empty list" do
97
+ expect(cfapi).to have_key("events")
98
+ expect((cfapi)["events"]).not_to be_empty
99
+ end
100
+ end
101
+
102
+ context "an empty event" do
103
+ let(:events) { [LogStash::Event.new] }
104
+ let(:subject) { (cfapi)["events"][0] }
105
+ it "should have a timestamp" do
106
+ expect(subject).to have_key("timestamp")
107
+ expect(subject["timestamp"]).not_to be_zero
108
+ end
109
+ it "should have an empty text string" do
110
+ expect(subject).to have_key("text")
111
+ expect(subject["text"]).to match("")
112
+ end
113
+ it "should have an empty field list" do
114
+ expect(subject).to have_key("fields")
115
+ expect(subject["fields"]).to be_kind_of(Array)
116
+ expect(subject["fields"]).to eql([])
117
+ end
118
+ end
119
+ end # simple cfapi with
120
+
121
+ describe "complex cfapi with" do
122
+ let(:cfapi) { LogStash::Outputs::Loginsight.new("host" => "localhost").send(:cfapi, events) }
123
+
124
+ context "only fields" do
125
+ let(:events) { [LogStash::Event.new("@timestamp"=>epoch123, "@version"=>1, "bar"=>"baz")] }
126
+ it "should produce a populated dict" do
127
+ expect(cfapi).to eql({"events" => [{"timestamp"=>123, "text"=>"", "fields"=>[{"name"=>"bar", "content"=>"baz"}]}]})
128
+ end
129
+ end
130
+
131
+ context "a populated event" do
132
+ let(:events) { [LogStash::Event.new("@timestamp"=>epoch123, "@version"=>1, "message"=>"foo", "bar"=>"baz")] }
133
+ it "should produce a populated dict" do
134
+ expect(cfapi).to eql({"events" => [{"timestamp"=>123, "text"=>"foo", "fields"=>[{"name"=>"bar", "content"=>"baz"}]}]})
135
+ end
136
+ end
137
+
138
+ context "a nested keyed event" do
139
+ let(:events) { [LogStash::Event.new("@timestamp"=>epoch123, "@version"=>1, "message"=>"foo", "bar"=>{"baz"=>"awesome"})] }
140
+ it "should produce an awesome dict" do
141
+ expect(cfapi).to eql({"events" => [{"timestamp"=>123, "text"=>"foo", "fields"=>[{"name"=>"bar_baz", "content"=>"awesome"}]}]})
142
+ end
143
+ end
144
+ end # complex cfapi with
145
+
146
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-loginsight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.13
5
+ platform: ruby
6
+ authors:
7
+ - Alan Castonguay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.60'
19
+ - - "<="
20
+ - !ruby/object:Gem::Version
21
+ version: '2.99'
22
+ name: logstash-core-plugin-api
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.99'
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '0.6'
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.6.0
42
+ name: manticore
43
+ prerelease: false
44
+ type: :runtime
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.6'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.6.0
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - "~>"
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.1
62
+ name: logstash-devutils
63
+ prerelease: false
64
+ type: :development
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.3.1
73
+ description: This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install logstash-output-loginsight. This gem is not a stand-alone program.
74
+ email: acastonguay@vmware.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - CHANGELOG.md
80
+ - CONTRIBUTORS
81
+ - Gemfile
82
+ - LICENSE
83
+ - README.md
84
+ - lib/logstash/outputs/loginsight.rb
85
+ - logstash-output-loginsight.gemspec
86
+ - spec/outputs/loginsight_spec.rb
87
+ homepage: https://github.com/alanjcastonguay/logstash-output-loginsight
88
+ licenses:
89
+ - Apache-2.0
90
+ metadata:
91
+ logstash_plugin: 'true'
92
+ logstash_group: output
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.8
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Output events to a Log Insight server. This uses the Ingestion API protocol.
113
+ test_files:
114
+ - spec/outputs/loginsight_spec.rb