logstash-output-rocketchat 0.1.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: d20c7f02c37817fc79c56c94a9b750c9489abd72d9569326aa1926d9388a555b
4
+ data.tar.gz: c0170d3f9dcf144ef35989fbff8cac04f6532d13b0ad8c365572b7c3a88c738e
5
+ SHA512:
6
+ metadata.gz: cd8c6f5c3be0f9809a29da1852440d964f1f017385e462a446402cce7bcc21c9cc7c3b07ac286a8e71a9a45e4a1a5755a3f060623afa24e9244b5a63b02acbb8
7
+ data.tar.gz: a3609cc25ba4c45a91c374bdd8da018aed39d2064ab1dad3d3daf2b3f69d1a5e034c7e720ed60f5ba450760210ce8158eebdb4d7a378647ae4c67de2c89c1051
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
+ * ffknob - ffknob@tce.rs.gov.br
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-rocketchat
2
+ Example output plugin. This should help bootstrap your effort to write your own output plugin!
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
6
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
7
+
8
+ if Dir.exist?(logstash_path) && use_logstash_source
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ end
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,233 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+
5
+ # Rocket.Chat is free, unlimited and open source. Replace email, HipChat & Slack with the ultimate team chat software solution.
6
+ #
7
+ # This Logstash output plugin allows to send events as messages to channels and groups of a Rocketchat server.
8
+
9
+ # An rocketchat output that does nothing.
10
+ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
11
+ config_name "rocketchat"
12
+
13
+ # Host address of the Rocketchat server
14
+ config :host, :validate => :string, :required => true
15
+
16
+ # Port in which the Rocketchat server is listening
17
+ config :port, :validate => :string,:default => 3000
18
+
19
+ # Http or https
20
+ config :scheme, :validate => :string, :default => "http"
21
+
22
+ # API route
23
+ config :api, :validate => :string, :default => "/api/v1"
24
+
25
+ # Username to use for the integration
26
+ config :username, :validate => :string, :required => true
27
+
28
+ # Password of the user
29
+ config :password, :validate => :string, :required => true
30
+
31
+ # A list of channels/groups to which the messages will be posted
32
+ config :channels, :validate => :string, :required => true, :list => true
33
+
34
+ # Format of the message (content)
35
+ config :format, :validate => :string, :default => "%{message}"
36
+
37
+ concurrency :single
38
+
39
+ public
40
+ def register
41
+ m = __method__.to_s
42
+
43
+ require 'rest-client'
44
+ require 'cgi'
45
+ require 'json'
46
+
47
+ raw_url = "#{@scheme}://#{@host}:#{@port}#{@api}"
48
+ @url = ::LogStash::Util::SafeURI.new(raw_url)
49
+
50
+ @logger.info("[#{m}] URL #{@url}, Username: #{@username}, Channel: #{@channels}")
51
+
52
+ auth()
53
+ end # def register
54
+
55
+ public
56
+ def receive(event)
57
+ return unless output?(event)
58
+
59
+ m = __method__.to_s
60
+
61
+ message = event.sprintf(@format)
62
+
63
+ @channels.map {|channel|
64
+ if send_message(channel, message)
65
+ @logger.debug("[#{m}] Message sent to room #{channel}")
66
+ else
67
+ @logger.error("[#{m}] An error occurred trying to send a message to room #{channel}.")
68
+ end
69
+ }
70
+
71
+ end # def event
72
+
73
+ public
74
+ def auth()
75
+ m = __method__.to_s
76
+
77
+ @logger.debug("[i#{m}] Trying to authenticate")
78
+
79
+ endpoint = "#{@url}/login"
80
+
81
+ payload = Hash.new
82
+ payload['username'] = @username
83
+ payload['password'] = @password
84
+
85
+ @logger.debug("[#{m}] Endpoint: #{endpoint}, payload: #{payload}")
86
+
87
+ body = make_request('POST', endpoint, nil, payload)
88
+
89
+ if body.nil?
90
+ @logger.error("[#{m}] An error occurred trying to authenticate to the Rocketchat server")
91
+ return false
92
+ else
93
+ status = body['status']
94
+ if status == 'success'
95
+ @userId = body['data']['userId']
96
+ @authToken = body['data']['authToken']
97
+ return true
98
+ end
99
+ end
100
+ end # def auth
101
+
102
+ public
103
+ def get_room_id(room_name)
104
+ m = __method__.to_s
105
+
106
+ @logger.debug("[#{m}] Trying to get the room id for room #{room_name}")
107
+
108
+ endpoint = "#{@url}/channels.info"
109
+ body = make_request('GET', endpoint, {:roomName => room_name}, nil)
110
+
111
+ if body.nil?
112
+ @logger.error("[#{m}] An error occurred trying to get the room id (channels endpoint) for room #{room_name}")
113
+ else
114
+ success = body['success']
115
+ if success
116
+ return body['channel']['_id']
117
+ else
118
+ @logger.info("[#{m}] Couldn't get the room id for room #{room_name} through the channels endpoint, trying with the groups endpoint")
119
+
120
+ endpoint = "#{@url}/groups.info"
121
+ body = make_request('GET', endpoint, {:roomName => room_name}, nil)
122
+
123
+ if body.nil?
124
+ @logger.error("[#{m}] An error occurred trying to get the room id (groups endpoint) for room #{room_name}")
125
+ else
126
+ success = body['success']
127
+ if success
128
+ return body['group']['_id']
129
+ else
130
+ return nil
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ return nil
137
+ end # def get_room_id
138
+
139
+ public
140
+ def send_message(room_name, message)
141
+ m = __method__.to_s
142
+
143
+ @logger.debug("[#{m}] Trying to send message to room #{room_name}")
144
+
145
+ endpoint = "#{@url}/chat.sendMessage"
146
+
147
+ room_id = get_room_id(room_name)
148
+
149
+ if room_id
150
+ payload = Hash.new
151
+ payload['message'] = Hash.new
152
+ payload['message']['rid'] = room_id
153
+ payload['message']['msg'] = message
154
+
155
+ body = make_request('POST', endpoint, nil, payload)
156
+
157
+ if body.nil?
158
+ @logger.error("[#{m}] An error occurred trying to send message to the Rocketchat server, room #{room_name}")
159
+ return false
160
+ else
161
+ status = body['status']
162
+ if status == 'success'
163
+ return true
164
+ end
165
+ end
166
+ else
167
+ @logger.warn("[#{m}] An error occurred trying to get the room id for room #{room_name}. Are you sure the user #{@user} is sunscripted to this room?")
168
+ end
169
+
170
+ return nil
171
+ end # def send_message
172
+
173
+ public
174
+ def make_request(method, endpoint, params = nil, payload = nil)
175
+ m = __method__.to_s
176
+
177
+ @logger.debug("[#{m}] Making a #{method} request to #{endpoint}")
178
+ @logger.debug("[#{m}] Auth token: #{@authToken}")
179
+ @logger.debug("[#{m}] User id: #{@userId}")
180
+ @logger.debug("[#{m}] Params: #{params}")
181
+ @logger.debug("[#{m}] Payload: #{payload}")
182
+
183
+ begin
184
+ if method == 'POST'
185
+ RestClient.post(
186
+ endpoint,
187
+ JSON.dump(payload),
188
+ {
189
+ :'X-Auth-Token' => @authToken || '',
190
+ :'X-User-Id' => @userId || '' ,
191
+ :accept => "application/json",
192
+ :'User-Agent' => "logstash-output-rocketchat",
193
+ :content_type => @content_type
194
+ }) { |response, request, result, &block|
195
+ if response.code != 200
196
+ @logger.error("[#{m}] An error occurred trying to request from the Rocketchat's server API: #{response.code}")
197
+ @logger.debug("[#{m}] Got a #{response.code} response: #{response}")
198
+
199
+ return response.body ? JSON.parse(response.body) : nil
200
+ else
201
+ @logger.debug("[#{m}] Got a #{response.code} response: #{response}")
202
+
203
+ return response.body ? JSON.parse(response.body) : nil
204
+ end
205
+ }
206
+ elsif method == 'GET'
207
+ RestClient.get(
208
+ endpoint,
209
+ {
210
+ :params => params,
211
+ :'X-Auth-Token' => @authToken || '',
212
+ :'X-User-Id' => @userId || '' ,
213
+ :accept => "application/json",
214
+ :'User-Agent' => "logstash-output-rocketchat",
215
+ :content_type => @content_type
216
+ }) { |response, request, result, &block|
217
+ if response.code != 200
218
+ @logger.error("[#{m}] An error occurred trying to request from the Rocketchat's server API: #{response.code}")
219
+ @logger.debug("[#{m}] Got a #{response.code} response: #{response}")
220
+
221
+ return response.body ? JSON.parse(response.body) : nil
222
+ else
223
+ @logger.debug("[#{m}] Got a #{response.code} response: #{response}")
224
+
225
+ return response.body ? JSON.parse(response.body) : nil
226
+ end
227
+ }
228
+ end
229
+ rescue Exception => e
230
+ @logger.error("[#{m}] Unhandled exception", :exception => e, :stacktrace => e.backtrace)
231
+ end
232
+ end # def make_request
233
+ end # class LogStash::Outputs::Rocketchat
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-rocketchat'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Writes events to Rocketchat.'
6
+ s.description = 'Rocket.Chat is the leading open source team chat software solution. Free, unlimited and completely customizable with on-premises and SaaS cloud hosting. This Logstash plugin allows to send events to Rocketchat''s channels.'
7
+ s.homepage = 'https://github.com/ffknob/logstash-output-rocketchat'
8
+ s.authors = ['ffknob']
9
+ s.email = 'ffknob@tce.rs.gov.br'
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 "logstash-codec-plain"
23
+ s.add_runtime_dependency "rest-client", '~> 1.8', ">= 1.8.0"
24
+ s.add_development_dependency "logstash-devutils"
25
+ s.add_development_dependency "logstash-filter-json"
26
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/rocketchat"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+
7
+ describe LogStash::Outputs::Rocketchat do
8
+ let(:sample_event) { LogStash::Event.new }
9
+ let(:output) { LogStash::Outputs::Rocketchat.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,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-rocketchat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ffknob
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-29 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'
39
+ name: logstash-codec-plain
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.8'
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 1.8.0
56
+ name: rest-client
57
+ prerelease: false
58
+ type: :runtime
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.8'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.8.0
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ name: logstash-devutils
74
+ prerelease: false
75
+ type: :development
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ name: logstash-filter-json
88
+ prerelease: false
89
+ type: :development
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: Rocket.Chat is the leading open source team chat software solution. Free,
96
+ unlimited and completely customizable with on-premises and SaaS cloud hosting. This
97
+ Logstash plugin allows to send events to Rocketchats channels.
98
+ email: ffknob@tce.rs.gov.br
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - CHANGELOG.md
104
+ - CONTRIBUTORS
105
+ - DEVELOPER.md
106
+ - Gemfile
107
+ - LICENSE
108
+ - README.md
109
+ - lib/logstash/outputs/rocketchat.rb
110
+ - logstash-output-rocketchat.gemspec
111
+ - spec/outputs/rocketchat_spec.rb
112
+ homepage: https://github.com/ffknob/logstash-output-rocketchat
113
+ licenses:
114
+ - Apache-2.0
115
+ metadata:
116
+ logstash_plugin: 'true'
117
+ logstash_group: output
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.7.6
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Writes events to Rocketchat.
138
+ test_files:
139
+ - spec/outputs/rocketchat_spec.rb