class Fluent::ChefClientInput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_chef_client.rb, line 25
def initialize
  super
  require "json"
  require "rbconfig"
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_chef_client.rb, line 31
def configure(conf)
  super
  @chef_config = {
    :config_file     => @config_file,
    :chef_server_url => @chef_server_url,
    :client_key      => @client_key,
    :node_name       => @node_name,
  }
  ruby = ::File.join(::RbConfig::CONFIG["bindir"], ::RbConfig::CONFIG["ruby_install_name"])
  if ::File.executable?(ruby)
    @ruby = ruby
  else
    @ruby = "ruby"
  end
end
run() click to toggle source
# File lib/fluent/plugin/in_chef_client.rb, line 57
def run
  next_run = ::Time.new
  while @running
    if ::Time.new < next_run
      sleep(1)
    else
      begin
        now = Engine.now
        data = nil
        $log.debug("invoking process: #{@ruby} #{__FILE__}")
        ::IO.popen([@ruby, __FILE__], "r+") do |io|
          io.write(::JSON.dump(@chef_config))
          io.close_write
          data = ::JSON.load(io.read)
        end
        $log.debug("#{File.basename(__FILE__).dump} exits as #{$?.exitstatus}")
        if $?.exitstatus == 0
          data.each do |key, val|
            Engine.emit("#{@tag}.#{key}", now, {"value" => val})
          end
          if ::Numeric === data["ohai_time"]
            Engine.emit("#{@tag}.behind_seconds", now, {"value" => now - data["ohai_time"]})
          end
        else
          raise("invalid response from #{__FILE__.dump}")
        end
      rescue => error
        $log.warn("failed to load attributes: #{error.inspect}")
        next
      ensure
        next_run = ::Time.new + @check_interval
      end
    end
  end
end
run_once() click to toggle source
# File lib/fluent/plugin/in_chef_client.rb, line 93
def run_once
  require "chef"
  if @config_file
    ::Chef::Config.from_file(@config_file)
  end
  if @chef_server_url
    ::Chef::Config[:chef_server_url] = @chef_server_url
  end
  if @client_key
    ::Chef::Config[:client_key] = @client_key
  end
  if @node_name
    ::Chef::Config[:node_name] = @node_name
  end
  node_name = ::Chef::Config[:node_name]
  node = ::Chef::Node.load(node_name)
  data = ::Hash[["ohai_time", "idletime_seconds", "uptime_seconds"].map { |attr| [attr, node[attr]] }]
  STDOUT.puts(::JSON.dump(data))
end
shutdown() click to toggle source
# File lib/fluent/plugin/in_chef_client.rb, line 52
def shutdown
  @running = false
  @thread.join
end
start() click to toggle source
# File lib/fluent/plugin/in_chef_client.rb, line 47
def start
  @running = true
  @thread = ::Thread.new(&method(:run))
end