Class: CollectdServer::DatapointBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/collectd_server/datapoint_builder.rb

Direct Known Subclasses

CPU, LoadAvg, Memory

Defined Under Namespace

Classes: CPU, LoadAvg, Memory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts, values) ⇒ DatapointBuilder

Returns a new instance of DatapointBuilder.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/collectd_server/datapoint_builder.rb', line 32

def initialize(parts, values)
  @parts = parts.dup
  parts.each do |part|
    next if part.content == ""
    case part
    when Packet::Host           then @host = part.content
    when Packet::Time           then @timestamp = part.content
    when Packet::Interval       then @interval = part.content
    when Packet::Plugin         then @plugin = part.content
    when Packet::PluginInstance then @plugin_instance = part.content
    when Packet::Type           then @type = part.content
    when Packet::TypeInstance   then @type_instance = part.content
    end
  end

  @values = values
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def host
  @host
end

#intervalObject (readonly)

Returns the value of attribute interval.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def interval
  @interval
end

#partsObject (readonly)

Returns the value of attribute parts.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def parts
  @parts
end

#pluginObject (readonly)

Returns the value of attribute plugin.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def plugin
  @plugin
end

#plugin_instanceObject (readonly)

Returns the value of attribute plugin_instance.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def plugin_instance
  @plugin_instance
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def timestamp
  @timestamp
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def type
  @type
end

#type_instanceObject (readonly)

Returns the value of attribute type_instance.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def type_instance
  @type_instance
end

#valuesObject (readonly)

Returns the value of attribute values.



27
28
29
# File 'lib/collectd_server/datapoint_builder.rb', line 27

def values
  @values
end

Class Method Details

.from_parts(parts, values) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/collectd_server/datapoint_builder.rb', line 8

def self.from_parts(parts, values)
  begin 
    plugin_name = parts.detect { |p| p.is_a?(Packet::Plugin) }.content
  rescue NoMethodError => e
    puts "failed to find plugin part of #{parts.inspect}"
    return []
    #raise e
  end

  builder = subclasses.detect { |c| c.plugin_name == plugin_name }
  
  if builder
    builder.new(parts, values)
  else
    warn "No builder found for plugin '#{plugin_name}'. Using default"
    self.new(parts, values)
  end.generate_datapoints
end

.inherited(subclass) ⇒ Object



65
66
67
# File 'lib/collectd_server/datapoint_builder.rb', line 65

def self.inherited(subclass)
  subclasses << subclass
end

.subclassesObject



61
62
63
# File 'lib/collectd_server/datapoint_builder.rb', line 61

def self.subclasses
  @subclasses ||= []
end

Instance Method Details

#generate_datapointsObject



50
51
52
53
54
# File 'lib/collectd_server/datapoint_builder.rb', line 50

def generate_datapoints
  @values.values.map do |val|
    Datapoint.new(metric_name, timestamp, val.value)
  end
end

#metric_name(parts = nil) ⇒ Object



56
57
58
59
# File 'lib/collectd_server/datapoint_builder.rb', line 56

def metric_name(parts = nil)
  parts ||= [@host, @plugin, @plugin_instance, @type, @type_instance]
  parts.compact.to_csv.chomp
end