logstash-output-rocketchat 0.1.2 → 0.1.3

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: c9983abca8265a54f9432a67c2d18ebf751b7c1e065d1b8718562a0cd736a400
4
- data.tar.gz: 51901b0d9fd53bcdd754b2bd916541e1c514283856e9184fa4d6835aac9177e7
3
+ metadata.gz: 44b757b06ab254981ccc585669f4076333cfa2fc9dc1cd0a9b424d4e39f52636
4
+ data.tar.gz: 45a307633b6816f649af2f1421606b2828be2debc33e90235ca525c6fa6343f4
5
5
  SHA512:
6
- metadata.gz: 47f7cd04aede88bd16e42fd927bc6a0505213d4ba5e87fe333abe2e9f86ef3c07ab76d928204f18e04d4ed47ff2c94b4f63a5ed75a195ef0c42e5cef594bebf8
7
- data.tar.gz: 7e1cbcf0f0d01c24a8994251b19f23e2ba760468d4a14357ba1366524c42e74a7c6874a3b9fa3b5038c0a711ad9c30bbdab91fbc13091cdc58987d7f70010fc5
6
+ metadata.gz: a353fe8fe553f1650b71553d91b7e87cd5946e0af7909413694ca7a9bfa7b71f6905541945b02183710e8eb62a2fbc8256ce765c809ad9744dce4a9ef14792b2
7
+ data.tar.gz: ab1a982a25ed8ba58e04ac7555255c442167ffa64fe9ad168ab62ecad2c34a146e8a2e90b2db495ee1db20be3a2991829ced1d949d98a3d0a498bbd01d202b9e
@@ -20,6 +20,7 @@ require "logstash/namespace"
20
20
  # username => "rc_integrations"
21
21
  # password => "p@$$w0rd"
22
22
  # channels => ["management", "operations", "rh"]
23
+ # content => "My message: %{message}"
23
24
  # }
24
25
  # }
25
26
  # ----------------------------------
@@ -51,8 +52,8 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
51
52
  # A list of channels/groups to which the messages will be posted
52
53
  config :channels, :validate => :string, :required => true, :list => true
53
54
 
54
- # Format of the message (content)
55
- config :format, :validate => :string, :default => "%{message}"
55
+ # Message's content to be sent
56
+ config :content, :validate => :string, :default => "%{message}"
56
57
 
57
58
  concurrency :single
58
59
 
@@ -63,12 +64,18 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
63
64
  require 'rest-client'
64
65
  require 'cgi'
65
66
  require 'json'
66
-
67
+
68
+ # Rocketchat's API
69
+ # More information in https://rocket.chat/docs/developer-guides/rest-api/
67
70
  raw_url = "#{@scheme}://#{@host}:#{@port}#{@api}"
68
- @url = ::LogStash::Util::SafeURI.new(raw_url)
71
+ @url = ::LogStash::Util::SafeURI.new(raw_url)
72
+
73
+ # We'll keep the rooms' ids so we don't need to ask again for each event
74
+ @room_ids = Hash.new
69
75
 
70
76
  @logger.info("[#{m}] URL #{@url}, Username: #{@username}, Channel: #{@channels}")
71
77
 
78
+ # We need to grab a token from Rocketchat
72
79
  auth()
73
80
  end # def register
74
81
 
@@ -78,8 +85,10 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
78
85
 
79
86
  m = __method__.to_s
80
87
 
81
- message = event.sprintf(@format)
88
+ # The message itself
89
+ message = event.sprintf(@content)
82
90
 
