Class: LogStash::Inputs::Exec

Inherits:
Base show all
Defined in:
lib/logstash/inputs/exec.rb

Overview

Run command line tools and capture the whole output as an event.

Notes:

  • The ‘@source’ of this event will be the command run.

  • The ‘@message’ of this event will be the entire stdout of the command as one event.

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes inherited from Base

#params, #threadable

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#initialize, #tag

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Inputs::Base

Instance Method Details

#registerObject



31
32
33
34
# File 'lib/logstash/inputs/exec.rb', line 31

def register
  @logger.info("Registering Exec Input", :type => @type,
               :command => @command, :interval => @interval)
end

#run(queue) ⇒ Object



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
# File 'lib/logstash/inputs/exec.rb', line 37

def run(queue)
  hostname = Socket.gethostname
  loop do
    start = Time.now
    @logger.info("Running exec", :command => @command) if @debug
    out = IO.popen(@command)
    # out.read will block until the process finishes.
    @codec.decode(out.read) do |event|
      decorate(event)
      event["host"] = hostname
      event["command"] = @command
      queue << event
    end
    
    duration = Time.now - start
    if @debug
      @logger.info("Command completed", :command => @command,
                   :duration => duration)
    end

    # Sleep for the remainder of the interval, or 0 if the duration ran
    # longer than the interval.
    sleeptime = [0, @interval - duration].max
    if sleeptime == 0
      @logger.warn("Execution ran longer than the interval. Skipping sleep.",
                   :command => @command, :duration => duration,
                   :interval => @interval)
    else
      sleep(sleeptime)
    end
  end # loop
end