Class: LogStash::Filters::Exec

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/exec.rb

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/logstash/filters/exec.rb', line 25

def filter(event)
  directory = event.sprintf(@directory) if @directory
  cmd = Shellwords.split(event.sprintf(@cmd))

  process = ChildProcess.build(*cmd)

  stderr = Tempfile.new("stderr")
  stdout = Tempfile.new("stdout")

  process.io.stdout = stdout
  process.io.stderr = stderr

  process.cwd = directory if @directory

  Bundler.with_clean_env do
    process.start
    process.poll_for_exit(timeout)
  end

  stderr.rewind
  stdout.rewind

  response = {
    "exit_code" => process.exit_code,
    "stderr" => stderr.read,
    "stdout" => stdout.read,
    "directory" => directory,
    "cmd" => @cmd
  }

  @logger.debug("Command ran successfully", :cmd => @cmd, :directory => directory)

  event.set(@target, response)
  filter_matched(event)
rescue => e
  response = {
    "exit_code" => 1,
    "stderr" => stderr.read,
    "directory" => directory,
    "cmd" => @cmd,
    "stdout" => stdout.read,
    "exception" => {
      "name" => e.class.name,
      "message" => e.message
    }
  }

  @logger.debug("Command failed", :cmd => cmd, :directory => directory, :message => e.message, :class => e.class.name)

  event.set(@target, response)
  filter_matched(event)
ensure
  stdout.close if stdout
  stderr.close if stderr
end

#registerObject



21
22
# File 'lib/logstash/filters/exec.rb', line 21

def register
end