91
+ # To which channels/groups should the message be sent
83
92
  @channels.map {|channel|
84
93
  if send_message(channel, message)
85
94
  @logger.debug("[#{m}] Message sent to room #{channel}")
@@ -94,16 +103,19 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
94
103
  def auth()
95
104
  m = __method__.to_s
96
105
 
97
- @logger.debug("[i#{m}] Trying to authenticate")
106
+ @logger.debug("[#{m}] Trying to authenticate")
98
107
 
108
+ # https://rocket.chat/docs/developer-guides/rest-api/authentication/login/
99
109
  endpoint = "#{@url}/login"
100
110
 
111
+ # We have to send a valid username/password in order to get a token
101
112
  payload = Hash.new
102
113
  payload['username'] = @username
103
114
  payload['password'] = @password
104
115
 
105
116
  @logger.debug("[#{m}] Endpoint: #{endpoint}, payload: #{payload}")
106
117
 
118
+ # Go get this token...
107
119
  body = make_request('POST', endpoint, nil, payload)
108
120
 
109
121
  if body.nil?
@@ -111,7 +123,8 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
111
123
  return false
112
124
  else
113
125
  status = body['status']
114
- if status == 'success'
126
+ if status == 'success'
127
+ # If it succeds, then we'll have an user id. and an authentication token
115
128
  @userId = body['data']['userId']
116
129
  @authToken = body['data']['authToken']
117
130
  return true
@@ -123,20 +136,32 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
123
136
  def get_room_id(room_name)
124
137
  m = __method__.to_s
125
138
 
126
- @logger.debug("[#{m}] Trying to get the room id for room #{room_name}")
139
+ @logger.debug("[#{m}] Trying to get the room's id for room #{room_name}")
127
140
 
141
+ # Have we got the room's id already?
142
+ if @room_ids.key?(room_name)
143
+ @logger.debug("[#{m}] Already got the id for room #{room_name}, no need to ask Rocketchat server")
144
+ return @room_ids[room_name]
145
+ end
146
+
147
+ # https://rocket.chat/docs/developer-guides/rest-api/channels/info/
128
148
  endpoint = "#{@url}/channels.info"
129
- body = make_request('GET', endpoint, {:roomName => room_name}, nil)
149
+ body = make_request('GET', endpoint, {:roomName => room_name}, nil)
130
150
 
131
151
  if body.nil?
132
152
  @logger.error("[#{m}] An error occurred trying to get the room id (channels endpoint) for room #{room_name}")
133
153
  else
134
154
  success = body['success']
135
155
  if success
136
- return body['channel']['_id']
156
+ # It's a channel... return the room's id
157
+ @logger.debug("[#{m}] Room #{room_name} is a channel, saving its id so we don't need to ask for it anymore")
158
+ @room_ids[:room_name] = body['channel']['_id']
159
+ @logger.debug("[#{m}] Rooms' ids: #{@room_ids}")
160
+ return body['channel']['_id']
137
161
  else
138
162
  @logger.info("[#{m}] Couldn't get the room id for room #{room_name} through the channels endpoint, trying with the groups endpoint")
139
163
 
164
+ # https://rocket.chat/docs/developer-guides/rest-api/groups/info/
140
165
  endpoint = "#{@url}/groups.info"
141
166
  body = make_request('GET', endpoint, {:roomName => room_name}, nil)
142
167
 
@@ -144,7 +169,11 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
144
169
  @logger.error("[#{m}] An error occurred trying to get the room id (groups endpoint) for room #{room_name}")
145
170
  else
146
171
  success = body['success']
147
- if success
172
+ if success
173
+ # It's a group... return the room's id
174
+ @logger.debug("[#{m}] Room #{room_name} is a group, saving its id so we don't need to ask for it anymore")
175
+ @room_ids[:room_name] = body['group']['_id']
176
+ @logger.debug("[#{m}] Rooms' ids: #{@room_ids}")
148
177
  return body['group']['_id']
149
178
  else
150
179
  return nil
@@ -160,10 +189,12 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
160
189
  def send_message(room_name, message)
161
190
  m = __method__.to_s
162
191
 
163
- @logger.debug("[#{m}] Trying to send message to room #{room_name}")
192
+ @logger.debug("[#{m}] Trying to a send message to room #{room_name}")
164
193
 
194
+ # https://rocket.chat/docs/developer-guides/rest-api/chat/sendmessage/
165
195
  endpoint = "#{@url}/chat.sendMessage"
166
196
 
197
+ # Go get the room's id
167
198
  room_id = get_room_id(room_name)
168
199
 
169
200
  if room_id
@@ -178,16 +209,20 @@ class LogStash::Outputs::Rocketchat < LogStash::Outputs::Base
178
209
  @logger.error("[#{m}] An error occurred trying to send message to the Rocketchat server, room #{room_name}")
179
210
  return false
180
211
  else
181
- status = body['status']
182
- if status == 'success'
212
+ success = body['success']
213
+ if success
214
+ # Message sent
183
215
  return true
216
+ else
217
+ # Something went wrong
218
+ return false
184
219
  end
185
220
  end
186
221
  else
187
- @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?")
222
+ @logger.warn("[#{m}] An error occurred trying to get the room id for room #{room_name}. Are you sure the user #{@user} is subscribed to this room?")
188
223
  end
189
224
 
190
- return nil
225
+ return false
191
226
  end # def send_message
192
227
 
193
228
  public
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-output-rocketchat'
3
- s.version = '0.1.2'
3
+ s.version = '0.1.3'
4
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 channelsi and groups.'
5
+ s.summary = 'Sends messages to a Rocketchat server with information from the events.'
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 channels and groups.'
7
7
  s.homepage = 'https://github.com/ffknob/logstash-output-rocketchat'
8
8
  s.authors = ['ffknob']
9
9
  s.email = 'ffknob@tce.rs.gov.br'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-output-rocketchat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ffknob
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-29 00:00:00.000000000 Z
11
+ date: 2019-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -94,7 +94,7 @@ dependencies:
94
94
  version: '0'
95
95
  description: Rocket.Chat is the leading open source team chat software solution. Free,
96
96
  unlimited and completely customizable with on-premises and SaaS cloud hosting. This
97
- Logstash plugin allows to send events to Rocketchat channelsi and groups.
97
+ Logstash plugin allows to send events to Rocketchat channels and groups.
98
98
  email: ffknob@tce.rs.gov.br
99
99
  executables: []
100
100
  extensions: []
@@ -134,6 +134,6 @@ rubyforge_project:
134
134
  rubygems_version: 2.7.6
135
135
  signing_key:
136
136
  specification_version: 4
137
- summary: Writes events to Rocketchat.
137
+ summary: Sends messages to a Rocketchat server with information from the events.
138
138
  test_files:
139
139
  - spec/outputs/rocketchat_spec.rb