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

Returns a new instance of SpecinfraInventoryInput.



33
34
35
36
37
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 33

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

Instance Attribute Details

#inventoryObject

Returns the value of attribute inventory.



31
32
33
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 31

def inventory
  @inventory
end

Instance Method Details

#_cast_byte(v) ⇒ Object



135
136
137
138
# 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) ⇒ Object



130
131
132
133
# 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) ⇒ Object



140
141
142
143
# 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) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



86
87
88
89
90
91
92
# 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) ⇒ Object



94
95
96
97
98
99
# 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) ⇒ Object



105
106
107
108
109
110
111
# 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

#runObject



74
75
76
77
78
79
80
81
82
83
84
# 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

#shutdownObject



68
69
70
71
72
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 68

def shutdown
  @finished = true
  @thread.join
  super
end

#startObject



62
63
64
65
66
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 62

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

#tag(key) ⇒ Object



101
102
103
# File 'lib/fluent/plugin/in_specinfra_inventory.rb', line 101

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