Skip to content

Commit

Permalink
Added resize method to Base. Fixed lastupdate to work with 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Morello committed Mar 3, 2010
1 parent 7b8993e commit b3396ab
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rdoc
Expand Up @@ -11,7 +11,7 @@

* Added graph! to RRD
* Added bang methods to base
* Added dump, last_update methods to RRD::Base instance (TODO: tune, resize)
* Added dump, last_update, resize methods to RRD::Base instance (TODO: tune)
* Added dump, tune, resize, last_update methods to RRD::Wrapper
* Escape ':' on graph label
* Added flags to be used on RRD::Base instances and for building graphs
Expand Down
10 changes: 8 additions & 2 deletions README.rdoc
Expand Up @@ -6,7 +6,7 @@ You may use it in the raw format, as many rrd libs in languages like perl or pyt

rrd-ffi uses ffi to wrap the librrd C bindings, not system calls.

IMPORTANT: You need librrd installed in your system for this gem to work
IMPORTANT: You need librrd installed (version 1.3.1 or above) in your system for this gem to work

= Basics

Expand All @@ -28,7 +28,7 @@ or

export LD_LIBRARY_PATH=/opt/local/lib

If you are not using MAC OS and still have problems, export the RRD_PATH variable with the path to your librrd file.
If you are not using MAC OS and still having problems, export the RRD_PATH variable with the path to your librrd file.

=== Installing librrd

Expand All @@ -38,6 +38,12 @@ Fedora/Red Hat: <tt>yum install rrdtool-devel</tt>

Mac: <tt>port install rrdtool</tt>

From source:
cd rrdtool-${rrdtool_version}
./configure --disable-ruby --prefix=/usr/local
make
make install

== Example Usage
require "rrd"
rrd = RRD::Base.new("myrrd.rrd")
Expand Down
10 changes: 10 additions & 0 deletions lib/rrd/base.rb
Expand Up @@ -56,6 +56,16 @@ def last_update
Wrapper.last_update(rrd_file)
end

# Writes a new file 'resize.rrd'
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

# See RRD::Wrapper.restore
def restore(xml_file, options = {})
options = options.clone
Expand Down
14 changes: 9 additions & 5 deletions lib/rrd/wrapper.rb
Expand Up @@ -41,6 +41,8 @@ class << self
extend FFI::Library

ffi_lib RRD::Wrapper.detect_rrd_lib
attach_function :rrd_strversion, [], :string

attach_function :rrd_create, [:int, :pointer], :int
attach_function :rrd_dump, [:int, :pointer], :int
attach_function :rrd_fetch, [:int, :pointer, :pointer, :pointer, :pointer, :pointer, :pointer, :pointer], :int
Expand All @@ -49,10 +51,11 @@ class << self
attach_function :rrd_info, [:int, :pointer], :pointer
attach_function :rrd_last, [:int, :pointer], :time_t

begin
if rrd_strversion >= "1.4"
attach_function :rrd_lastupdate_r, [:string, :pointer, :pointer, :pointer, :pointer], :int
rescue Exception => e
puts "Please upgrade your rrdtool version to use last_update method"
alias :rrd_lastupdate :rrd_lastupdate_r
else
attach_function :rrd_lastupdate, [:string, :pointer, :pointer, :pointer, :pointer], :int
end

attach_function :rrd_resize, [:int, :pointer], :int
Expand Down Expand Up @@ -173,13 +176,12 @@ def last(*args)
# [1266933900, "0.9", "253" ]]
#
def last_update(file)
raise "Please upgrade your rrdtool version before using last_updae method" unless respond_to?(:rrd_lastupdate_r)
update_time_ptr = empty_pointer
ds_count_ptr = empty_pointer
ds_names_ptr = empty_pointer
values_ptr = FFI::MemoryPointer.new(:pointer)

return false if rrd_lastupdate_r(file, update_time_ptr, ds_count_ptr, ds_names_ptr, values_ptr) == -1
return false if rrd_lastupdate(file, update_time_ptr, ds_count_ptr, ds_names_ptr, values_ptr) == -1
update_time = update_time_ptr.get_ulong(0)
ds_count = ds_count_ptr.get_ulong(0)
ds_names = ds_names_ptr.get_pointer(0).get_array_of_string(0, ds_count)
Expand All @@ -190,6 +192,8 @@ def last_update(file)
end

# Used to modify the number of rows in an RRA
#
# Creates a new file in the same directory, called 'resize.rrd'
def resize(*args)
argv = to_pointer(["resize"] + args)
rrd_resize(args.size+1, argv) == 0
Expand Down
6 changes: 6 additions & 0 deletions spec/rrd/base_spec.rb
Expand Up @@ -56,6 +56,12 @@
@rrd.restore(XML_FILE, :force_overwrite => true).should be_true
end

it "should resize a RRA from rrd file" do
RRD::Wrapper.should_receive(:info).and_return({"step" => 5, "rra[1].pdp_per_row" => 12}) # step of 1 minute on RRA
RRD::Wrapper.should_receive(:resize).with(RRD_FILE, "1", "GROW", "60").and_return(true)
@rrd.resize(1, :grow => 1.hour)
end

it "should return the last update made" do
RRD::Wrapper.should_receive(:last_update).with(RRD_FILE).and_return([])
@rrd.last_update
Expand Down
1 change: 0 additions & 1 deletion spec/rrd/wrapper_spec.rb
Expand Up @@ -71,7 +71,6 @@
end

it "should return the last entered values" do
pending unless RRD::Wrapper.respond_to?(:rrd_lastupdate_r)
result = RRD::Wrapper.last_update(RRD_FILE)
result.should have(2).lines
result[1][0].should == 1266945375
Expand Down

0 comments on commit b3396ab

Please sign in to comment.