lib/rubyipmi/freeipmi/commands/fru.rb in rubyipmi-0.6.0 vs lib/rubyipmi/freeipmi/commands/fru.rb in rubyipmi-0.7.0

- old
+ new

@@ -1,51 +1,108 @@ module Rubyipmi::Freeipmi class Fru < Rubyipmi::Freeipmi::BaseCommand + attr_accessor :list + + DEFAULT_FRU = 'default_fru_device' + def initialize(opts = ObservableHash.new) - super("ipmi-fru", opts) + super("ipmi-fru", opts) + @list = {} end - # return the list of fru information in a hash - def list - @list ||= parse(command) + def get_from_list(key) + if list.has_key?(DEFAULT_FRU) + if list[DEFAULT_FRU].has_key?(key) + list[DEFAULT_FRU][key] + else + nil + end + end end + + def manufacturer + get_from_list('board_manufacturer') + end + + def board_serial + get_from_list('board_serial_number') + end + def serial - list["chassis_serial_number"] + get_from_list('board_serial_number') end - def manufacturer - list["board_manufacturer"] + def model + get_from_list('board_product_name') end - def product - list["board_product_name"] + # method to retrieve the raw fru data + def getfrus + command + return @result end + def names + list.keys + end + + # return the list of fru information in a hash + def list + if @list.count < 1 + parse(getfrus) + end + @list + end + private def method_missing(method, *args, &block) - if not list.has_key?(method.to_s) - raise NoMethodError - else - list[method.to_s] - end - end + name = method.to_s + fru = list.fetch(name, nil) + # if the user wanted some data from the default fru, lets show the data for the fru. Otherwise + # we return the Fru with the given name + if fru.nil? + if list[DEFAULT_FRU].keys.include?(name) + return list[DEFAULT_FRU][name] + else + # maybe we should return nil instead? hmm... + raise NoMethodError + end + else + return fru + end + end # parse the fru information def parse(data) - datalist = {} - data.lines.each do |line| - key, value = line.split(':') - next if key =~ /\n/ - value = value.strip - key = key.gsub(/FRU/, '').strip.gsub(/\ /, '_').downcase - datalist[key] = value.strip + if ! data.nil? and ! data.empty? + parsed_data = [] + data.lines.each do |line| + if line =~ /^FRU.*/ + # this is the either the first line of of the fru or another fru + if parsed_data.count != 0 + # we have reached a new fru device so lets record the previous fru + new_fru = FruData.new(parsed_data) + parsed_data = [] + @list[new_fru[:name]] = new_fru + end + + end + parsed_data << line + end + # process the last fru + if parsed_data.count != 0 + # we have reached a new fru device so lets record the previous fru + new_fru = FruData.new(parsed_data) + parsed_data = [] + @list[new_fru[:name]] = new_fru + end end - return datalist + return @list end # run the command and return result def command value = runcmd @@ -54,6 +111,43 @@ end end end + class FruData < Hash + + def name + self[:name] + end + + def initialize(data) + parse(data) + end + + # parse the fru information that should be an array of lines + def parse(data) + if ! data.nil? + data.each do |line| + key, value = line.split(':', 2) + if key =~ /^FRU.*/ + if value =~ /([\w\s]*)\(.*\)/ + self[:name] = $~[1].strip.gsub(/\ /, '_').downcase + end + else + key = key.strip.gsub(/\ /, '_').downcase.gsub(/fru_/, '') + if ! value.nil? + self[key] = value.strip + + end + end + end + end + end + + private + + def method_missing(method, *args, &block) + self.fetch(method.to_s, nil) + end + + end end \ No newline at end of file