Class: GraphiteReporting

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef-handler-graphite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GraphiteReporting

Returns a new instance of GraphiteReporting.



28
29
30
31
32
# File 'lib/chef-handler-graphite.rb', line 28

def initialize(options = {})
  @metric_key = options[:metric_key]
  @graphite_host = options[:graphite_host]
  @graphite_port = options[:graphite_port]
end

Instance Attribute Details

#graphite_host=(value) ⇒ Object (writeonly)

Sets the attribute graphite_host

Parameters:

  • value

    the value to set the attribute graphite_host to.



26
27
28
# File 'lib/chef-handler-graphite.rb', line 26

def graphite_host=(value)
  @graphite_host = value
end

#graphite_port=(value) ⇒ Object (writeonly)

Sets the attribute graphite_port

Parameters:

  • value

    the value to set the attribute graphite_port to.



26
27
28
# File 'lib/chef-handler-graphite.rb', line 26

def graphite_port=(value)
  @graphite_port = value
end

#metric_key=(value) ⇒ Object (writeonly)

Sets the attribute metric_key

Parameters:

  • value

    the value to set the attribute metric_key to.



26
27
28
# File 'lib/chef-handler-graphite.rb', line 26

def metric_key=(value)
  @metric_key = value
end

Instance Method Details

#reportObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef-handler-graphite.rb', line 34

def report
  gemspec = if Gem::Specification.respond_to? :find_by_name
    Gem::Specification.find_by_name('chef-handler-graphite')
  else
    Gem.source_index.find_name('chef-handler-graphite').last
  end

  Chef::Log.debug("#{gemspec.full_name} loaded as a handler.")

  g = Graphite.new
  g.host = @graphite_host
  g.port = @graphite_port

  metrics = Hash.new
  metrics[:updated_resources] = run_status.updated_resources.length if run_status.updated_resources
  metrics[:all_resources] = run_status.all_resources.length if run_status.all_resources
  metrics[:elapsed_time] = run_status.elapsed_time

  if run_status.success?
    metrics[:success] = 1
    metrics[:fail] = 0
  else
    metrics[:success] = 0
    metrics[:fail] = 1
  end

  g.push_to_graphite do |graphite|
    metrics.each do |metric, value|
      Chef::Log.debug("#{@metric_key}.#{metric} #{value} #{g.time_now}")
      graphite.puts "#{@metric_key}.#{metric} #{value} #{g.time_now}"
    end
  end
end