class Fluent::SpecinfraInventoryInput

Constants

KEY_DELIMITER

Attributes

inventory[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 33
def initialize
  super
  require 'specinfra'
  require 'specinfra/host_inventory'
end

Public Instance Methods

_cast_byte(v) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 135
def _cast_byte(v)
  m = v.match(/^(\d+)(kb|KB|kB)$/)
  m.nil? ? v : m[0].to_i * 1024
end
_cast_num(v) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 130
def _cast_num(v)
  m = v.match(/^([1-9]\d*|0)$/)
  m.nil? ? v : m[0].to_i
end
_cast_percent(v) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 140
def _cast_percent(v)
  m = v.match(/^(\d+)%$/)
  m.nil? ? v : m[0].to_i
end
cast(inv) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 113
def cast(inv)
  if @cast_num || @cast_byte || @cast_percent
    if inv.is_a?(Hash)
      inv = Hash[inv.map { |k,v| [k, cast(v)] }]
    elsif inv.is_a?(Array)
      inv = inv.map do |v|
        v = cast(v)
      end
    else
      inv = _cast_num(inv) if @cast_num && inv.is_a?(String)
      inv = _cast_byte(inv) if @cast_byte && inv.is_a?(String)
      inv = _cast_percent(inv) if @cast_percent && inv.is_a?(String)
    end
  end
  inv
end
configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 39
def configure(conf)
  super

  Specinfra.configuration.send(:backend, @backend.to_sym)
  Specinfra.configuration.send(:path, @path) if @path
  Specinfra.configuration.send(:host, @host) if @host
  Specinfra.configuration.send(:env, @env) if @env

  opt = {}
  opt[:family]  = @family  if @family
  opt[:release] = @release if @release
  opt[:arch]    = @arch    if @arch
  Specinfra.configuration.send(:os, opt) if opt.length > 0

  opt = {}
  opt[:user] = @ssh_user  if @ssh_user
  opt[:port] = @ssh_port  if @ssh_port
  Specinfra.configuration.send(:ssh_options, opt) if opt.length > 0

  @inventory = Specinfra::HostInventory.instance
  @inventory_keys = Specinfra::HostInventory::KEYS if @inventory_keys.empty?
end
emit_combine_record(time) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 86
def emit_combine_record(time)
  records = {}
  @inventory_keys.each do |key|
    records.merge!(record(key))
  end
  router.emit(@tag_prefix, time, records) if records.length > 0
end
emit_separate_record(time) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 94
def emit_separate_record(time)
  time = Engine.now
  @inventory_keys.each do |key|
    router.emit(tag(key), time, record(key))
  end
end
record(key) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 105
def record(key)
  inv = @inventory
  key.split(KEY_DELIMITER).each do |k|
    inv = inv[k]
  end
  {key => cast(inv)}
end
run() click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 74
def run
  loop do
    time = Engine.now
    if @combine
      emit_combine_record(time)
    else
      emit_separate_record(time)
    end
    sleep @time_span
  end
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 68
def shutdown
  @finished = true
  @thread.join
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 62
def start
  super
  @finished = false
  @thread = Thread.new(&method(:run))
end
tag(key) click to toggle source
# File lib/fluent/plugin/in_specinfra_inventory.rb, line 101
def tag(key)
  "#{@tag_prefix}.#{key}"
end