Class: Fluent::SplunkAPIOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::SplunkAPIOutput
- Defined in:
- lib/fluent/plugin/out_splunkapi.rb
Instance Method Summary collapse
- #chunk_to_buffers(chunk) ⇒ Object
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ SplunkAPIOutput
constructor
A new instance of SplunkAPIOutput.
- #record_to_kvp(record) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #write(chunk) ⇒ Object
Constructor Details
#initialize ⇒ SplunkAPIOutput
Returns a new instance of SplunkAPIOutput.
55 56 57 58 59 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 55 def initialize super require 'net/http/persistent' require 'time' end |
Instance Method Details
#chunk_to_buffers(chunk) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 150 def chunk_to_buffers(chunk) buffers = {} chunk.msgpack_each do |tag, event| (buffers[@source_formatter.call(tag)] ||= []) << event end return buffers end |
#configure(conf) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 61 def configure(conf) super case @source when '{TAG}' @source_formatter = lambda { |tag| tag } else @source_formatter = lambda { |tag| @source.sub('{TAG}', tag) } end case @time_format when 'none' @time_formatter = nil when 'unixtime' @time_formatter = lambda { |time| time.to_s } when 'localtime' @time_formatter = lambda { |time| Time.at(time).localtime } else @timef = TimeFormatter.new(@time_format, @localtime) @time_formatter = lambda { |time| @timef.format(time) } end case @format when 'json' @formatter = lambda { |record| record.to_json } when 'kvp' @formatter = lambda { |record| record_to_kvp(record) } when 'text' @formatter = lambda { |record| = record['message'] record.delete('message') if record.length == 0 else "[#{record_to_kvp(record)}] #{message}" end } end if @protocol == 'rest' @username, @password = @auth.split(':') @base_url = "https://#{@server}/services/receivers/simple?sourcetype=#{@sourcetype}" @base_url += "&host=#{@host}" if @host @base_url += "&index=#{@index}" if @index @base_url += "&check-index=false" unless @check_index elsif @protocol == 'storm' @username, @password = 'x', @access_token @base_url = "https://#{@api_hostname}/1/inputs/http?index=#{@project_id}&sourcetype=#{@sourcetype}" @base_url += "&host=#{@host}" if @host end end |
#format(tag, time, record) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 137 def format(tag, time, record) if @time_formatter time_str = "#{@time_formatter.call(time)}: " else time_str = '' end record.delete('time') event = time_str + @formatter.call(record) + "\n" [tag, event].to_msgpack end |
#record_to_kvp(record) ⇒ Object
117 118 119 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 117 def record_to_kvp(record) record.map {|k,v| v == nil ? "#{k}=" : "#{k}=\"#{v}\""}.join(' ') end |
#shutdown ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 129 def shutdown # NOTE: call super before @http.shutdown because super may flush final output super @http.shutdown $log.debug "shutdown from #{@base_url}" end |
#start ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 121 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.debug "initialized for #{@base_url}" end |
#write(chunk) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fluent/plugin/out_splunkapi.rb', line 158 def write(chunk) chunk_to_buffers(chunk).each do |source, | uri = URI @base_url + "&source=#{source}" post = Net::HTTP::Post.new uri.request_uri post.basic_auth @username, @password post.body = .join('') $log.debug "POST #{uri}" # retry up to :post_retry_max times 1.upto(@post_retry_max) do |c| response = @http.request uri, post break if response.code != "503" $log.debug "=> #{response.code} (#{response.message})" sleep @post_retry_interval # fluentd will retry processing on exception # FIXME: this may duplicate logs with multiple buffers raise "#{uri}: #{response.message}" if c == @post_retry_max end end end |