diff options
-rw-r--r-- | CHANGELOG | 3 | ||||
-rw-r--r-- | lib/puppet/util/metric.rb | 27 | ||||
-rwxr-xr-x | test/util/metrics.rb | 109 |
3 files changed, 71 insertions, 68 deletions
@@ -1,3 +1,6 @@ + Replaced the obsolete RRD ruby library with the maintained + RubyRRDtool library (which requires rrdtool2) (#659). + The Portage package provider now calls eix-update automatically when eix's database is absent or out of sync (#666). diff --git a/lib/puppet/util/metric.rb b/lib/puppet/util/metric.rb index 3e6378808..5a54f602c 100644 --- a/lib/puppet/util/metric.rb +++ b/lib/puppet/util/metric.rb @@ -5,7 +5,7 @@ require 'puppet' class Puppet::Util::Metric # Load the library as a feature, so we can test its presence. - Puppet.features.add :rrd, :libs => 'RRD' + Puppet.features.add :rrd, :libs => 'RRDtool' attr_accessor :type, :name, :value, :label attr_writer :values @@ -25,12 +25,8 @@ class Puppet::Util::Metric start ||= Time.now.to_i - 5 - path = self.path - args = [ - path, - "--start", start, - "--step", Puppet[:rrdinterval] - ] + @rrd = RRDtool.new(self.path) + args = [] values.each { |value| # the 7200 is the heartbeat -- this means that any data that isn't @@ -40,14 +36,14 @@ class Puppet::Util::Metric args.push "RRA:AVERAGE:0.5:1:300" begin - RRD.create(*args) + @rrd.create( Puppet[:rrdinterval].to_i, start, args) rescue => detail raise "Could not create RRD file %s: %s" % [path,detail] end end def dump - puts RRD.info(self.path) + puts @rrd.info end def graph(range = nil) @@ -57,7 +53,7 @@ class Puppet::Util::Metric end unit = 60 * 60 * 24 - colorstack = %w{#ff0000 #00ff00 #0000ff #099000 #000990 #f00990 #0f0f0f} + colorstack = %w{#ff0000 #00ff00 #0000ff #ffff00 #ff99ff #ff9966 #66ffff #990000 #099000 #000990 #f00990 #0f0f0f #555555 #333333 #ffffff} {:daily => unit, :weekly => unit * 7, :monthly => unit * 30, :yearly => unit * 365}.each do |name, time| file = self.path.sub(/\.rrd$/, "-%s.png" % name) @@ -86,7 +82,8 @@ class Puppet::Util::Metric end begin - RRD.graph(*args) + #Puppet.warning "args = #{args}" + RRDtool.graph( args ) rescue => detail Puppet.err "Failed to graph %s: %s" % [self.name,detail] end @@ -125,14 +122,20 @@ class Puppet::Util::Metric self.create(time - 5) end + @rrd ||= RRDtool.new(self.path) + # XXX this is not terribly error-resistant args = [time] + temps = [] values.each { |value| + #Puppet.warning "value[0]: #{value[0]}; value[1]: #{value[1]}; value[2]: #{value[2]}; " args.push value[2] + temps.push value[0] } arg = args.join(":") + template = temps.join(":") begin - RRD.update(self.path,arg) + @rrd.update( template, [ arg ] ) #system("rrdtool updatev %s '%s'" % [self.path, arg]) rescue => detail raise Puppet::Error, "Failed to update %s: %s" % [self.name,detail] diff --git a/test/util/metrics.rb b/test/util/metrics.rb index c4e375e7b..fd997df17 100755 --- a/test/util/metrics.rb +++ b/test/util/metrics.rb @@ -7,75 +7,72 @@ require 'puppet/util/metric' require 'puppettest' require 'puppet/type' -if Puppet.features.rrd? - class TestMetric < Test::Unit::TestCase - include PuppetTest +class TestMetric < PuppetTest::TestCase + confine "Missing RRDtool library" => Puppet.features.rrd? + include PuppetTest - def gendata - totalmax = 1000 - changemax = 1000 - eventmax = 10 - maxdiff = 10 + def gendata + totalmax = 1000 + changemax = 1000 + eventmax = 10 + maxdiff = 10 - types = [Puppet.type(:file), Puppet.type(:package), Puppet.type(:package)] - data = [:total, :managed, :outofsync, :changed, :totalchanges] - events = [:file_changed, :package_installed, :service_started] + types = [Puppet.type(:file), Puppet.type(:package), Puppet.type(:package)] + data = [:total, :managed, :outofsync, :changed, :totalchanges] + events = [:file_changed, :package_installed, :service_started] - # if this is the first set of data points... - typedata = Hash.new { |typehash,type| - typehash[type] = Hash.new(0) - } - eventdata = Hash.new(0) - typedata = {} - typedata[:total] = rand(totalmax) - typedata[:managed] = rand(typedata[:total]) - typedata[:outofsync] = rand(typedata[:managed]) - typedata[:changed] = rand(typedata[:outofsync]) - typedata[:totalchanges] = rand(changemax) + # if this is the first set of data points... + typedata = Hash.new { |typehash,type| + typehash[type] = Hash.new(0) + } + eventdata = Hash.new(0) + typedata = {} + typedata[:total] = rand(totalmax) + typedata[:managed] = rand(typedata[:total]) + typedata[:outofsync] = rand(typedata[:managed]) + typedata[:changed] = rand(typedata[:outofsync]) + typedata[:totalchanges] = rand(changemax) - events.each { |event| - eventdata[event] = rand(eventmax) - } + events.each { |event| + eventdata[event] = rand(eventmax) + } - return {:typedata => typedata, :eventdata => eventdata} - end + return {:typedata => typedata, :eventdata => eventdata} + end - def rundata(report, time) - assert_nothing_raised { - gendata.each do |name, data| - report.newmetric(name, data) - end - report.metrics.each { |n, m| m.store(time) } - } - end + def rundata(report, time) + assert_nothing_raised { + gendata.each do |name, data| + report.newmetric(name, data) + end + report.metrics.each { |n, m| m.store(time) } + } + end - def setup - super - Puppet[:rrdgraph] = true - end + def setup + super + Puppet[:rrdgraph] = true + end - def test_fakedata - report = Puppet::Transaction::Report.new - time = Time.now.to_i - start = time - 10.times { - rundata(report, time) - time += 300 - } + def test_fakedata + report = Puppet::Transaction::Report.new + time = Time.now.to_i + start = time + 10.times { rundata(report, time) + time += 300 + } + rundata(report, time) - report.metrics.each do |n, m| m.graph end + report.metrics.each do |n, m| m.graph end - File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of| - of.puts "<html><body>" - report.metrics.each { |name, metric| - of.puts "<img src=%s.png><br>" % metric.name - } + File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of| + of.puts "<html><body>" + report.metrics.each { |name, metric| + of.puts "<img src=%s.png><br>" % metric.name } - end + } end -else - $stderr.puts "Missing RRD library -- skipping metric tests" end # $Id$ |