Class: Ganglia::GMetric
- Inherits:
-
Object
- Object
- Ganglia::GMetric
- Defined in:
- lib/gmetric.rb
Constant Summary collapse
- SLOPE =
{ 'zero' => 0, 'positive' => 1, 'negative' => 2, 'both' => 3, 'unspecified' => 4 }
Class Method Summary collapse
Class Method Details
.pack(metric) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gmetric.rb', line 36 def self.pack(metric) metric = { :hostname => '', :group => '', :spoof => 0, :units => '', :slope => 'both', :tmax => 60, :dmax => 0 }.merge(metric) # convert bools to ints metric[:spoof] = 1 if metric[:spoof].is_a? TrueClass metric[:spoof] = 0 if metric[:spoof].is_a? FalseClass raise "Missing key, value, type" if not metric.key? :name or not metric.key? :value or not metric.key? :type raise "Invalid metric type" if not %w(string int8 uint8 int16 uint16 int32 uint32 float double).include? metric[:type] = XDRPacket.new data = XDRPacket.new # METADATA payload .pack_int(128) # gmetadata_full .pack_string(metric[:hostname]) # hostname .pack_string(metric[:name].to_s) # name of the metric .pack_int(metric[:spoof].to_i) # spoof hostname flag .pack_string(metric[:type].to_s) # one of: string, int8, uint8, int16, uint16, int32, uint32, float, double .pack_string(metric[:name].to_s) # name of the metric .pack_string(metric[:units].to_s) # units for the value, e.g. 'kb/sec' .pack_int(SLOPE[metric[:slope]]) # sign of the derivative of the value over time, one of zero, positive, negative, both, default both .pack_uint(metric[:tmax].to_i) # maximum time in seconds between gmetric calls, default 60 .pack_uint(metric[:dmax].to_i) # lifetime in seconds of this metric, default=0, meaning unlimited ## MAGIC NUMBER: equals the elements of extra data, here it's 1 because I added Group. .pack_int(1) ## METADATA EXTRA DATA: functionally key/value .pack_string("GROUP") .pack_string(metric[:group].to_s) # DATA payload data.pack_int(128+5) # string message data.pack_string(metric[:hostname].to_s) # hostname data.pack_string(metric[:name].to_s) # name of the metric data.pack_int(metric[:spoof].to_i) # spoof hostname flag data.pack_string("%s") # data.pack_string(metric[:value].to_s) # value of the metric [.data, data.data] end |
.send(host, port, metric) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gmetric.rb', line 15 def self.send(host, port, metric) gmetric = self.pack(metric) if defined?(EventMachine) and EventMachine.reactor_running? # open an ephemereal UDP socket since # we do not plan on recieving any data conn = EM.open_datagram_socket('', 0) conn.send_datagram gmetric[0], host, port conn.send_datagram gmetric[1], host, port conn.close_connection_after_writing else conn = UDPSocket.new conn.connect(host, port) conn.send gmetric[0], 0 conn.send gmetric[1], 0 conn.close end end |