logstash-output-mqtt 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c13e7a4156045d08c121c0de23f4eaa1a4bce1995b2d079abb4dd15ac7d5c1d9
4
- data.tar.gz: 610ddd709836340d87c55ce1ecc2ef145ca6c6e447d8a9688e55d4f775639457
3
+ metadata.gz: 69b541cd6a2abd62541b3500c13cafdd67c2ea83270fcdfbbcc923dee527a0d9
4
+ data.tar.gz: e1696a7ecffef182fac7aff34e57def48983ba8dc064ba7504ddb61ef2a5db31
5
5
  SHA512:
6
- metadata.gz: f98f37cd1a440228baf58e576765eef0c573d349e857ded201c680fd9c5f87085a11cb63f21262eb95f27b05e00d4eccf8197676b903502f8f7d94adfa666441
7
- data.tar.gz: 27a270fd71d5deba222d85113231be86f43b747e538d11dcc404f459dd88e96a7d1dc26801a8af3ccf6a98a3ff906c55451d664fb964f56f05417730401aba63
6
+ metadata.gz: 3f19c42ebcad7d7d0f80788ab2509396d2a4602b7f7cb344b195124a702acdd12c37ded9986d09d003c54fd69d9ee1fa7784282e674c3149f28441cb6ddf8dca
7
+ data.tar.gz: 4f7d48105f283f95705e0d127a79739a7f625928762404c11d3374e23e6b316fb85e316cf3cdb222d5be2b0df462be9c9761ada9c0453d8a4311f36856ce30db
data/Gemfile CHANGED
@@ -1,3 +1,10 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
+ logstash_path = ENV["LOGSTASH_PATH"] || "/usr/share/logstash"
5
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
6
+
7
+ if Dir.exist?(logstash_path) && use_logstash_source
8
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
9
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
10
+ end
@@ -102,7 +102,11 @@ class LogStash::Outputs::MQTT < LogStash::Outputs::Base
102
102
  # Time in seconds to wait before retrying a connection
103
103
  config :connect_retry_interval, :validate => :number, :default => 10
104
104
 
105
- public
105
+ # Time Keep alive connexion between ping to remote servers
106
+ config :keep_alive, :validate => :number, :default => 15
107
+
108
+
109
+
106
110
  def register
