Class: Fluent::SplunkHECOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_splunkhec.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object

This method is called before starting. Here we construct the Splunk HEC URL to POST data to If the configuration is invalid, raise Fluent::ConfigError.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/out_splunkhec.rb', line 27

def configure(conf)
  super
  @splunk_url = @protocol + '://' + @host + ':' + @port + '/services/collector/event'
  log.debug 'splunkhec: sent data to ' + @splunk_url

  if conf['event_host'] == nil
    begin
      @event_host = `hostname`.delete!("\n")
    rescue
      @event_host = 'unknown'
    end
  end
end

#format(tag, time, record) ⇒ Object

This method is called when an event reaches to Fluentd. Use msgpack to serialize the object.



51
52
53
# File 'lib/fluent/plugin/out_splunkhec.rb', line 51

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#send_to_splunk(body) ⇒ Object



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
123
124
125
126
127
# File 'lib/fluent/plugin/out_splunkhec.rb', line 98

def send_to_splunk(body)
  log.debug "splunkhec: " + body + "\n"

  uri = URI(@splunk_url)

  # Create client
  http = Net::HTTP.new(uri.host, uri.port)

  # Create Request
  req = Net::HTTP::Post.new(uri)
  # Add headers
  req.add_field "Authorization", "Splunk #{@token}"
  # Add headers
  req.add_field "Content-Type", "application/json; charset=utf-8"
  # Set body
  req.body = body
  # Handle SSL
  if @protocol == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  # Fetch Request
  res = http.request(req)
  log.debug "splunkhec: response HTTP Status Code is #{res.code}"
  if res.code.to_i != 200
    body = JSON.parse(res.body)
    raise SplunkHECOutputError.new(body['text'], body['code'], body['invalid-event-number'], res.code)
  end
end

#shutdownObject



45
46
47
# File 'lib/fluent/plugin/out_splunkhec.rb', line 45

def shutdown
  super
end

#startObject



41
42
43
# File 'lib/fluent/plugin/out_splunkhec.rb', line 41

def start
  super
end

#write(chunk) ⇒ Object

Loop through all records and sent them to Splunk



56
57
58
59
60
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
# File 'lib/fluent/plugin/out_splunkhec.rb', line 56

def write(chunk)
  body = ''
  chunk.msgpack_each {|(tag,time,record)|
    # Parse record to Splunk event format
    case record
    when Fixnum
      event = record.to_s
    when Hash
      if @send_event_as_json
        event = record.to_json
      else
        event = record.to_json.gsub("\"", %q(\\\"))
      end
    else
      event = record
    end

    sourcetype = @sourcetype == 'tag' ? tag : @sourcetype

    # Build body for the POST request
    if !@usejson
      event = record["time"]+ " " + record["message"].to_json.gsub(/^"|"$/,"")
      body << '{"time":"'+ DateTime.parse(record["time"]).strftime("%Q") +'", "event":"' + event + '", "sourcetype" :"' + sourcetype + '", "source" :"' + @source + '", "index" :"' + @index + '", "host" : "' + @event_host + '"}'
    elsif @send_event_as_json
      body << '{"time" :' + time.to_s + ', "event" :' + event + ', "sourcetype" :"' + sourcetype + '", "source" :"' + @source + '", "index" :"' + @index + '", "host" : "' + @event_host + '"}'
    else
      body << '{"time" :' + time.to_s + ', "event" :"' + event + '", "sourcetype" :"' + sourcetype + '", "source" :"' + @source + '", "index" :"' + @index + '", "host" : "' + @event_host + '"}'
    end

    if @send_batched_events
      body << "\n"
    else
      send_to_splunk(body)
      body = ''
    end
  }

  if @send_batched_events
    send_to_splunk(body)
  end
end