Skip to content

Suggestion for the bmc fact #30

@dcj

Description

@dcj

bmclib is extremely helpful, thank you!

FYI, I modified the code to combine all the separate facts into one structured fact, called bmc,

Facter.add(:bmc) do
  setcode do
    bmc_hash = {}
    bmc_hash["device_present"] = device_present?
    bmc_hash["tools_present"] = tools_present?
    if device_present? && tools_present?
      bmc_hash["ip"] = ip
      bmc_hash["mac"] = mac
      bmc_hash["netmask"] = subnet
      bmc_hash["gateway"] = gateway
      bmc_hash["vlan"] = vlanid
      bmc_hash["assignment"] = "unknown"
      if dhcp? 
        bmc_hash["assignment"] = "dhcp"
      end
      if static? 
        bmc_hash["assignment"] = "static"
      end
    end
    bmc_hash
  end
end

def lanconfig
  @lanconfig ||= parse_laninfo
end

def parse_laninfo
  if ipmitool.empty? or ipmitool.nil?
    return {}
  end
  channel_lookup = {
    'Dell Inc.'         => 1,
    'FUJITSU'           => 2,
    'FUJITSU SIEMENS'   => 2,
    'HP'                => 2,
    'Intel Corporation' => 3,
  }
  channel = channel_lookup.fetch(Facter.value(:manufacturer), 1)
  landata = Facter::Core::Execution.exec("#{ipmitool} lan print #{channel} 2>/dev/null") || ''
  laninfo = {}
  landata.lines.each do |line|
    # clean up the data from spaces
    item = line.split(':', 2)
    key = item.first.strip.downcase.gsub(' ','_')
    value = item.last.strip
    laninfo[key] = value
  end
  laninfo
end

def ipmitool
  unless @ipmitool
    timeout = Facter::Core::Execution.which('timeout')
    ipmitool_cmd = Facter::Core::Execution.which('ipmitool')
    if ipmitool_cmd and timeout
      @ipmitool = "#{timeout} 2 #{ipmitool_cmd}"
    elsif ipmitool_cmd
      @ipmitool = ipmitool_cmd
    else
      @ipmitool = nil
    end
  end
  @ipmitool
end

def ip
  lanconfig["ip_address"]
end

def mac
  lanconfig["mac_address"]
end

def subnet
  lanconfig["subnet_mask"]
end

def gateway
  lanconfig["default_gateway_ip"]
end

def vlanid
  lanconfig["802.1q_vlan_id"].downcase!
end

def dhcp?
  lanconfig["ip_address_source"].match(/dhcp/i) != nil
end

def static?
  lanconfig["ip_address_source"].match(/static/i) != nil
end

def ipsrc
  lanconfig["ip_address_source"].downcase!
end

def device_present?
  File.exists?('/dev/ipmi0') || File.exists?('/dev/ipmi/0') || File.exists?('/dev/ipmidev/0')
end

def tools_present?
  !Facter::Core::Execution.which('ipmitool').nil?
end

I do not know much about Ruby, so I won't be surprised if there is a better way to do this....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions