Class: Fluent::Plugin::FactorInput

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

Instance Method Summary collapse

Instance Method Details

#before_shutdownObject



66
67
68
69
# File 'lib/fluent/plugin/in_factor.rb', line 66

def before_shutdown
  super
  gather_factor if @run_at_shutdown
end

#configure(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/in_factor.rb', line 25

def configure(conf)
  super

  if !@run_interval && @run_at_start
    raise ConfigError, "'run_at_start' option needs 'run_interval' option"
  end

  factor_keys = [
    "hostname",
    "operatingsystem",
    "kernel",
    "processors",
    "memory",
    "volumes",
    "interfaces",
    "is_virtual",
  ]

  @factors = @factors.to_h

  if @factors.empty?
    factor_keys.each do |key|
      @factors[key] = true
    end
  end

  if @factors.select { |k,v| v == true }.empty?
    raise ConfigError, "one or more factor are required"
  end
end

#gather_factorObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fluent/plugin/in_factor.rb', line 89

def gather_factor
  begin
    record = {}
    @factors.each do |factor, enabled|
      record[factor] = send(factor) if enabled
    end
    router.emit(@tag, Fluent::EventTime.now, record)
  rescue => e
    router.emit_error_event(@tag, Fluent::EventTime.now, record, e)
  end
end

#hostnameObject



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

def hostname
  Facter.value(:hostname)
end

#interfacesObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fluent/plugin/in_factor.rb', line 143

def interfaces
  interfaces = []
  interface_names = Facter.value(:interfaces).split(/,/)
  if interface_names
    interface_names.each do |if_name|
      next if if_name == "lo"
      interfaces.push({
        "name" => if_name,
        "ipaddress" => Facter.value("ipaddress_#{if_name}"),
        "netmask" => Facter.value("netmask_#{if_name}"),
        "macaddress" => Facter.value("macaddress_#{if_name}"),
      })
    end
  end
  interfaces
end

#is_virtualObject



160
161
162
# File 'lib/fluent/plugin/in_factor.rb', line 160

def is_virtual
  Facter.value(:is_virtual)
end

#kernelObject



113
114
115
116
117
118
# File 'lib/fluent/plugin/in_factor.rb', line 113

def kernel
  {
    "name" => Facter.value(:kernel),
    "release" => Facter.value(:kernelrelease),
  }
end

#memoryObject



124
125
126
127
128
# File 'lib/fluent/plugin/in_factor.rb', line 124

def memory
  {
    "size" => Facter.value(:memorysize_mb )
  }
end

#operatingsystemObject



105
106
107
108
109
110
111
# File 'lib/fluent/plugin/in_factor.rb', line 105

def operatingsystem
  {
    "name" => Facter.value(:operatingsystem),
    "version" => Facter.value(:operatingsystemrelease),
    "family" => Facter.value(:osfamily),
  }
end

#processorsObject



120
121
122
# File 'lib/fluent/plugin/in_factor.rb', line 120

def processors
  Facter.value(:processors)
end

#runObject



77
78
79
# File 'lib/fluent/plugin/in_factor.rb', line 77

def run
  gather_factor
end

#run_periodicObject



81
82
83
84
85
86
87
# File 'lib/fluent/plugin/in_factor.rb', line 81

def run_periodic
  gather_factor if @run_at_start
  while true
    sleep @run_interval
    gather_factor
  end
end

#shutdownObject



71
72
73
74
75
# File 'lib/fluent/plugin/in_factor.rb', line 71

def shutdown
  super
  @thread.terminate
  @thread.join
end

#startObject



56
57
58
59
60
61
62
63
64
# File 'lib/fluent/plugin/in_factor.rb', line 56

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

#volumesObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fluent/plugin/in_factor.rb', line 130

def volumes
  volumes = []
  blockdevice_names = Facter.value(:blockdevices).split(/,/)
  blockdevice_names.each do |device_name|
    next if device_name !~ /(hd.*|sd.*|md.*)/
    volumes.push({
      "name" => device_name,
      "size" => Facter.value("blockdevice_#{device_name}_size") / 1024 / 1024 / 1024,
    })
  end
  volumes
end