Class: RRD::Base

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

Overview

TODO: add bang methods

Constant Summary collapse

BANG_METHODS =
[:create!, :dump!, :ends_at!, :fetch!, :first!, :info!, :last!, :last_update!, :resize!, :restore!, :starts_at!, :update!]
RESTORE_FLAGS =
[:force_overwrite, :range_check]
DUMP_FLAGS =
[:no_header]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rrd_file) ⇒ Base

Returns a new instance of Base.



12
13
14
# File 'lib/rrd/base.rb', line 12

def initialize(rrd_file)
  @rrd_file = rrd_file
end

Instance Attribute Details

#rrd_fileObject

Returns the value of attribute rrd_file.



5
6
7
# File 'lib/rrd/base.rb', line 5

def rrd_file
  @rrd_file
end

Instance Method Details

#bang(method, *args, &block) ⇒ Object



107
108
109
110
111
# File 'lib/rrd/base.rb', line 107

def bang(method, *args, &block)
  result = send(method, *args, &block)
  raise error unless result
  result
end

#create(options = {}, &block) ⇒ Object

Creates a rrd file. Basic usage:

rrd.create :start => Time.now - 10.seconds, :step => 5.minutes do
  datasource "memory", :type => :gauge, :heartbeat => 10.minutes, :min => 0, :max => :unlimited
  archive :average, :every => 10.minutes, :during => 1.year
end


25
26
27
28
29
# File 'lib/rrd/base.rb', line 25

def create(options = {}, &block)
  builder = RRD::Builder.new(rrd_file, options)
  builder.instance_eval(&block)
  builder.save
end

#dump(xml_file, options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/rrd/base.rb', line 31

def dump(xml_file, options = {})
  options = options.clone
  line_params = RRD.to_line_parameters(options, DUMP_FLAGS)
  Wrapper.dump(rrd_file, xml_file, *line_params)
end

#ends_atObject Also known as: last

Returns a time object with the last entered value date



38
39
40
# File 'lib/rrd/base.rb', line 38

def ends_at
  Time.at Wrapper.last(rrd_file)
end

#errorObject



16
17
18
# File 'lib/rrd/base.rb', line 16

def error
  Wrapper.error
end

#fetch(consolidation_function, options = {}) ⇒ Object

Basic usage: rrd.fetch :average



45
46
47
48
49
50
51
52
53
# File 'lib/rrd/base.rb', line 45

def fetch(consolidation_function, options = {})
  options = {:start => Time.now - 1.day, :end => Time.now}.merge options
  
  options[:start] = options[:start].to_i
  options[:end] = options[:end].to_i
  line_params = RRD.to_line_parameters(options)
  
  Wrapper.fetch(rrd_file, consolidation_function.to_s.upcase, *line_params)
end

#infoObject

See RRD::Wrapper.info



56
57
58
# File 'lib/rrd/base.rb', line 56

def info
  Wrapper.info(rrd_file)
end

#last_updateObject

See RRD::Wrapper.last_update



61
62
63
# File 'lib/rrd/base.rb', line 61

def last_update
  Wrapper.last_update(rrd_file)
end

#methodsObject



103
104
105
# File 'lib/rrd/base.rb', line 103

def methods
  super + BANG_METHODS
end

#resize(rra_num, options) ⇒ Object

Writes a new file ‘resize.rrd’

You will need to know the RRA number, starting from 0:

rrd.resize(0, :grow => 10.days)


70
71
72
73
74
75
76
77
# File 'lib/rrd/base.rb', line 70

def resize(rra_num, options)
  info = self.info
  step = info["step"]
  rra_step = info["rra[#{rra_num}].pdp_per_row"]
  action = options.keys.first.to_s.upcase
  delta = (options.values.first / (step * rra_step)).to_i # Force an integer
  Wrapper.resize(rrd_file, rra_num.to_s, action, delta.to_s)
end

#restore(xml_file, options = {}) ⇒ Object

See RRD::Wrapper.restore



80
81
82
83
84
# File 'lib/rrd/base.rb', line 80

def restore(xml_file, options = {})
  options = options.clone
  line_params = RRD.to_line_parameters(options, RESTORE_FLAGS)
  Wrapper.restore(xml_file, rrd_file, *line_params)
end

#starts_atObject Also known as: first

Returns a time object with the first entered value date



87
88
89
# File 'lib/rrd/base.rb', line 87

def starts_at
  Time.at Wrapper.first(rrd_file)
end

#update(time, *data) ⇒ Object

Basic usage: rrd.update Time.now, 20.0, 20, nil, 2

Note: All datasources must receive a value, based on datasources order in rrd file



95
96
97
98
99
100
# File 'lib/rrd/base.rb', line 95

def update(time, *data)
  new_data = data.map {|item| item.nil? ? "U" : item}
  new_data = [time.to_i] + new_data
  
  Wrapper.update(rrd_file, new_data.join(":"))
end