Class: LogStash::Outputs::Unomaly

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/unomaly.rb

Overview

An example output that does nothing.

Defined Under Namespace

Classes: InvalidHTTPConfigError

Instance Method Summary collapse

Instance Method Details

#clientObject



220
221
222
# File 'lib/logstash/outputs/unomaly.rb', line 220

def client
  @client ||= make_client
end

#client_configObject

def register



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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/logstash/outputs/unomaly.rb', line 133

def client_config
  c = {
      connect_timeout: @connect_timeout,
      socket_timeout: @socket_timeout,
      request_timeout: @request_timeout,
      follow_redirects: @follow_redirects,
      automatic_retries: @automatic_retries,
      retry_non_idempotent: @retry_non_idempotent,
      check_connection_timeout: @validate_after_inactivity,
      pool_max: @pool_max,
      pool_max_per_route: @pool_max_per_route,
      cookies: @cookies,
      keepalive: @keepalive
  }

  if @proxy
    # Symbolize keys if necessary
    c[:proxy] = @proxy.is_a?(Hash) ?
                    @proxy.reduce({}) {|memo,(k,v)| memo[k.to_sym] = v; memo} :
                    @proxy
  end

  if @user
    if !@password || !@password.value
      raise ::LogStash::ConfigurationError, "User '#{@user}' specified without password!"
    end

    # Symbolize keys if necessary
    c[:auth] = {
        :user => @user,
        :password => @password.value,
        :eager => true
    }
  end

  c[:ssl] = {}
  if @ssl_certificate_validation == "disable" || @ssl_certificate_validation == "false"
    c[:ssl][:verify] = :disable
  elsif @ssl_certificate_validation == "browser"
    c[:ssl][:verify] = :browser
  end

  if @cacert
    c[:ssl][:ca_file] = @cacert
  end

  if @truststore
    c[:ssl].merge!(
        :truststore => @truststore,
        :truststore_type => @truststore_type,
        :truststore_password => @truststore_password.value
    )

    if c[:ssl][:truststore_password].nil?
      raise LogStash::ConfigurationError, "Truststore declared without a password! This is not valid, please set the 'truststore_password' option"
    end
  end

  if @keystore
    c[:ssl].merge!(
        :keystore => @keystore,
        :keystore_type => @keystore_type,
        :keystore_password => @keystore_password.value
    )

    if c[:ssl][:keystore_password].nil?
      raise LogStash::ConfigurationError, "Keystore declared without a password! This is not valid, please set the 'keystore_password' option"
    end
  end

  if @client_cert && @client_key
    c[:ssl][:client_cert] = @client_cert
    c[:ssl][:client_key] = @client_key
  elsif !!@client_cert ^ !!@client_key
    raise InvalidHTTPConfigError, "You must specify both client_cert and client_key for an HTTP client, or neither!"
  end

  c
end

#closeObject



225
226
227
# File 'lib/logstash/outputs/unomaly.rb', line 225

def close
  @client.close
end

#flatten(data, prefix) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/logstash/outputs/unomaly.rb', line 229

def flatten(data, prefix)
  ret = {}
  if data.is_a? Hash
    data.each { |key, value|
      if prefix.to_s.empty?
        ret.merge! flatten(value, "#{key.to_s}")
      else
        ret.merge! flatten(value, "#{prefix}__#{key.to_s}")
      end
    }
  elsif data.is_a? Array
    data.each_with_index {|val,index | ret.merge! flatten(val, "#{prefix}__#{index}")}
  else
    return {prefix => data.to_s}
  end

  ret
end

#log_failure(message, opts) ⇒ Object



330
331
332
# File 'lib/logstash/outputs/unomaly.rb', line 330

def log_failure(message, opts)
  @logger.error("[HTTP Output Failure] #{message}", opts)
end

#multi_receive(events) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/logstash/outputs/unomaly.rb', line 298

def multi_receive(events)
  if debug
    puts events.to_json
  end

  events.each_slice(@batch_size) do |chunk|
    documents = []
    chunk.each do |event|
      unomaly_event = {
        message: event.get(@message_key).to_s,
        source: event.get(@source_key).to_s,
      }

       = event.to_hash

      if @keep_timestamp
        unomaly_event["timestamp"] = event.get("@timestamp")
        .delete("@timestamp")
      end

      .delete(@source_key)
      .delete(@message_key)

      unomaly_event["metadata"]=flatten(,"")


      documents.push(unomaly_event)
    end
    send_batch(documents)
  end
end

#registerObject



124
125
126
127
128
129
130
131
# File 'lib/logstash/outputs/unomaly.rb', line 124

def register
  @total = 0
  @total_failed = 0
  logger.info("Initialized Unomaly output plugin with configuration",
              :host => @host,
              :accept_self_signed_cert => @accept_self_signed_cert)

end

#send_batch(events) ⇒ Object



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
288
289
290
291
292
293
294
# File 'lib/logstash/outputs/unomaly.rb', line 248

def send_batch(events)
  url = @host + @api_path
  body = events.to_json

  request = client.post(url, {
      :body => body,
      :headers => {"Content-Type"=>"application/json"}
  })

  request.on_success do |response|
    if response.code == 200
      @logger.debug("Successfully sent ",
                    :response_code => response.code,
                    :total => @total,
                    :time => Time::now.utc)
    else
      @total_failed += 1
      log_failure(
          "Encountered non-200 HTTP code #{response.code}",
          :response_code => response.code,
          :url => url,
          :response_body => response.body,
          :total_failed => @total_failed)
    end
  end

  request.on_failure do |exception|
    @total_failed += 1
    log_failure("Could not access URL",
                :url => url,
                :method => @http_method,
                :body => body,
                :message => exception.message,
                :class => exception.class.name,
                :backtrace => exception.backtrace,
                :total_failed => @total_failed
    )
  end

  @logger.debug("Sending Unomaly Event",
                :total => @total,
                :time => Time::now.utc)
  request.call

rescue Exception => e
  @logger.error("[Exception=] #{e.message} #{e.backtrace}")
end