class Fluent::OhaiInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/ohai_input.rb, line 7
def initialize
  require 'open3'
  require 'json'
  super
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/ohai_input.rb, line 18
def configure(conf)
  super      
  @ohai_cmd = 'ohai'
  if @ohai_path
    @ohai_cmd = File.join(@ohai_path, @ohai_cmd) 
  end

  @interval = Config.time_value(@interval)
end
run() click to toggle source
# File lib/fluent/plugin/ohai_input.rb, line 50
def run

  loop do 
                  
    begin
      stdout, stderr, status = Open3.capture3(@ohai_cmd)
      if status.success?                
        # replace hostname in ohai json if user configured it this way
        json = JSON.parse(stdout)
        json['hostname'] = @hostname
        now = Engine.now
        log.debug "['#{now}'] Emitting ohai data"
        Engine.emit("#{@tag}", now, json)
      else
        log.error "Command '#{@ohai_cmd}' failed with exit code #{status} and message '#{stderr}'"          
      end
    rescue StandardError => e
      log.error "ohai-plugin: failed to run ohai."
      log.error "error: #{e.message}"
      log.error e.backtrace.join("\n")
    end
    sleep @interval      
  end
end
shutdown() click to toggle source
# File lib/fluent/plugin/ohai_input.rb, line 46
def shutdown
  Thread.kill(@thread)
end
start() click to toggle source
# File lib/fluent/plugin/ohai_input.rb, line 28
def start

  # check whether 'ohai' is installed
  begin 
    stdout, stderr, status = Open3.capture3("#{@ohai_cmd} -v")
    if !status.success? || /Ohai/.match(stdout).nil?
      log.error "'ohai' executable does not return desired output. Ignoring plugin ..."
    end
    log.debug "stdout for 'ohai -v' test: #{stdout}"
    log.debug "stderr for 'ohai -v' test: #{stderr}"

  rescue Errno::ENOENT => e 
    log.error "'ohai' executable not found. Ingoring plugin ..."
  end

  @thread = Thread.new(&method(:run))
end