107
111
  @options = {
108
112
  :host => @host
@@ -131,6 +135,9 @@ class LogStash::Outputs::MQTT < LogStash::Outputs::Base
131
135
  if @ca_file
132
136
  @options[:ca_file] = @ca_file
133
137
  end
138
+ if @keep_alive
139
+ @options[:keep_alive] = @keep_alive
140
+ end
134
141
 
135
142
  # Encode events using the given codec
136
143
  # Use an array as a buffer so the multi_receive can handle multiple events with a single connection
@@ -138,50 +145,41 @@ class LogStash::Outputs::MQTT < LogStash::Outputs::Base
138
145
  @codec.on_event do |event, encoded_event|
139
146
  @event_buffer.push([event, encoded_event])
140
147
  end
141
-
142
148
  end # def register
143
149
 
144
- public
145
150
  def receive(event)
146
-
147
151
  @codec.encode(event)
148
152
  handle_events
149
153
  end # def receive
150
154
 
151
- public
152
155
  def multi_receive(events)
153
- events.each do |event|
154
- @codec.encode(event)
155
- end
156
-
156
+ events.each { |event| @codec.encode(event) }
157
157
  # Handle all events at once to prevent taking a new connection for each event
158
158
  handle_events
159
159
  end
160
160
 
161
- public
162
161
  def close
163
-
164
162
  @closing = true
165
163
  end # def close
166
164
 
167
165
  private
168
- def handle_events
169
- # Simple design: connect separately for each event / bunch of events in the buffer
170
- # This way it is easy to cope with network failures, ie. if connection fails just try it again
166
+
167
+ def mqtt_client
171
168
  @logger.debug("Connecting MQTT with options #{@options}")
172
- MQTT::Client.connect(@options) do |client|
173
- while event = @event_buffer.first do
174
- @logger.debug("Publishing MQTT event #{event[1]} with topic #{@topic}, retain #{@retain}, qos #{@qos}")
175
- client.publish(event[0].sprintf(@topic), event[1], @retain, @qos)
176
- @event_buffer.shift
177
- end
178
- end
169
+ @mqtt_client ||= MQTT::Client.connect(@options)
170
+ end
179
171
 
172
+ def handle_events
173
+ # It is easy to cope with network failures, ie. if connection fails just try it again
174
+ while event = @event_buffer.first do
175
+ @logger.debug("Publishing MQTT event #{event[1]} with topic #{@topic}, retain #{@retain}, qos #{@qos}")
176
+ mqtt_client.publish(event[0].sprintf(@topic), event[1], @retain, @qos)
177
+ @event_buffer.shift
178
+ end
180
179
  rescue StandardError => e
181
180
  @logger.error("Error #{e.message} while publishing to MQTT server. Will retry in #{@connect_retry_interval} seconds.")
182
-
181
+ @mqtt_client = nil
183
182
  Stud.stoppable_sleep(@connect_retry_interval, 1) { @closing }
184
183
  retry
185
184
  end # def handle_event
186
-
187
185
  end # class LogStash::Outputs::MQTT
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-output-mqtt'
3
- s.version = "1.1.0"
4
- s.licenses = ["Apache License (2.0)"]
3
+ s.version = "1.2.1"
4
+ s.licenses = ["Apache-2.0"]
5
5
  s.summary = "This is Logstash output plugin for the http://mqtt.org/[MQTT] protocol"
6
6
  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"
7
7
  s.authors = ["Tommi Palomäki"]
@@ -19,8 +19,9 @@ Gem::Specification.new do |s|
19
19
  s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
20
20
 
21
21
  # Gem dependencies
22
- s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
23
23
  s.add_runtime_dependency "logstash-codec-json"
24
24
  s.add_runtime_dependency "mqtt"
25
- s.add_development_dependency "logstash-devutils", "< 1.3.5"
25
+
26
+ s.add_development_dependency "logstash-devutils"
26
27
  end
@@ -4,6 +4,7 @@ require "logstash/outputs/mqtt"
4
4
  require "logstash/codecs/json"
5
5
  require "logstash/event"
6
6
  require "time"
7
+ require "json"
7
8
 
8
9
  describe LogStash::Outputs::MQTT do
9
10
  # Define a sample event and its JSON encoded form
@@ -11,48 +12,37 @@ describe LogStash::Outputs::MQTT do
11
12
  tstamp = Time.parse(datestring)
12
13
  tstamp_utc = tstamp.utc.iso8601(3)
13
14
  let(:sample_event) { LogStash::Event.new("message" => "test message", "@timestamp" => datestring) }
14
- encoded_event = "{\"@timestamp\":\"" + tstamp_utc + "\",\"@version\":\"1\",\"message\":\"test message\"}"
15
+ encoded_event = "{\"message\":\"test message\",\"@version\":\"1\",\"@timestamp\":\"" + tstamp_utc + "\"}"
15
16
 
16
17
  settings = {
17
18
  "host" => "test.mosquitto.org",
18
19
  "topic" => "hello"
19
20
  }
20
21
  let(:output) { LogStash::Outputs::MQTT.new(settings) }
21
- let(:stub_client) { Object.new }
22
+ let(:stub_client) { instance_double(MQTT::Client) }
22
23
 
23
24
  before do
24
25
  output.register
25
26
  end
26
27
 
27
28
  describe "receive message" do
28
-
29
29
  it "connects and publishes with correct arguments" do
30
-
31
30
  expect(MQTT::Client).to receive(:connect).with(
32
31
  :host => "test.mosquitto.org",
33
32
  :port => 8883
34
- ) do |*args, &block|
35
- block.call(stub_client)
36
- end
37
-
33
+ ).and_return(stub_client)
38
34
  expect(stub_client).to receive(:publish).with("hello", encoded_event, false, 0)
39
-
40
35
  output.receive(sample_event)
41
36
  end
42
37
  end
43
38
 
44
39
  describe "multi_receive" do
45
40
  it "connects once and publishes all messages" do
46
-
47
41
  expect(MQTT::Client).to receive(:connect).once.with(
48
42
  :host => "test.mosquitto.org",
49
43
  :port => 8883
50
- ) do |*args, &block|
51
- block.call(stub_client)
52
- end
53
-
44
+ ).and_return(stub_client)
54
45
  expect(stub_client).to receive(:publish).exactly(3).times.with("hello", encoded_event, false, 0)
55
-
56
46
  three_events = [sample_event, sample_event, sample_event]
57
47
  output.multi_receive(three_events)
58
48
  end
