class LogStash::Outputs::LMLogs

An example output that does nothing.

Public Instance Methods

client() click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 153
def client
  @client ||= make_client
end
client_config() click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 110
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 @access_id
    if !@access_key || !@access_key.value
      raise ::LogStash::ConfigurationError, "access_id '#{@access_id}' specified without access_key!"
    end

    # Symbolize keys if necessary
    c[:auth] = {
        :access_id => @access_id,
        :access_key => @access_key.value,
        :eager => true
    }
  end
end
close() click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 158
def close
  @client.close
end
generate_auth_string(body) click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 163
def generate_auth_string(body)
  timestamp = DateTime.now.strftime('%Q')
  hash_this = "POST#{timestamp}#{body}/log/ingest"
  sign_this = OpenSSL::HMAC.hexdigest(
                OpenSSL::Digest.new('sha256'),
                "#{@access_key.value}",
                hash_this
              )
  signature = Base64.strict_encode64(sign_this)
  "LMv1 #{@access_id}:#{signature}:#{timestamp}"
end
isValidPayloadSize(documents,lmlogs_event,max_payload_size) click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 273
def isValidPayloadSize(documents,lmlogs_event,max_payload_size)
  if (documents.to_json.bytesize + lmlogs_event.to_json.bytesize) >  max_payload_size 
        send_batch(documents)
        documents = []
        
  end
  documents.push(lmlogs_event)
  return documents
end
log_failure(message, opts) click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 269
def log_failure(message, opts)
  @logger.error("[HTTP Output Failure] #{message}", opts)
end
multi_receive(events) click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 238
def multi_receive(events)
  puts @@MAX_PAYLOAD_SIZE
  if debug
    puts events.to_json
  end

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

      lmlogs_event["_lm.resourceId"] = {}
      lmlogs_event["_lm.resourceId"]["#{@lm_property}"] = event.get(@property_key.to_s)

      if @keep_timestamp
        lmlogs_event["timestamp"] = event.get("@timestamp")
      end
      
      if @timestamp_is_key
        lmlogs_event["timestamp"] = event.get(@timestamp_key.to_s)
      end

      documents = isValidPayloadSize(documents,lmlogs_event,@@MAX_PAYLOAD_SIZE)

    end
    send_batch(documents)
  end
end
register() click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 102
def register
  @total = 0
  @total_failed = 0
  logger.info("Initialized LogicMonitor output plugin with configuration",
              :host => @host)

end
send_batch(events) click to toggle source
# File lib/logstash/outputs/lmlogs.rb, line 175
def send_batch(events)
  url = "https://" + @portal_name + ".logicmonitor.com/rest/log/ingest"
  body = events.to_json
  auth_string = generate_auth_string(body)
  request = client.post(url, {
      :body => body,
      :headers => {
              "Content-Type" => "application/json",
              "User-Agent" => "LM Logs Logstash Plugin",
              "Authorization" => "#{auth_string}"
      }
  })

  request.on_success do |response|
    if response.code == 202
      @total += events.length
      @logger.debug("Successfully sent ",
                    :response_code => response.code,
                    :batch_size => events.length,
                    :total_sent => @total,
                    :time => Time::now.utc)
    elsif response.code == 207
      log_failure(
        "207 HTTP code - some of the events successfully parsed, some not. ",
        :response_code => response.code,
        :url => url,
        :response_body => response.body,
        :total_failed => @total_failed)
    else
      @total_failed += 1
      log_failure(
          "Encountered non-202/207 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 LM Logs",
                :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/lmlogs.rb, line 147
def make_client
  puts client_config
  Manticore::Client.new(client_config)
end