Class: Collectd

Inherits:
Object
  • Object
show all
Defined in:
lib/rrd-grapher/notifier/collectdrb.rb

Overview

This file was not written by me but it did not come with a license and I cannot find itw owner so if you know the author let me know.

Constant Summary collapse

@@type_code =

Send a data point consisting of one or multiple values. Multiple values are used for RRDs with multiple data series (DS’s in RRD terms). An examples of a multi-valued RRDs in the collectd types is disk_write with a ‘read’ and a ‘write’ value. Arguments: pl=plugin, pi=plugin_instance, t=type, ti=type_instance, values: array of [type, value] Eg.: values(‘disk’, ‘sda0’, ‘disk’, ‘ops’, [[:counter, 1034], [:counter, 345]])

{:gauge => 1, :counter => 0, :derive => 2, :absolute => 3}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, interval) ⇒ Collectd

Create a collectd collection object tied to a specific destination server and port. The host parameter is the hostname sent to collectd, typically ‘hostname -f`.strip The use of the interval is unclear, it’s simply sent to collectd with every packet…



20
21
22
23
24
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 20

def initialize(host, interval)
  @interval = interval
  @host = host || %x{hostname -s}.strip
  start
end

Class Method Details

.number(type, num) ⇒ Object

Encode an integer



13
14
15
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 13

def self.number(type, num)
  [type, 12].pack("nn") + [num >> 32, num & 0xffffffff].pack("NN")
end

.string(type, str) ⇒ Object

Encode a string (type 0, null terminated string)



7
8
9
10
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 7

def self.string(type, str)
  str += "\000"
  [type, str.length+4].pack("nn") + str
end

Instance Method Details

#absolute(pl, pi, t, ti, value) ⇒ Object

Send a data point with one or multiple absolute values



83
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 83

def absolute(pl, pi, t, ti, value) values(pl, pi, t, ti, Array(value).map{|v| [:absolute, v]}); end

#chkObject

Check the length of the current packet and flush it if we reach a high-water mark



40
41
42
43
44
45
46
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 40

def chk
  if @pkt.size > 900 # arbitrary flush, 1024 is the max allowed
    flush
    #sleep(0.01) # don't overwhelm output buffers
    start
  end
end

#counter(pl, pi, t, ti, value) ⇒ Object

Send a data point with one or multiple counter values



79
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 79

def counter (pl, pi, t, ti, value) values(pl, pi, t, ti, Array(value).map{|v| [:counter, v]}); end

#derive(pl, pi, t, ti, value) ⇒ Object

Send a data point with one or multiple derive values



81
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 81

def derive  (pl, pi, t, ti, value) values(pl, pi, t, ti, Array(value).map{|v| [:derive, v]}); end

#flushObject

Send the current packet



36
37
38
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 36

def flush
  raise "need to be redefined"
end

#force_sendObject



48
49
50
51
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 48

def force_send
  flush
  start
end

#gauge(pl, pi, t, ti, value) ⇒ Object

Send a data point with one or multiple gauge values



77
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 77

def gauge   (pl, pi, t, ti, value) values(pl, pi, t, ti, Array(value).map{|v| [:gauge, v]}); end

#plugin(p) ⇒ Object



56
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 56

def plugin(p)          @pkt << Collectd.string(2, @plugin = p)          unless @plugin == p; end

#plugin_instance(p) ⇒ Object



57
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 57

def plugin_instance(p) @pkt << Collectd.string(3, @plugin_instance = p) unless @plugin_instance == p; end

#start(time = nil) ⇒ Object

Start a fresh packet, this is usually not called directly. Issues a time marker using either the passed time (unix time integer) or the same time as the previous packet (useful when overrunning from one packet to the next)



29
30
31
32
33
34
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 29

def start(time=nil)
  @pkt = Collectd.string(0, @host)
  @pkt << Collectd.number(1, Time.new.to_i)
  @pkt << Collectd.number(7, @interval)
  @plugin = @plugin_instance = @tipe = @tipe_instance = nil
end

#time(t) ⇒ Object

Issue time, plugin, plugin_instance, type, and type_instance markers. These are not typically called directly



55
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 55

def time(t)            @pkt << Collectd.number(1, @time = t)            unless @time == t; end

#tipe(p) ⇒ Object



58
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 58

def tipe(p)            @pkt << Collectd.string(4, @tipe = p)            unless @tipe == p; end

#tipe_instance(p) ⇒ Object



59
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 59

def tipe_instance(p)   @pkt << Collectd.string(5, @tipe_instance = p)   unless @tipe_instance == p; end

#to_sObject



85
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 85

def to_s; @pkt; end

#values(pl, pi, t, ti, values) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/rrd-grapher/notifier/collectdrb.rb', line 67

def values(pl, pi, t, ti, values)
  chk
  plugin(pl); plugin_instance(pi)
  tipe(t); tipe_instance(ti)
  @pkt << [6, 4+2+values.size*9, values.size].pack("nnn")
  @pkt << values.map{|t,v| [@@type_code[t]].pack("C")}.join
  @pkt << values.map{|t,v| t == :gauge ? [v].pack("E") : [v>>32, v & 0xffffffff].pack("NN")}.join
end