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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/logstash/outputs/rocketchat.rb', line 94 def auth() m = __method__.to_s @logger.debug("[i#{m}] Trying to authenticate") endpoint = "#{@url}/login" payload = Hash.new payload['username'] = @username payload['password'] = @password @logger.debug("[#{m}] Endpoint: #{endpoint}, payload: #{payload}") 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' @userId = body['data']['userId'] @authToken = body['data']['authToken'] return true end end end |
#get_room_id(room_name) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/logstash/outputs/rocketchat.rb', line 123 def get_room_id(room_name) m = __method__.to_s @logger.debug("[#{m}] Trying to get the room id for room #{room_name}") 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 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") 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 return body['group']['_id'] else return nil end end end end return nil end |
#make_request(method, endpoint, params = nil, payload = nil) ⇒ Object
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/logstash/outputs/rocketchat.rb', line 194 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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/logstash/outputs/rocketchat.rb', line 76 def receive(event) return unless output?(event) m = __method__.to_s = event.sprintf(@format) @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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/logstash/outputs/rocketchat.rb', line 60 def register m = __method__.to_s require 'rest-client' require 'cgi' require 'json' raw_url = "#{@scheme}://#{@host}:#{@port}#{@api}" @url = ::LogStash::Util::SafeURI.new(raw_url) @logger.info("[#{m}] URL #{@url}, Username: #{@username}, Channel: #{@channels}") auth() end |
#send_message(room_name, message) ⇒ Object
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 187 188 189 190 191 |
# File 'lib/logstash/outputs/rocketchat.rb', line 160 def (room_name, ) m = __method__.to_s @logger.debug("[#{m}] Trying to send message to room #{room_name}") endpoint = "#{@url}/chat.sendMessage" 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 status = body['status'] if status == 'success' return true 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 sunscripted to this room?") end return nil end |