Class: LogStash::Outputs::Rocketchat
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Rocketchat
- Defined in:
- lib/logstash/outputs/rocketchat.rb
Overview
An rocketchat output that does nothing.
Instance Method Summary collapse
- #auth ⇒ Object
- #get_room_id(room_name) ⇒ Object
- #make_request(method, endpoint, params = nil, payload = nil) ⇒ Object
- #receive(event) ⇒ Object
- #register ⇒ Object
- #send_message(room_name, message) ⇒ Object
Instance Method Details
#auth ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/logstash/outputs/rocketchat.rb', line 103 def auth() m = __method__.to_s @logger.debug("[#{m}] Trying to authenticate") # https://rocket.chat/docs/developer-guides/rest-api/authentication/login/ endpoint = "#{@url}/login" # We have to send a valid username/password in order to get a token payload = Hash.new payload['username'] = @username payload['password'] = @password @logger.debug("[#{m}] Endpoint: #{endpoint}, payload: #{payload}") # Go get this token... body = make_request('POST', endpoint, nil, payload) if body.nil? @logger.error("[#{m}] An error occurred trying to authenticate to the Rocketchat server") return false else status = body['status'] if status == 'success' # If it succeds, then we'll have an user id. and an authentication token @userId = body['data']['userId'] @authToken = body['data']['authToken'] return true end end end |
#get_room_id(room_name) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/logstash/outputs/rocketchat.rb', line 136 def get_room_id(room_name) m = __method__.to_s @logger.debug("[#{m}] Trying to get the room's id for room #{room_name}") # Have we got the room's id already? if @room_ids.key?(room_name) @logger.debug("[#{m}] Already got the id for room #{room_name}, no need to ask Rocketchat server") return @room_ids[room_name] end # https://rocket.chat/docs/developer-guides/rest-api/channels/info/ endpoint = "#{@url}/channels.info" body = make_request('GET', endpoint, {:roomName => room_name}, nil) if body.nil? @logger.error("[#{m}] An error occurred trying to get the room id (channels endpoint) for room #{room_name}") else success = body['success'] if success # It's a channel... return the room's id @logger.debug("[#{m}] Room #{room_name} is a channel, saving its id so we don't need to ask for it anymore") @room_ids[:room_name] = body['channel']['_id'] @logger.debug("[#{m}] Rooms' ids: #{@room_ids}") return body['channel']['_id'] else @logger.info("[#{m}] Couldn't get the room id for room #{room_name} through the channels endpoint, trying with the groups endpoint") # https://rocket.chat/docs/developer-guides/rest-api/groups/info/ endpoint = "#{@url}/groups.info" body = make_request('GET', endpoint, {:roomName => room_name}, nil) if body.nil? @logger.error("[#{m}] An error occurred trying to get the room id (groups endpoint) for room #{room_name}") else success = body['success'] if success # It's a group... return the room's id @logger.debug("[#{m}] Room #{room_name} is a group, saving its id so we don't need to ask for it anymore") @room_ids[:room_name] = body['group']['_id'] @logger.debug("[#{m}] Rooms' ids: #{@room_ids}") return body['group']['_id'] else return nil end end end end return nil end |
#make_request(method, endpoint, params = nil, payload = nil) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/logstash/outputs/rocketchat.rb', line 229 def make_request(method, endpoint, params = nil, payload = nil) m = __method__.to_s @logger.debug("[#{m}] Making a #{method} request to #{endpoint}") @logger.debug("[#{m}] Auth token: #{@authToken}") @logger.debug("[#{m}] User id: #{@userId}") @logger.debug("[#{m}] Params: #{params}") @logger.debug("[#{m}] Payload: #{payload}") begin if method == 'POST' RestClient.post( endpoint, JSON.dump(payload), { :'X-Auth-Token' => @authToken || '', :'X-User-Id' => @userId || '' , :accept => "application/json", :'User-Agent' => "logstash-output-rocketchat", :content_type => @content_type }) { |response, request, result, &block| if response.code != 200 @logger.error("[#{m}] An error occurred trying to request from the Rocketchat's server API: #{response.code}") @logger.debug("[#{m}] Got a #{response.code} response: #{response}") return response.body ? JSON.parse(response.body) : nil else @logger.debug("[#{m}] Got a #{response.code} response: #{response}") return response.body ? JSON.parse(response.body) : nil end } elsif method == 'GET' RestClient.get( endpoint, { :params => params, :'X-Auth-Token' => @authToken || '', :'X-User-Id' => @userId || '' , :accept => "application/json", :'User-Agent' => "logstash-output-rocketchat", :content_type => @content_type }) { |response, request, result, &block| if response.code != 200 @logger.error("[#{m}] An error occurred trying to request from the Rocketchat's server API: #{response.code}") @logger.debug("[#{m}] Got a #{response.code} response: #{response}") return response.body ? JSON.parse(response.body) : nil else @logger.debug("[#{m}] Got a #{response.code} response: #{response}") return response.body ? JSON.parse(response.body) : nil end } end rescue Exception => e @logger.error("[#{m}] Unhandled exception", :exception => e, :stacktrace => e.backtrace) end end |
#receive(event) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/logstash/outputs/rocketchat.rb', line 83 def receive(event) return unless output?(event) m = __method__.to_s # The message itself = event.sprintf(@content) # To which channels/groups should the message be sent @channels.map {|channel| if (channel, ) @logger.debug("[#{m}] Message sent to room #{channel}") else @logger.error("[#{m}] An error occurred trying to send a message to room #{channel}.") end } end |
#register ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/logstash/outputs/rocketchat.rb', line 61 def register m = __method__.to_s require 'rest-client' require 'cgi' require 'json' # Rocketchat's API # More information in https://rocket.chat/docs/developer-guides/rest-api/ raw_url = "#{@scheme}://#{@host}:#{@port}#{@api}" @url = ::LogStash::Util::SafeURI.new(raw_url) # We'll keep the rooms' ids so we don't need to ask again for each event @room_ids = Hash.new @logger.info("[#{m}] URL #{@url}, Username: #{@username}, Channel: #{@channels}") # We need to grab a token from Rocketchat auth() end |
#send_message(room_name, message) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/logstash/outputs/rocketchat.rb', line 189 def (room_name, ) m = __method__.to_s @logger.debug("[#{m}] Trying to a send message to room #{room_name}") # https://rocket.chat/docs/developer-guides/rest-api/chat/sendmessage/ endpoint = "#{@url}/chat.sendMessage" # Go get the room's id room_id = get_room_id(room_name) if room_id payload = Hash.new payload['message'] = Hash.new payload['message']['rid'] = room_id payload['message']['msg'] = body = make_request('POST', endpoint, nil, payload) if body.nil? @logger.error("[#{m}] An error occurred trying to send message to the Rocketchat server, room #{room_name}") return false else success = body['success'] if success # Message sent return true else # Something went wrong return false end end else @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?") end return false end |