diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-06-30 22:30:27 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-06-30 22:30:27 +0000 |
commit | 34e779fcf2bfed1c6874084af6c99e072c4ecc7f (patch) | |
tree | c8d6cf6d26d8b6dfda7b7fe8f814f0e29cfd66de | |
parent | 24f07e0686cc9d81452d33daf215fa050ec89129 (diff) | |
download | puppet-34e779fcf2bfed1c6874084af6c99e072c4ecc7f.tar.gz puppet-34e779fcf2bfed1c6874084af6c99e072c4ecc7f.tar.xz puppet-34e779fcf2bfed1c6874084af6c99e072c4ecc7f.zip |
Significantly redoing metrics. There are now no class variables for metrics, nor no class methods for it.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1350 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet.rb | 5 | ||||
-rw-r--r-- | lib/puppet/metric.rb | 145 | ||||
-rw-r--r-- | lib/puppet/reports/rrdgraph.rb | 20 | ||||
-rwxr-xr-x | lib/puppet/server/report.rb | 1 | ||||
-rw-r--r-- | test/other/metrics.rb | 42 | ||||
-rwxr-xr-x | test/other/report.rb | 34 | ||||
-rwxr-xr-x | test/server/report.rb | 16 |
7 files changed, 101 insertions, 162 deletions
diff --git a/lib/puppet.rb b/lib/puppet.rb index 26b3b8125..71a15fe23 100644 --- a/lib/puppet.rb +++ b/lib/puppet.rb @@ -241,11 +241,6 @@ module Puppet :runinterval => [1800, # 30 minutes "How often puppetd applies the client configuration; in seconds"] ) - self.setdefaults("metrics", - :rrddir => ["$vardir/rrd", - "The directory where RRD database files are stored."], - :rrdgraph => [false, "Whether RRD information should be graphed."] - ) # configuration parameter access and stuff def self.[](param) diff --git a/lib/puppet/metric.rb b/lib/puppet/metric.rb index 4aa2fe3b2..1370a5297 100644 --- a/lib/puppet/metric.rb +++ b/lib/puppet/metric.rb @@ -4,135 +4,17 @@ require 'puppet' module Puppet # A class for handling metrics. This is currently ridiculously hackish. class Metric - def self.init - @@typemetrics = Hash.new { |typehash,typename| - typehash[typename] = Hash.new(0) - } - - @@eventmetrics = Hash.new(0) - - @@metrics = {} - end - - self.init - - def self.clear - self.init - end - - def self.gather - self.init - - # first gather stats about all of the types - Puppet::Type.eachtype { |type| - type.each { |instance| - hash = @@typemetrics[type] - hash[:total] += 1 - if instance.managed? - hash[:managed] += 1 - end - } - } - - # the rest of the metrics are injected directly by type.rb - end - - def self.add(type,instance,metric,count) - return unless defined? @@typemetrics - case metric - when :outofsync: - @@typemetrics[type][metric] += count - when :changes: - @@typemetrics[type][:changed] += 1 - @@typemetrics[type][:totalchanges] += count - else - raise Puppet::DevError, "Unknown metric %s" % metric - end - end - - # we're currently throwing away the type and instance information - def self.addevents(type,instance,events) - return unless defined? @@eventmetrics - events.each { |event| - @@eventmetrics[event] += 1 - } - end - - # Iterate across all of the metrics - def self.each - @@metrics.each { |name,metric| - yield metric - } - end - - # I'm nearly positive this method is used only for testing - def self.load(ary) - @@typemetrics = ary[0] - @@eventmetrics = ary[1] - end - - def self.graph(range = nil) - @@metrics.each { |name,metric| - metric.graph(range) - } - end - - def self.store(time = nil) - require 'RRD' - unless time - time = Time.now.to_i - end - @@metrics.each { |name,metric| - metric.store(time) - } - end - - def self.tally - type = self.new("typecount","Types") - type.newvalue("Number",@@typemetrics.length) - - metrics = { - :total => "Instances", - :managed => "Managed Instances", - :outofsync => "Out of Sync Instances", - :changed => "Changed Instances", - :totalchanges => "Total Number of Changes", - } - total = Hash.new(0) - @@typemetrics.each { |type,instancehash| - name = type.name.to_s - instmet = self.new("type-" + name,name.capitalize) - metrics.each { |symbol,label| - instmet.newvalue(symbol.to_s,instancehash[symbol],label) - total[symbol] += instancehash[symbol] - } - } - - totalmet = self.new("typetotals","Type Totals") - metrics.each { |symbol,label| - totalmet.newvalue(symbol.to_s,total[symbol],label) - } - - eventmet = self.new("events") - total = 0 - @@eventmetrics.each { |event,count| - event = event.to_s - # add the specific event as a value, with the label being a - # capitalized version with s/_/ /g - eventmet.newvalue( - event, - count, - event.capitalize.gsub(/_/,' ') - ) - - total += count - } - eventmet.newvalue("total",total,"Event Total") - end + Puppet.config.setdefaults("metrics", + :rrddir => {:default => "$vardir/rrd", + :owner => "$user", + :group => "$group", + :desc => "The directory where RRD database files are stored." + }, + :rrdgraph => [false, "Whether RRD information should be graphed."] + ) attr_accessor :type, :name, :value, :label - def create Puppet.config.use(:metrics) @@ -156,7 +38,8 @@ module Puppet end def initialize(name,label = nil) - @name = name + @name = name.to_s + if label @label = label else @@ -164,11 +47,6 @@ module Puppet end @values = [] - if @@metrics.include?(self.name) - raise "Somehow created two metrics with name %s" % self.name - else - @@metrics[self.name] = self - end end def newvalue(name,value,label = nil) @@ -183,7 +61,9 @@ module Puppet end def graph(range = nil) + require 'RRD' args = [self.path.sub(/rrd$/,"png")] + args.push("--title",self.label) args.push("--imgformat","PNG") args.push("--interlace") @@ -212,6 +92,7 @@ module Puppet end def store(time) + require 'RRD' unless FileTest.exists?(File.join(Puppet[:rrddir],@name + ".rrd")) self.create end diff --git a/lib/puppet/reports/rrdgraph.rb b/lib/puppet/reports/rrdgraph.rb new file mode 100644 index 000000000..29eca4aeb --- /dev/null +++ b/lib/puppet/reports/rrdgraph.rb @@ -0,0 +1,20 @@ +require 'puppet' + +Puppet::Server::Report.newreport(:rrdgraph) do |report| + time = Time.now.to_i + + File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of| + of.puts "<html><body>" + report.metrics.each do |name, metric| + metric.store(time) + + metric.graph + + of.puts "<img src=%s.png><br>" % name + end + + of.puts "</body></html>" + } +end + +# $Id$ diff --git a/lib/puppet/server/report.rb b/lib/puppet/server/report.rb index 7be44fa44..28005e636 100755 --- a/lib/puppet/server/report.rb +++ b/lib/puppet/server/report.rb @@ -60,6 +60,7 @@ class Server def initialize(*args) super Puppet.config.use(:reporting) + Puppet.config.use(:metrics) end def mkclientdir(client, dir) diff --git a/test/other/metrics.rb b/test/other/metrics.rb index 4cbc1ff1e..c6a563bb2 100644 --- a/test/other/metrics.rb +++ b/test/other/metrics.rb @@ -36,21 +36,27 @@ if $haverrd typehash[type] = Hash.new(0) } eventdata = Hash.new(0) - types.each { |type| - name = type.name - typedata[type] = {} - typedata[type][:total] = rand(totalmax) - typedata[type][:managed] = rand(typedata[type][:total]) - typedata[type][:outofsync] = rand(typedata[type][:managed]) - typedata[type][:changed] = rand(typedata[type][:outofsync]) - typedata[type][:totalchanges] = rand(changemax) - } + 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) } - return [typedata,eventdata] + 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 setup @@ -59,24 +65,20 @@ if $haverrd end def test_fakedata - assert_nothing_raised { Puppet::Metric.init } + report = Puppet::Transaction::Report.new time = Time.now.to_i start = time 10.times { - assert_nothing_raised { Puppet::Metric.load(gendata) } - assert_nothing_raised { Puppet::Metric.tally } - assert_nothing_raised { Puppet::Metric.store(time) } - assert_nothing_raised { Puppet::Metric.clear } + rundata(report, time) time += 300 } - assert_nothing_raised { Puppet::Metric.load(gendata) } - assert_nothing_raised { Puppet::Metric.tally } - assert_nothing_raised { Puppet::Metric.store(time) } - assert_nothing_raised { Puppet::Metric.graph([start,time]) } + rundata(report, time) + + report.metrics.each do |n, m| m.graph end File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of| of.puts "<html><body>" - Puppet::Metric.each { |metric| + report.metrics.each { |name, metric| of.puts "<img src=%s.png><br>" % metric.name } } diff --git a/test/other/report.rb b/test/other/report.rb index 8d6f8541c..8b3cb9517 100755 --- a/test/other/report.rb +++ b/test/other/report.rb @@ -59,6 +59,40 @@ class TestReports < Test::Unit::TestCase ) } end + + def test_rrdgraph_report + Puppet.config.use(:metrics) + # First do some work + objects = [] + 25.times do |i| + file = tempfile() + + # Make every third file + File.open(file, "w") { |f| f.puts "" } if i % 3 == 0 + + objects << Puppet::Type.newfile( + :path => file, + :ensure => "file" + ) + end + + comp = newcomp(*objects) + + trans = nil + assert_nothing_raised("Failed to create transaction") { + trans = comp.evaluate + } + + assert_nothing_raised("Failed to evaluate transaction") { + trans.evaluate + } + + code = Puppet::Server::Report.report("rrdgraph") + + assert_nothing_raised { + code.call(trans.report) + } + end end # $Id$ diff --git a/test/server/report.rb b/test/server/report.rb index 927052936..ba3bece4b 100755 --- a/test/server/report.rb +++ b/test/server/report.rb @@ -34,11 +34,14 @@ class TestServerRunner < Test::Unit::TestCase def test_report # Create a bunch of log messages in an array. - report = [] + report = Puppet::Transaction::Report.new + 10.times { |i| - report << info("Report test message %s" % i) - report[-1].tags = %w{a list of tags} - report[-1].tags << "tag%s" % i + log = info("Report test message %s" % i) + log.tags = %w{a list of tags} + log.tags << "tag%s" % i + + report.newlog(log) } # Now make our reporting client @@ -61,13 +64,16 @@ class TestServerRunner < Test::Unit::TestCase } # Make sure our report is valid and stuff. - report.zip(newreport).each do |ol,nl| + report.logs.zip(newreport.logs).each do |ol,nl| %w{level message time tags source}.each do |method| assert_equal(ol.send(method), nl.send(method), "%s got changed" % method) end end end + + def test_rrdgraph_report + end end # $Id$ |