pairing with luke, nagios_command provider skeleton
[vinpup.git] / test / util / metrics.rb
blobb0ac1e2f5f49da25ccac2ef2f558f6b52ea109f9
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../lib/puppettest'
5 require 'puppet'
6 require 'puppet/util/metric'
7 require 'puppettest'
8 require 'puppet/type'
10 class TestMetric < PuppetTest::TestCase
11     confine "Missing RRDtool library" =>  Puppet.features.rrd?
12     include PuppetTest
14     def gendata
15         totalmax = 1000
16         changemax = 1000
17         eventmax = 10
18         maxdiff = 10
20         types = [Puppet.type(:file), Puppet.type(:package), Puppet.type(:package)]
21         data = [:total, :managed, :outofsync, :changed, :totalchanges]
22         events = [:file_changed, :package_installed, :service_started]
24         # if this is the first set of data points...
25         typedata = Hash.new { |typehash,type|
26             typehash[type] = Hash.new(0)
27         }
28         eventdata = Hash.new(0)
29         typedata = {}
30         typedata[:total] = rand(totalmax)
31         typedata[:managed] = rand(typedata[:total])
32         typedata[:outofsync] = rand(typedata[:managed])
33         typedata[:changed] = rand(typedata[:outofsync])
34         typedata[:totalchanges] = rand(changemax)
36         events.each { |event|
37             eventdata[event] = rand(eventmax)
38         }
40         return {:typedata => typedata, :eventdata => eventdata}
41     end
43     def rundata(report, time)
44         assert_nothing_raised {
45             gendata.each do |name, data|
46                 report.newmetric(name, data)
47             end
48             report.metrics.each { |n, m| m.store(time) }
49         }
50     end
52     def setup
53         super
54         Puppet[:rrdgraph] = true
55     end
57     def test_fakedata
58         report = Puppet::Transaction::Report.new
59         time = Time.now.to_i
60         start = time
61         10.times {
62             rundata(report, time)
63             time += 300
64         }
65         rundata(report, time)
67         report.metrics.each do |n, m| m.graph end
69         File.open(File.join(Puppet[:rrddir],"index.html"),"w") { |of|
70             of.puts "<html><body>"
71             report.metrics.each { |name, metric|
72                 of.puts "<img src=%s.png><br>" % metric.name
73             }
74         }
75     end
76 end