Class: FFWD::Plugin::Collectd::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/ffwd/plugin/collectd/connection.rb

Direct Known Subclasses

InputTCP, InputUDP

Instance Method Summary collapse

Constructor Details

#initialize(bind, core, config) ⇒ Connection

Returns a new instance of Connection.



23
24
25
26
27
28
# File 'lib/ffwd/plugin/collectd/connection.rb', line 23

def initialize bind, core, config
  @bind = bind
  @core = core
  @db = TypesDB.open config[:types_db]
  @key = config[:key]
end

Instance Method Details

#format_type_instance(type, i) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/ffwd/plugin/collectd/connection.rb', line 85

def format_type_instance type, i
  if @db
    return @db.get_name(type, i)
  end

  i.to_s
end

#format_what(a) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ffwd/plugin/collectd/connection.rb', line 93

def format_what a
  p = if a[:plugin_instance]
        "#{a[:plugin]}-#{a[:plugin_instance]}"
      else
        a[:plugin].to_s
      end

  t = if a[:type_instance]
        "#{a[:type]}-#{a[:type_instance]}"
      else
        a[:type].to_s
      end

  "#{p}/#{t}"
end

#read_multiple(plugin, plugin_i, type, type_i, values) ⇒ Object

Handle payload with multiple values.

If a database is not available, plugin_instance becomes the running integer, or index of the value.

If a database is avaialble, it would follow the current structure and determine what the name of the plugin_instance is.

collectd.org/documentation/manpages/types.db.5.shtml



74
75
76
77
78
79
80
81
82
83
# File 'lib/ffwd/plugin/collectd/connection.rb', line 74

def read_multiple(plugin, plugin_i, type, type_i, values)
  values.each_with_index do |v, i|
    a = {:plugin => plugin, :type => type}
    a[:plugin_instance] = plugin_i unless plugin_i.nil? or plugin_i.empty?
    a[:type_instance] = format_type_instance(type, i)
    a[:value_type] = v[0]
    a[:what] = format_what(a)
    yield a, v[1]
  end
end

#read_single(plugin, plugin_i, type, type_i, v) {|a, | ... } ⇒ Object

Yields:

  • (a, )


56
57
58
59
60
61
62
63
# File 'lib/ffwd/plugin/collectd/connection.rb', line 56

def read_single(plugin, plugin_i, type, type_i, v)
  a = {:plugin => plugin, :type => type}
  a[:plugin_instance] = plugin_i unless plugin_i.nil? or plugin_i.empty?
  a[:type_instance] = type_i unless type_i.nil? or type_i.empty?
  a[:value_type] = v[0]
  a[:what] = format_what(a)
  yield a, v[1]
end

#read_values(plugin, plugin_i, type, type_i, values, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ffwd/plugin/collectd/connection.rb', line 48

def read_values(plugin, plugin_i, type, type_i, values, &block)
  if values.size == 1
    return read_single(plugin, plugin_i, type, type_i, values[0], &block)
  end

  read_multiple(plugin, plugin_i, type, type_i, values, &block)
end

#receive_data(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ffwd/plugin/collectd/connection.rb', line 30

def receive_data(data)
  Parser.parse(data) do |m|
    plugin = m[:plugin]
    type = m[:type]
    plugin_i = m[:plugin_instance]
    type_i = m[:type_instance]

    read_values(plugin, plugin_i, type, type_i, m[:values]) do |a, v|
      @core.input.metric(
        :key => @key, :time => m[:time], :value => v,
        :host => m[:host], :attributes => a)
      @bind.increment :received_metrics
    end
  end
rescue => e
  @bind.log.error "Failed to receive data", e
end