logstash-output-mqtt2 1.0.0

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
+ SHA256:
3
+ metadata.gz: 72cf08e5cf11cf8a9d3a50c7742f2fc9100675228cec24130b696d9a6a51f9bc
4
+ data.tar.gz: 92090082b0aa43f1a09ced91cf87df83b1ac9f71bb9eace96661e0dda948b9bd
5
+ SHA512:
6
+ metadata.gz: aa035bf08e1208ae928bd5e9387227e841f2accbedc0d7e597c6da3afc188e9cad8b96d67ea5589775696ac2d2be28c959a6d3368b71b70d0ee188e804f13c97
7
+ data.tar.gz: 3598c8d057db24716c978ab1b18cc2e3ceccba6d7f38b907c530ff3da53fca6839b18d90ace7f20020693e44fd890a290342e6e78500bcb286811bbf1fb5e8a4
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
+ * -
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/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-output-mqtt2
2
+ Example output plugin. This should help bootstrap your effort to write your own output plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
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,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/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-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-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,184 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "mqtt"
4
+
5
+ # This is Logstash output plugin for the http://mqtt.org/[MQTT] protocol.
6
+ #
7
+ # Features:
8
+ #
9
+ # * Publish messages to a topic
10
+ # * TSL/SSL connection to MQTT server (optional)
11
+ # * Message publishing to a topic
12
+ # * QoS levels 0 and 1 (note that QoS 2 is not currently supported due to https://github.com/njh/ruby-mqtt[ruby-mqtt] library limitations)
13
+ # * Fault tolerance for network shortages, however not optimzied for performance since it takes a new connection for each event (or a bunch of events) to be published
14
+ # * MQTT protocol version 3.1.0
15
+ #
16
+ # Example publishing to test.mosquitto.org:
17
+ # [source,ruby]
18
+ # ----------------------------------
19
+ # output {
20
+ # mqtt {
21
+ # host => "test.mosquitto.org"
22
+ # port => 8883
23
+ # topic => "hello"
24
+ # }
25
+ # }
26
+ # ----------------------------------
27
+ #
28
+ # Example publishing to https://aws.amazon.com/iot/[AWS IoT]:
29
+ # [source,ruby]
30
+ # ----------------------------------
31
+ # output {
32
+ # mqtt {
33
+ # host => "somehostname.iot.someregion.amazonaws.com"
34
+ # port => 8883
35
+ # topic => "hello"
36
+ # client_id => "clientidfromaws"
37
+ # ssl => true
38
+ # cert_file => "certificate.pem.crt"
39
+ # key_file => "private.pem.key"
40
+ # ca_file => "root-CA.crt"
41
+ # }
42
+ # }
43
+ # ----------------------------------
44
+ #
45
+ # Topic may also depend on parts of the event using the standard sprintf syntax.
46
+ # [source,ruby]
47
+ # ----------------------------------
48
+ # output {
49
+ # mqtt {
50
+ # ...
51
+ # topic => "something/%{myfield}"
52
+ # }
53
+ # }
54
+ # ----------------------------------
55
+
56
+ class LogStash::Outputs::MQTT < LogStash::Outputs::Base
57
+
58
+ config_name "mqtt2"
59
+
60
+ # The default codec for this plugin is JSON. You can override this to suit your particular needs however.
61
+ default :codec, "json"
62
+
63
+ # MQTT server host name
64
+ config :host, :validate => :string, :required => true
65
+
66
+ # Port to connect to
67
+ config :port, :validate => :number, :default => 8883
68
+
69
+ # Topic that the messages will be published to
70
+ config :topic, :validate => :string, :required => true
71
+
72
+ # Retain flag of the published message
73
+ # If true, the message will be stored by the server and be sent immediately to each subscribing client
74
+ # so that the subscribing client doesn't have to wait until a publishing client sends the next update
75
+ config :retain, :validate => :boolean, :default => false
76
+
77
+ # QoS of the published message, can be either 0 (at most once) or 1 (at least once)
78
+ config :qos, :validate => :number, :default => 0
79
+
80
+ # Client identifier (generated automatically if not given)
81
+ config :client_id, :validate => :string
82
+
83
+ # Username to authenticate to the server with
84
+ config :username, :validate => :string
85
+
86
+ # Password to authenticate to the server with
87
+ config :password, :validate => :string
88
+
89
+ # Set to true to enable SSL/TLS encrypted communication
90
+ config :ssl, :validate => :boolean, :default => false
91
+
92
+ # Client certificate file used to SSL/TLS communication
93
+ config :cert_file, :validate => :path
94
+
95
+ # Private key file associated with the client certificate
96
+ config :key_file, :validate => :path
97
+
98
+ # Root CA certificate
99
+ config :ca_file, :validate => :path
100
+
101
+ # Time in seconds to wait before retrying a connection
102
+ config :connect_retry_interval, :validate => :number, :default => 10
103
+
104
+ # Time Keep alive connexion between ping to remote servers
105
+ config :keep_alive, :validate => :number, :default => 15
106
+
107
+
108
+ public
109
+ def register
110
+ @options = {
111
+ :host => @host
112
+ }
113
+ if @port
114
+ @options[:port] = @port
115
+ end
116
+ if @client_id
117
+ @options[:client_id] = @client_id
118
+ end
119
+ if @username
120
+ @options[:username] = @username
121
+ end
122
+ if @password
123
+ @options[:password] = @password
124
+ end
125
+ if @ssl
126
+ @options[:ssl] = @ssl
127
+ end
128
+ if @cert_file
129
+ @options[:cert_file] = @cert_file
130
+ end
131
+ if @key_file
132
+ @options[:key_file] = @key_file
133
+ end
134
+ if @ca_file
135
+ @options[:ca_file] = @ca_file
136
+ end
137
+ if @keep_alive
138
+ @options[:keep_alive] = @keep_alive
139
+ end
140
+
141
+ # Encode events using the given codec
142
+ # Use an array as a buffer so the multi_receive can handle multiple events with a single connection
143
+ @event_buffer = Array.new
144
+ @codec.on_event do |event, encoded_event|
145
+ @event_buffer.push([event, encoded_event])
146
+ end
147
+ end # def register
148
+ public
149
+ def receive(event)
150
+ @codec.encode(event)
151
+ handle_events
152
+ end # def receive
153
+
154
+ def multi_receive(events)
155
+ events.each { |event| @codec.encode(event) }
156
+ # Handle all events at once to prevent taking a new connection for each event
157
+ handle_events
158
+ end
159
+
160
+ def close
161
+ @closing = true
162
+ end # def close
163
+
164
+ private
165
+
166
+ def mqtt_client
167
+ @logger.debug("Connecting MQTT with options #{@options}")
168
+ @mqtt_client ||= MQTT::Client.connect(@options)
169
+ end
170
+
171
+ def handle_events
172
+ # It is easy to cope with network failures, ie. if connection fails just try it again
173
+ while event = @event_buffer.first do
174
+ @logger.debug("Publishing MQTT event #{event[1]} with topic #{@topic}, retain #{@retain}, qos #{@qos}")
175
+ mqtt_client.publish(event[0].sprintf(@topic), event[1], @retain, @qos)
176
+ @event_buffer.shift
177
+ end
178
+ rescue StandardError => e
179
+ @logger.error("Error #{e.message} while publishing to MQTT server. Will retry in #{@connect_retry_interval} seconds.")
180
+ @mqtt_client = nil
181
+ Stud.stoppable_sleep(@connect_retry_interval, 1) { @closing }
182
+ retry
183
+ end # def handle_event
184
+ end # class LogStash::Outputs::MQTT2
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "logstash-output-mqtt2"
3
+ s.version = "1.0.0"
4
+ s.license = "Apache-2.0"
5
+ s.summary = "Logstash MQTT OUTPUT"
6
+ s.description = "Logstash MQTT OUTPUT to manage real time actions"
7
+ s.homepage = "https://github.com/hupiv/logstash-output-mqtt2"
8
+ s.authors = ["HUPI - Vincent Moreno"]
9
+ s.email = "contact@hupi.fr"
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", "~> 2.0"
22
+ s.add_runtime_dependency "logstash-codec-plain"
23
+ s.add_development_dependency "logstash-devutils"
24
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/mqtt2"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+
7
+ describe LogStash::Outputs::Mqtt2 do
8
+ let(:sample_event) { LogStash::Event.new }
9
+ let(:output) { LogStash::Outputs::Mqtt2.new }
10
+
11
+ before do
12
+ output.register
13
+ end
14
+
15
+ describe "receive message" do
16
+ subject { output.receive(sample_event) }
17
+
18
+ it "returns a string" do
19
+ expect(subject).to eq("Event received")
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-mqtt2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - HUPI - Vincent Moreno
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logstash-codec-plain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: logstash-devutils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Logstash MQTT OUTPUT to manage real time actions
56
+ email: contact@hupi.fr
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - CONTRIBUTORS
63
+ - DEVELOPER.md
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/logstash/outputs/mqtt2.rb
68
+ - logstash-output-mqtt2.gemspec
69
+ - spec/outputs/mqtt2_spec.rb
70
+ homepage: https://github.com/hupiv/logstash-output-mqtt2
71
+ licenses:
72
+ - Apache-2.0
73
+ metadata:
74
+ logstash_plugin: 'true'
75
+ logstash_group: output
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.3.7
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Logstash MQTT OUTPUT
95
+ test_files:
96
+ - spec/outputs/mqtt2_spec.rb