class Fluent::SplunkAPIOutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 44
def initialize
  super
  require 'net/http/persistent'
  require 'json'
  @idx_indexers = 0
  @indexers = []
end

Public Instance Methods

chunk_to_buffers(chunk) click to toggle source
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 83
def chunk_to_buffers(chunk)
  buffers = {}
  chunk.msgpack_each do |tag, message|
    event = JSON.parse(message)
    uri = get_baseurl(tag, event)
    (buffers[uri] ||= []) << event['payload']
  end
  return buffers
end
configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 52
def configure(conf)
  super

  if @server.match(/,/)
    @indexers = @server.split(',')
  else
    @indexers = [@server]
  end
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 78
def format(tag, time, record)
  event = "#{record.to_json}\n"
  [tag, event].to_msgpack
end
get_baseurl(key, event) click to toggle source
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 124
def get_baseurl(key, event)
  base_url = ''
  @username, @password = @auth.split(':')
  server = @indexers[@idx_indexers];
  @idx_indexers = (@idx_indexers + 1) % @indexers.length
  base_url = "https://#{server}/services/receivers/simple?sourcetype=#{key}"
  base_url += "&host=#{event['host']}"
  base_url += "&index=#{@index}"
  base_url += "&source=#{event['source']}"
  base_url += "&check-index=false" unless @check_index
  base_url
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 70
def shutdown
  # NOTE: call super before @http.shutdown because super may flush final output
  super

  @http.shutdown
  log.debug "shutdown from splunkapi"
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 62
def start
  super
  @http = Net::HTTP::Persistent.new 'fluentd-plugin-splunkapi'
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @verify
  @http.headers['Content-Type'] = 'text/plain'
  log.info "initialized for splunkapi"
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_splunkapi-ssnl.rb, line 93
def write(chunk)
  chunk_to_buffers(chunk).each do |url, messages|
    uri = URI url
    post = Net::HTTP::Post.new uri.request_uri
    post.basic_auth @username, @password
    post.body = messages.join('')
    log.debug "POST #{uri}"
    # retry up to :post_retry_max times
    1.upto(@post_retry_max) do |c|
      response = @http.request uri, post
      log.debug "=> #{response.code} (#{response.message})"
      if response.code == "200"
        # success
        break
      elsif response.code.match(/^40/)
        # user error
        log.error "#{uri}: #{response.code} (#{response.message})\n#{response.body}"
        break
      elsif c < @post_retry_max
        # retry
        sleep @post_retry_interval
        next
      else
        # other errors. fluentd will retry processing on exception
        # FIXME: this may duplicate logs when using multiple buffers
        raise "#{uri}: #{response.message}"
      end
    end
  end
end