class FFWD::Plugin::Collectd::Connection

Public Class Methods

new(bind, core, config) click to toggle source
# 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

Public Instance Methods

format_type_instance(type, i) click to toggle source
# 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) click to toggle source
# 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) { |a, v| ... } click to toggle source

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

# 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, v| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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