Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit df51ce1a91a63a54283ccc07e9654a54048a7f69 @igrigorik committed Jan 17, 2010
Showing with 74 additions and 0 deletions.
  1. +49 −0 lib/gmetric.rb
  2. +23 −0 spec/gmetric_spec.rb
  3. +2 −0 spec/helper.rb
@@ -0,0 +1,49 @@
+module Ganglia
+ class GMetric
+
+ SLOPE = {
+ 'zero' => 0,
+ 'positive' => 1,
+ 'negative' => 2,
+ 'both' => 3,
+ 'unspecified' => 4
+ }
+
+ def self.pack(data)
+ xdr = XDRPacket.new
+
+ xdr.pack_int(0) # type gmetric
+ xdr.pack_string(data[:type]) # one of: string, int8, uint8, int16, uint16, int32, uint32, float, double
+ xdr.pack_string(data[:name]) # name of the metric
+ xdr.pack_string(data[:val].to_s) # value of the metric
+ xdr.pack_string(data[:units]) # units for the value, e.g. 'kb/sec'
+ xdr.pack_int(SLOPE[data[:slope]]) # sign of the derivative of the value over time, one of zero, positive, negative, both, default both
+ xdr.pack_uint(data[:tmax].to_i) # maximum time in seconds between gmetric calls, default 60
+ xdr.pack_uint(data[:dmax].to_i) # lifetime in seconds of this metric, default=0, meaning unlimited
+
+ xdr.get_buffer
+ end
+ end
+
+ class XDRPacket
+ def initialize
+ @data = []
+ end
+
+ def pack_int(data)
+
+ end
+
+ def pack_string(data)
+
+ end
+
+ def pack_uint(data)
+
+ end
+
+ def get_buffer
+ @data.join
+ end
+ end
+end
@@ -0,0 +1,23 @@
+require "helper"
+
+describe Ganglia::GMetric do
+
+ it "should pack GMetric into XDR format from Ruby hash" do
+ result = "0000000000000006737472696e67000000000003666f6f00000000036261720000000000000000030000003c00000000"
+ data = {
+ :slope => 'both',
+ :name => 'foo',
+ :val => 'bar',
+ :tmax => 60,
+ :units => '',
+ :dmax => 0,
+ :type => 'string'
+ }
+
+ g = Ganglia::GMetric.pack(data)
+ g.size.should == 48
+ # g.hex.should == result
+
+ end
+
+end
@@ -0,0 +1,2 @@
+require "lib/gmetric"
+require "spec"

0 comments on commit df51ce1

Please sign in to comment.