From b3396abcba90f0669bb5315a4d3e6c832cc41015 Mon Sep 17 00:00:00 2001 From: Thiago Morello Date: Wed, 3 Mar 2010 17:04:01 -0300 Subject: [PATCH] Added resize method to Base. Fixed lastupdate to work with 1.3 --- CHANGELOG.rdoc | 2 +- README.rdoc | 10 ++++++++-- lib/rrd/base.rb | 10 ++++++++++ lib/rrd/wrapper.rb | 14 +++++++++----- spec/rrd/base_spec.rb | 6 ++++++ spec/rrd/wrapper_spec.rb | 1 - 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 52ab318..5da93a2 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -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 diff --git a/README.rdoc b/README.rdoc index 6720c25..3fe4ae5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 @@ -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 @@ -38,6 +38,12 @@ Fedora/Red Hat: yum install rrdtool-devel Mac: port install rrdtool +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") diff --git a/lib/rrd/base.rb b/lib/rrd/base.rb index 4864b64..cfa4a33 100644 --- a/lib/rrd/base.rb +++ b/lib/rrd/base.rb @@ -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 diff --git a/lib/rrd/wrapper.rb b/lib/rrd/wrapper.rb index c7a00cb..f3b1c97 100644 --- a/lib/rrd/wrapper.rb +++ b/lib/rrd/wrapper.rb @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/spec/rrd/base_spec.rb b/spec/rrd/base_spec.rb index 0499022..e3ebec5 100644 --- a/spec/rrd/base_spec.rb +++ b/spec/rrd/base_spec.rb @@ -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 diff --git a/spec/rrd/wrapper_spec.rb b/spec/rrd/wrapper_spec.rb index 2134895..4d03119 100644 --- a/spec/rrd/wrapper_spec.rb +++ b/spec/rrd/wrapper_spec.rb @@ -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