Class: Fluent::ChefAPIInput

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

Defined Under Namespace

Classes: ChefConfig

Instance Method Summary collapse

Constructor Details

#initializeChefAPIInput

Returns a new instance of ChefAPIInput.



17
18
19
20
# File 'lib/fluent/plugin/in_chef_api.rb', line 17

def initialize
  super
  require "chef-api"
end

Instance Method Details

#configure(conf) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fluent/plugin/in_chef_api.rb', line 53

def configure(conf)
  super
  @config = {}
  if @config_file
    @config = @config.merge(ChefConfig.load_file(@config_file))
  end
  if @chef_server_url
    @config[:endpoint] = @chef_server_url
  end
  if @node_name
    @config[:client] = value
  end
  if @client_key
    @config[:key] = ::File.read(@client_key)
  end
end

#emit_node_metrics(node, data) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fluent/plugin/in_chef_api.rb', line 131

def emit_node_metrics(node, data)
  begin
    Engine.emit("#{@tag}.run_list", Engine.now, data.merge({"value" => node.run_list.length, "node" => node.name}))
    if node.automatic["ohai_time"]
      ohai_time = node.automatic["ohai_time"].to_i
      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}))
    end
  rescue => error
    $log.warn("failed to fetch metrics from node: #{node.name}: #{error.class}: #{error.message}")
  end
end

#runObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fluent/plugin/in_chef_api.rb', line 80

def run
  connection = ChefAPI::Connection.new(@config.dup)
  next_run = ::Time.new
  while @running
    if ::Time.new < next_run
      sleep(1)
    else
      begin
        if @monitor_multi
          run_once(connection)
        else
          run_once_single(connnection)
        end
      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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fluent/plugin/in_chef_api.rb', line 109

def run_once(connection)
  data = @default_values.dup
  if @chef_environment
    nodes = connection.environments.fetch(@chef_environment).nodes
  else
    nodes = connection.nodes
  end
  Engine.emit("#{@tag}.nodes", Engine.now, data.merge({"value" => nodes.count}))
  begin
    nodes.instance_eval do
      if Hash === @collection
        @collection = Hash[@collection.to_a.shuffle]
      end
    end
  rescue => error
    $log.warn("failed to shuffle nodes: #{error.class}: #{error.message}")
  end
  nodes.each do |node|
    emit_node_metrics(node, data)
  end
end

#run_once_single(connection) ⇒ Object



103
104
105
106
107
# File 'lib/fluent/plugin/in_chef_api.rb', line 103

def run_once_single(connection)
  data = @default_values.dup
  node = connection.nodes.fetch(@node_name)
  emit_node_metrics(node, data)
end

#shutdownObject



75
76
77
78
# File 'lib/fluent/plugin/in_chef_api.rb', line 75

def shutdown
  @running = false
  @thread.join
end

#startObject



70
71
72
73
# File 'lib/fluent/plugin/in_chef_api.rb', line 70

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