In Files

Parent

Class/Module Index [+]

Quicksearch

Errand

Errand: Wraps the RRD Ruby library provided by your distribution's package manager, exposing a more Ruby-like and accessible interface.

Public Class Methods

new(opts={}) click to toggle source
# File lib/errand.rb, line 11
def initialize(opts={})
  raise ArgumentError unless opts[:filename]
  @filename = opts[:filename]
  @backend = ::RRD
end

Public Instance Methods

create(opts={}) click to toggle source
# File lib/errand.rb, line 52
def create(opts={})
  step  = (opts[:step] || 300).to_s
  start = (opts[:start] || Time.now.to_i - 10).to_s

  options = ["--step", step, "--start", start]

  sources = opts[:sources].map { |source|
    name      = source[:name]
    type      = source[:type].to_s.upcase
    heartbeat = source[:heartbeat]
    min       = source[:min]
    max       = source[:max]

    ds = ["DS", name, type]
    ds += [heartbeat, min, max] if heartbeat && min && max

    ds.join(':')
  }

  archives = opts[:archives].map { |archive|
    function = archive[:function].to_s.upcase
    xff      = archive[:xff]
    steps    = archive[:steps]
    rows     = archive[:rows]

    rra = ["RRA", function]
    rra += [xff, steps, rows] if xff && steps && rows

    rra.join(':')
  }

  args = options + sources + archives
  @backend.create(@filename, *args) # * "flattens" the array

  true
end
data_sources() click to toggle source

ordered array of data sources as defined in rrd

# File lib/errand.rb, line 138
def data_sources
  self.info.keys.grep(/^ds\[/).map { |ds| ds[3..-1].split(']').first}.uniq
end
dump(opts={}) click to toggle source
# File lib/errand.rb, line 17
def dump(opts={})
  output = opts[:filename] || ""
  @backend.dump(@filename, output)
end
fetch(opts={}) click to toggle source
# File lib/errand.rb, line 26
def fetch(opts={})
  start  = (opts[:start] || Time.now.to_i - 3600).to_s
  finish = (opts[:finish] || Time.now.to_i).to_s
  function = opts[:function] ? opts[:function].to_s.upcase : "AVERAGE"

  args = [@filename, "--start", start, "--end", finish, function]

  data = @backend.fetch(*args)
  start  = data[0]
  finish = data[1]
  labels = data[2]
  values = data[3]
  points = {}

  # compose a saner representation of the data
  labels.each_with_index do |label, index|
    points[label] = []
    values.each do |tuple|
      value = tuple[index].nan? ? nil : tuple[index]
      points[label] << value
    end
  end

  {:start => start, :finish => finish, :data => points}
end
info() click to toggle source
# File lib/errand.rb, line 133
def info
  @backend.info(@filename)
end
last() click to toggle source
# File lib/errand.rb, line 22
def last
  @backend.last(@filename)
end
update(opts={}) click to toggle source
# File lib/errand.rb, line 89
def update(opts={})
  time_specified = opts[:sources].find { |source| source[:time] }
  times = opts[:sources].map {|s| s[:time]}.sort.uniq if time_specified

  sources = opts[:sources].map { |source|
    source[:name]
  }.join(':')

  case
  when time_specified && times.size == 1
    values = "#{times.first}:" + opts[:sources].map { |s|
      s[:value]
    }.join(':')

    args = ["--template", sources, values]
    @backend.update(@filename, *args)
  when time_specified && times.size > 1
    times.each do |t|
      points = opts[:sources].find_all { |source| source[:time] == t }

      sources = points.map { |p|
        p[:name]
      }.join(':')

      values = "#{t}:" + points.map { |p|
        p[:value]
      }.join(':')

      args = ["--template", sources, values]
      @backend.update(@filename, *args)
    end

  when !time_specified
    values = "N:" + opts[:sources].map { |source|
      source[:value]
    }.join(':')

    args = ["--template", sources, values]
    @backend.update(@filename, *args)
  end

  true
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.