class LogStash::Inputs::Ngn

Periodically run a shell command and capture the whole output as an event.

Notes:

Public Instance Methods

execute(queue) click to toggle source

Execute a given script @param [String] A script string @param [Array or Queue] A queue to append events to

# File lib/logstash/inputs/ngn.rb, line 78
def execute(queue)
  start = Time.now
  output = exit_status = nil
  begin
    @logger.debug? && @logger.debug("Running exec", :script => @script)
    output, exit_status = run_script()
  rescue StandardError => e
    @logger.error("Error while running script",
      :script => @script, :e => e, :backtrace => e.backtrace)
  rescue Exception => e
    @logger.error("Exception while running script",
      :script => @script, :e => e, :backtrace => e.backtrace)
  end
  duration = Time.now - start
  @logger.debug? && @logger.debug("Command completed", :script => @script, :duration => duration)
  if output
    @codec.decode(output) do |decoded|

      @token = decoded.get("token")

      decoded.get("values").each do |val|
        event = LogStash::Event.new(val)
        decorate(event)
        event.set("[@metadata][host]", @hostname)
        event.set("[@metadata][duration]", duration)
        event.set("[@metadata][exit_status]", exit_status)
        queue << event
      end
    end
  end
  duration
end
register() click to toggle source
# File lib/logstash/inputs/ngn.rb, line 45
def register
  @logger.info("Registering Exec Input", :type => @type, :script => @script, :interval => @interval, :schedule => @schedule)
  @hostname = Socket.gethostname
  @io       = nil
  
  if (@interval.nil? && @schedule.nil?) || (@interval && @schedule)
    raise LogStash::ConfigurationError, "jdbc input: either 'interval' or 'schedule' option must be defined."
  end
end
run(queue) click to toggle source
# File lib/logstash/inputs/ngn.rb, line 55
def run(queue)
  if @schedule
    @scheduler = Rufus::Scheduler.new(:max_work_threads => 1)
    @scheduler.cron @schedule do
      execute(queue)
    end
    @scheduler.join
  else
    while !stop?
      duration = execute(queue)
      wait_until_end_of_interval(duration)
    end # loop
  end
end
stop() click to toggle source
# File lib/logstash/inputs/ngn.rb, line 70
def stop
  close_io()
  @scheduler.shutdown(:wait) if @scheduler
end

Private Instance Methods

close_io() click to toggle source

Close @io

# File lib/logstash/inputs/ngn.rb, line 131
def close_io
  return if @io.nil? || @io.closed?
  @io.close
  @io = nil
end
run_script() click to toggle source
# File lib/logstash/inputs/ngn.rb, line 113
def run_script
  command = "python3 #{@script} -u #{@username} -p #{@password} -e #{@env}"
  if @token
    command << " -t \"#{@token}\""
  end

  @logger.debug? && @logger.debug("Running exec", :command => command)
    
  @io = IO.popen(command)
  output = @io.read
  @io.close # required in order to read $?
  exit_status = $?.exitstatus # should be threadsafe as per rb_thread_save_context
  [output, exit_status]
ensure
  close_io()
end
wait_until_end_of_interval(duration) click to toggle source

Wait until the end of the interval @param [Integer] the duration of the last command executed

# File lib/logstash/inputs/ngn.rb, line 139
def wait_until_end_of_interval(duration)
  # 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
    Stud.stoppable_sleep(sleeptime) { stop? }
  else
    @logger.warn("Execution ran longer than the interval. Skipping sleep.",
                 :script => @script, :duration => duration, :interval => @interval)
  end
end