class LogStash::Inputs::Ngc
Periodically run a shell command and capture the whole output as an event.
Notes:
-
The `script` field of this event will be the python script run.
-
The `api_key` field of this event will be the key used to access NGC.
Public Instance Methods
execute(queue)
click to toggle source
Execute a given command @param [String] A command string @param [Array or Queue] A queue to append events to
# File lib/logstash/inputs/ngc.rb, line 81 def execute(queue) start = Time.now output = exit_status = nil begin 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| @jwt_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/ngc.rb, line 48 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/ngc.rb, line 58 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/ngc.rb, line 73 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/ngc.rb, line 133 def close_io return if @io.nil? || @io.closed? @io.close @io = nil end
run_script()
click to toggle source
# File lib/logstash/inputs/ngc.rb, line 115 def run_script command = "python3 #{@script} -k #{@api_key} -e #{@env} --auth_url #{@auth_url} --api_url #{@api_url}" if @jwt_token command << " -t #{@jwt_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/ngc.rb, line 141 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