class Fluent::Plugin::CalyptiaMonitoringMachineId

Constants

DBUS_MACHINE_ID_PATH
ETC_MACHINE_ID_PATH

Public Class Methods

new(worker_id, log) click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 20
def initialize(worker_id, log)
  @worker_id = worker_id.to_i
  @log = log
end

Public Instance Methods

id() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 40
def id
  if linux?
    linux_id
  elsif windows?
    windows_id
  elsif macos?
    macos_id
  end
end
linux?() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 29
def linux?
  RUBY_PLATFORM =~ /linux/
end
macos?() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 25
def macos?
  RUBY_PLATFORM =~ /darwin/
end
windows?() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 33
def windows?
  RUBY_PLATFORM =~ /mingw|mswin/
end

Private Instance Methods

linux_id() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 63
def linux_id
  machine_id = ""
  begin
    machine_id = File.read(DBUS_MACHINE_ID_PATH).strip
  rescue Errno::ENOENT
    machine_id = File.read(ETC_MACHINE_ID_PATH).strip rescue ""
  end
  if machine_id.empty?
    @log.info "MachineID is not retrived from #{DBUS_MACHINE_ID_PATH} or #{ETC_MACHINE_ID_PATH}. Using UUID instead."
    "#{SecureRandom.uuid}:#{@worker_id}"
  else
    "#{machine_id}:#{@worker_id}"
  end
end
macos_id() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 52
def macos_id
  require 'open3'
  o,_e, s = Open3.capture3 %q(ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}')
  unless s.success?
    @log.info "MachineID is not retrived from ioreg. Using UUID instead."
    "#{SecureRandom.uuid}:#{@worker_id}"
  else
    "#{o.strip}:#{@worker_id}"
  end
end
windows_id() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_machine_id.rb, line 78
def windows_id
  require 'win32/registry'

  machine_id = nil
  Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\\Microsoft\\Cryptography') do |key|
    machine_id = key.read("MachineGuid")[1] rescue ""
  end
  if machine_id.empty?
    @log.info "MachineID is not retrived from Registry. Using UUID instead."
    "#{SecureRandom.uuid}:#{@worker_id}"
  else
    "#{machine_id}:#{@worker_id}"
  end
end