class LogStash::Outputs::Unomaly

An example output that does nothing.

Public Instance Methods

client() click to toggle source
# File lib/logstash/outputs/unomaly.rb, line 220
def client
  @client ||= make_client
end
client_config() click to toggle source
# 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
close() click to toggle source
# File lib/logstash/outputs/unomaly.rb, line 225
def close
  @client.close
end
flatten(data, prefix) click to toggle source
# 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) click to toggle source
# File lib/logstash/outputs/unomaly.rb, line 330
def log_failure(message, opts)
  @logger.error("[HTTP Output Failure] #{message}", opts)
end
multi_receive(events) click to toggle source
# 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,
      }

      metadata = event.to_hash

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

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

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


      documents.push(unomaly_event)
    end
    send_batch(documents)
  end
end
register() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

make_client() click to toggle source
# File lib/logstash/outputs/unomaly.rb, line 214
def make_client
  puts client_config
  Manticore::Client.new(client_config)
end