Class: Fluent::ChefClientInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_chef_client.rb

Instance Method Summary collapse

Methods inherited from Input

config_param

Constructor Details

#initializeChefClientInput

Returns a new instance of ChefClientInput.



25
26
27
28
29
# File 'lib/fluent/plugin/in_chef_client.rb', line 25

def initialize
  super
  require "json"
  require "rbconfig"
end

Instance Method Details

#configure(conf) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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_onceObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

#shutdownObject



52
53
54
55
# File 'lib/fluent/plugin/in_chef_client.rb', line 52

def shutdown
  @running = false
  @thread.join
end

#startObject



47
48
49
50
# File 'lib/fluent/plugin/in_chef_client.rb', line 47

def start
  @running = true
  @thread = ::Thread.new(&method(:run))
end