@@ -65,14 +55,14 @@ describe "Test subtopic:" do
65
55
  tstamp = Time.parse(datestring)
66
56
  tstamp_utc = tstamp.utc.iso8601(3)
67
57
  let(:sample_event) { LogStash::Event.new("message" => "test message", "@timestamp" => datestring, "subtopic" => "mysubtopic") }
68
- encoded_event = "{\"@timestamp\":\"" + tstamp_utc + "\",\"@version\":\"1\",\"message\":\"test message\",\"subtopic\":\"mysubtopic\"}"
58
+ encoded_event = "{\"@version\":\"1\",\"@timestamp\":\"" + tstamp_utc + "\",\"message\":\"test message\",\"subtopic\":\"mysubtopic\"}"
69
59
 
70
60
  settings = {
71
61
  "host" => "test.mosquitto.org",
72
62
  "topic" => "hello/%{subtopic}"
73
63
  }
74
64
  let(:output) { LogStash::Outputs::MQTT.new(settings) }
75
- let(:stub_client) { Object.new }
65
+ let(:stub_client) { instance_double(MQTT::Client) }
76
66
 
77
67
  before do
78
68
  output.register
@@ -80,16 +70,11 @@ describe "Test subtopic:" do
80
70
 
81
71
  describe "receive message on a subtopic based on a field of an event" do
82
72
  it "connects and publishes" do
83
-
84
73
  expect(MQTT::Client).to receive(:connect).with(
85
74
  :host => "test.mosquitto.org",
86
75
  :port => 8883
87
- ) do |*args, &block|
88
- block.call(stub_client)
89
- end
90
-
76
+ ).and_return(stub_client)
91
77
  expect(stub_client).to receive(:publish).with("hello/mysubtopic", encoded_event, false, 0)
92
-
93
78
  output.receive(sample_event)
94
79
  end
95
80
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-output-mqtt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommi Palomäki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-24 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - "~>"
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.60'
19
+ - - "<="
17
20
  - !ruby/object:Gem::Version
18
- version: '2.0'
21
+ version: '2.99'
19
22
  name: logstash-core-plugin-api
20
- prerelease: false
21
23
  type: :runtime
24
+ prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
25
31
  - !ruby/object:Gem::Version
26
- version: '2.0'
32
+ version: '2.99'
27
33
  - !ruby/object:Gem::Dependency
28
34
  requirement: !ruby/object:Gem::Requirement
29
35
  requirements:
@@ -31,8 +37,8 @@ dependencies:
31
37
  - !ruby/object:Gem::Version
32
38
  version: '0'
33
39
  name: logstash-codec-json
34
- prerelease: false
35
40
  type: :runtime
41
+ prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
44
  - - ">="
@@ -45,8 +51,8 @@ dependencies:
45
51
  - !ruby/object:Gem::Version
46
52
  version: '0'
47
53
  name: mqtt
48
- prerelease: false
49
54
  type: :runtime
55
+ prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
58
  - - ">="
@@ -55,17 +61,17 @@ dependencies:
55
61
  - !ruby/object:Gem::Dependency
56
62
  requirement: !ruby/object:Gem::Requirement
57
63
  requirements:
58
- - - "<"
64
+ - - ">="
59
65
  - !ruby/object:Gem::Version
60
- version: 1.3.5
66
+ version: '0'
61
67
  name: logstash-devutils
62
- prerelease: false
63
68
  type: :development
69
+ prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
- - - "<"
72
+ - - ">="
67
73
  - !ruby/object:Gem::Version
68
- version: 1.3.5
74
+ version: '0'
69
75
  description: This gem is a logstash plugin required to be installed on top of the
70
76
  Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not
71
77
  a stand-alone program
@@ -85,7 +91,7 @@ files:
85
91
  - spec/outputs/mqtt_spec.rb
86
92
  homepage: https://github.com/kompa3/logstash-output-mqtt
87
93
  licenses:
88
- - Apache License (2.0)
94
+ - Apache-2.0
89
95
  metadata:
90
96
  logstash_plugin: 'true'
91
97
  logstash_group: output
@@ -104,8 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
110
  - !ruby/object:Gem::Version
105
111
  version: '0'
106
112
  requirements: []
107
- rubyforge_project:
108
- rubygems_version: 2.6.13
113
+ rubygems_version: 3.2.33
109
114
  signing_key:
110
115
  specification_version: 4
111
116
  summary: This is Logstash output plugin for the http://mqtt.org/[MQTT] protocol