Class: RRDRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/rrd_repo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ RRDRepo

Returns a new instance of RRDRepo.



20
21
22
# File 'lib/rrd_repo.rb', line 20

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/rrd_repo.rb', line 4

def filename
  @filename
end

Class Method Details

.create(filename, datasources, start = Time.now) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#_rrdObject



61
62
63
64
# 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

#infoObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



24
25
26
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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 = []) ⇒ Object



57
58
59
# File 'lib/rrd_repo.rb', line 57

def write(time, values = [])
  _rrd.update(time, *values)
end