Class: Fluent::SpecinfraInventoryInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_specinfra_inventory.rb

Constant Summary collapse

KEY_DELIMITER =
"."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpecinfraInventoryInput



26
27
28
29
30
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 26

def initialize
  super
  require 'specinfra'
  require 'specinfra/host_inventory'
end

Instance Attribute Details

#inventoryObject

Returns the value of attribute inventory.



24
25
26
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 24

def inventory
  @inventory
end

Instance Method Details

#_cast_byte(v) ⇒ Object



126
127
128
129
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 126

def _cast_byte(v)
  m = v.match(/^(\d+)(kb|KB|kB)$/)
  m.nil? ? v : m[0].to_i * 1024
end

#_cast_num(v) ⇒ Object



121
122
123
124
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 121

def _cast_num(v)
  m = v.match(/^([1-9]\d*|0)$/)
  m.nil? ? v : m[0].to_i
end

#_cast_percent(v) ⇒ Object



131
132
133
134
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 131

def _cast_percent(v)
  m = v.match(/^(\d+)%$/)
  m.nil? ? v : m[0].to_i
end

#cast(inv) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 104

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) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 32

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 = @inventory.keys if @inventory_keys.empty?
end

#emit_combine_record(time) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 77

def emit_combine_record(time)
  records = {}
  @inventory_keys.each do |key|
    records.merge!(record(key))
  end
  Engine.emit(@tag_prefix, time, records) if records.length > 0
end

#emit_separate_record(time) ⇒ Object



85
86
87
88
89
90
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 85

def emit_separate_record(time)
  time = Engine.now
  @inventory_keys.each do |key|
    Engine.emit(tag(key), time, record(key))
  end
end

#record(key) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 96

def record(key)
  inv = @inventory
  key.split(KEY_DELIMITER).each do |k|
    inv = inv[k]
  end
  {key => cast(inv)}
end

#runObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 65

def run
  loop do
    time = Engine.now
    if @combine
      emit_combine_record(time)
    else
      emit_separate_record(time)
    end
    sleep @time_span
  end
end

#shutdownObject



60
61
62
63
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 60

def shutdown
  @finished = true
  @thread.join
end

#startObject



55
56
57
58
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 55

def start
  @finished = false
  @thread = Thread.new(&method(:run))
end

#tag(key) ⇒ Object



92
93
94
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 92

def tag(key)
  "#{@tag_prefix}.#{key}"
end