Class: Fluent::ChefAPIInput

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

Instance Method Summary collapse

Constructor Details

#initializeChefAPIInput

Returns a new instance of ChefAPIInput.



15
16
17
18
# File 'lib/fluent/plugin/in_chef_api.rb', line 15

def initialize
  super
  require "chef-api"
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
# File 'lib/fluent/plugin/in_chef_api.rb', line 20

def configure(conf)
  super
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fluent/plugin/in_chef_api.rb', line 34

def run
  connection = ChefAPI::Connection.new(
    :endpoint => @chef_server_url,
    :client   => @node_name,
    :key      => ::File.read(@client_key),
  )
  next_run = ::Time.new
  while @running
    if ::Time.new < next_run
      sleep(1)
    else
      begin
        run_once(connection)
      rescue => error
        $log.warn("failed to fetch metrics: #{error.class}: #{error.message}")
        next
      ensure
        next_run = ::Time.new + @check_interval
      end
    end
  end
end

#run_once(connection) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fluent/plugin/in_chef_api.rb', line 57

def run_once(connection)
  if @chef_environment
    nodes = connection.environments.fetch(@chef_environment).nodes
    data = {"chef_environment" => @chef_environment}
  else
    nodes = connection.nodes
    data = {}
  end
  Engine.emit("#{@tag}.nodes", Engine.now, data.merge({"value" => nodes.count}))
  nodes.each do |node|
    begin
      Engine.emit("#{@tag}.run_list", Engine.now, data.merge({"value" => node.run_list.length, "node" => node.name}))
      ohai_time = node.automatic.fetch("ohai_time")
      Engine.emit("#{@tag}.ohai_time", Engine.now, data.merge({"value" => ohai_time, "node" => node.name}))
      Engine.emit("#{@tag}.behind_seconds", Engine.now, data.merge({"value" => Time.new.to_i - ohai_time, "node" => node.name}))
    rescue => error
      $log.warn("failed to fetch metrics from node: #{node.name}: #{error.class}: #{error.message}")
    end
  end
end

#shutdownObject



29
30
31
32
# File 'lib/fluent/plugin/in_chef_api.rb', line 29

def shutdown
  @running = false
  @thread.join
end

#startObject



24
25
26
27
# File 'lib/fluent/plugin/in_chef_api.rb', line 24

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