class RRDRepo
Attributes
filename[R]
Public Class Methods
create(filename, datasources, start = Time.now)
click to toggle source
# File lib/rrd_repo.rb, line 6 def self.create(filename, datasources, start = Time.now) one_day = 60 * 60 * 24 rrd = RRD::Base.new(filename) rrd.create(:start => start, :step => 60) do datasources.each do |name| datasource name, :type => :gauge, :heartbeat => 120, :min => 0, :max => :unlimited end archive :average, :every => 60, :during => one_day archive :average, :every => 300, :during => one_day end RRDRepo.new(filename) end
new(filename)
click to toggle source
# File lib/rrd_repo.rb, line 20 def initialize(filename) @filename = filename end
Public Instance Methods
_rrd()
click to toggle source
# File lib/rrd_repo.rb, line 61 def _rrd raise("Unable to open RRD, because it doesn't exist: #{@filename}") unless File.exist?(@filename) @rrd ||= RRD::Base.new(@filename) end
info()
click to toggle source
# File lib/rrd_repo.rb, line 28 def info info = _rrd.info archives = unflatten_archives(info) { :step => info["step"], :archives => archives.map do |archive| { :consolidation_function => archive["cf"], :resolution_sec => info["step"] * archive["pdp_per_row"], :duration_sec => info["step"] * archive["pdp_per_row"] * archive["rows"] } end } end
latest_value(datasource)
click to toggle source
# File lib/rrd_repo.rb, line 24 def latest_value(datasource) _rrd.info["ds[#{datasource}].last_ds"].to_f end
read(resolution, start_ts, end_ts, function = :average)
click to toggle source
# File lib/rrd_repo.rb, line 43 def read(resolution, start_ts, end_ts, function = :average) start_time = (start_ts.to_i / resolution) * resolution end_time = (end_ts.to_i / resolution) * resolution data = _rrd.fetch( function, :resolution => resolution, :start => start_time, :end => end_time ) header, values = data.first, data[1..-1] nan_filtered = values.reject { |v| v[1..-1].any?(&:nan?) } nan_filtered.map { |v| Hash[header.zip(v)] } end
write(time, values = [])
click to toggle source
# File lib/rrd_repo.rb, line 57 def write(time, values = []) _rrd.update(time, *values) end
Private Instance Methods
unflatten_archives(info)
click to toggle source
Takes a flat hash output from an rrd “info” call and unflattens it by each archive.
Input: {
"rra[0].pdp_per_row"=>1, "rra[0].xff"=>0.5, "rra[1].pdp_per_row"=>168, "rra[1].xff"=>0.5, ...
}
Output: [
{ "pdp_per_row"=>1, "xff" => 0.5, }, { "pdp_per_row" => 168, "xff" => 0.5 }
]
# File lib/rrd_repo.rb, line 91 def unflatten_archives(info) info.select { |key, value| key.index("rra") == 0 } .group_by { |elem| elem[0].split('.')[0] } .sort_by { |rra, _| rra } .map { |rra, values| Hash[values.map { |key, val| [key.split('.')[1], val] }] } end