Class: Fluent::SplunkAPIOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_splunkapi-ssnl.rb

Instance Method Summary collapse

Constructor Details

#initializeSplunkAPIOutput

Returns a new instance of SplunkAPIOutput.



44
45
46
47
48
49
50
# File 'lib/fluent/plugin/out_splunkapi-ssnl.rb', line 44

def initialize
  super
  require 'net/http/persistent'
  require 'json'
  @idx_indexers = 0
  @indexers = []
end

Instance Method Details

#chunk_to_buffers(chunk) ⇒ Object



83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



78
79
80
81
# 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) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# 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

#shutdownObject



70
71
72
73
74
75
76
# 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

#startObject



62
63
64
65
66
67
68
# 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) ⇒ Object



93
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
121
122
# 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