Class: Embulk::Input::Splunk

Inherits:
InputPlugin
  • Object
show all
Defined in:
lib/embulk/input/splunk.rb

Constant Summary collapse

SPLUNK_UNLIMITED_RESULTS =

Zero means unlimited results. Splunk’s default is 100.

0
SPLUNK_TIME_FORMAT =
"%Y-%m-%dT%H:%M:%S.%L%:z"
SPLUNK_OUTPUT_FORMAT =
"json"
SPLUNK_DEFAULT_TIME_FIELD =
"_time"
SPLUNK_TIME_FIELD =
{ "name" => SPLUNK_DEFAULT_TIME_FIELD, "type" => "string" }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resume(task, columns, count, &control) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/embulk/input/splunk.rb', line 53

def self.resume(task, columns, count, &control)
  task_reports = yield(task, columns, count)
  
  next_config_diff = {}
  
  latest_time_in_results = task_reports.first[:latest_time_in_results]
  
  if task["incremental"] && latest_time_in_results.present?
    next_config_diff[:earliest_time] = latest_time_in_results
  end

  return next_config_diff
end

.transaction(config, &control) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/embulk/input/splunk.rb', line 19

def self.transaction(config, &control)

  task = {
    "scheme" => config.param("scheme", :string, default: "https"),
    "host" => config.param("host", :string),
    "port" => config.param("port", :integer, default: 8089),
    "username" => config.param("username", :string),
    "password" => config.param("password", :string),

    "query" => config.param("query", :string),
    
    "earliest_time" => config.param(:earliest_time, :string, default: nil),
    "latest_time" => config.param(:latest_time, :string, default: nil),

    "incremental" => config.param("incremental", :bool, default: false),
    "table" => config.param("table", :array, default: [])
  }
  
  if task["incremental"] && task["latest_time"]
    Embulk.logger.warn "Incremental is 'true' and latest_time is set. This may have unexpected results."
  end
  
  if task["table"].select { |field| field["name"] == "_time" }.empty?
      Embulk.logger.warn "_time is not included in table. Automatically adding it."
      task["table"] << SPLUNK_TIME_FIELD
  end

  columns = task["table"].map do |column|
    Column.new(nil, column["name"], column["type"]&.to_sym || :string, column["format"])
  end

  resume(task, columns, 1, &control)
end

Instance Method Details

#build_query(query) ⇒ Object



87
88
89
90
# File 'lib/embulk/input/splunk.rb', line 87

def build_query(query)
  # Append table expression to query. Even if already present in the query, this should do no harm.
  "#{query} | table #{ @fields.join(", ") } "
end

#initObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/embulk/input/splunk.rb', line 67

def init
  splunk_config = {
    :scheme => task[:scheme],
    :host => task[:host],
    :port => task[:port],
    :username => task[:username],
    :password => task[:password]
  }
  @earliest_time, @latest_time = task[:earliest_time], task[:latest_time]
  Embulk.logger.info "Earliest time:  #{@earliest_time} / Latest time: #{@latest_time}"
  
  @fields = task["table"].collect { |entry| entry["name"] }
  Embulk.logger.info "Using fields #{@fields.join', '} in query"
  
  @query = build_query( task[:query] )
  
  Embulk.logger.info "Establishing connection to Splunk"
  @service = ::Splunk::connect(splunk_config)
end

#runObject



92
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
123
124
# File 'lib/embulk/input/splunk.rb', line 92

def run
  Embulk.logger.info "Running query `#{@query}`"
  
  stream = @service.create_oneshot(@query,
                                   count: SPLUNK_UNLIMITED_RESULTS,
                                   output_format: SPLUNK_OUTPUT_FORMAT,
                                   earliest_time: @earliest_time,
                                   latest_time: @latest_time)

  reader = ::Splunk::ResultsReader.new(stream)
  
  latest_time = nil

  reader.each do |result|
    #We convert event_time to Ruby time for comparison only. 
    event_time = Time.strptime( result[SPLUNK_DEFAULT_TIME_FIELD], SPLUNK_TIME_FORMAT )
    
    #We need to keep track of latest time for incremental loads. 
    # Unfortunately, Splunk was not respecting our sort requests, so we need to do a comparison for each row.
    latest_time = latest_time.nil? ? event_time : [latest_time, event_time].max

    row = @fields.map { |field| result[ field ] }
    page_builder.add( row )
  end
  
  page_builder.finish
  
  task_result = {
    latest_time_in_results: latest_time.strftime(SPLUNK_TIME_FORMAT)
  }

  return task_result
end