summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/facter.rb18
-rw-r--r--lib/facter/manufacturer.rb34
2 files changed, 39 insertions, 13 deletions
diff --git a/lib/facter.rb b/lib/facter.rb
index de8b43b..8a52a6a 100644
--- a/lib/facter.rb
+++ b/lib/facter.rb
@@ -618,16 +618,20 @@ class Facter
setcode 'uname -r'
end
- { "LSBRelease" => "^LSB Version:\t(.*)$",
- "LSBDistId" => "^Distributor ID:\t(.*)$",
- "LSBDistRelease" => "^Release:\t(.*)$",
- "LSBDistDescription" => "^Description:\t(.*)$",
- "LSBDistCodeName" => "^Codename:\t(.*)$"
+ { "LSBRelease" => %r{^LSB Version:\t(.*)$},
+ "LSBDistId" => %r{^Distributor ID:\t(.*)$},
+ "LSBDistRelease" => %r{^Release:\t(.*)$},
+ "LSBDistDescription" => %r{^Description:\t(.*)$},
+ "LSBDistCodeName" => %r{^Codename:\t(.*)$}
}.each do |fact, pattern|
Facter.add(fact) do
setcode do
- output = Resolution.exec('lsb_release -a 2>/dev/null')
- if output =~ /#{pattern}/
+ unless defined?(@@lsbdata) and defined?(@@lsbtime) and (Time.now.to_i - @@lsbtime.to_i < 5)
+ type = nil
+ @@lsbtime = Time.now
+ @@lsbdata = Resolution.exec('lsb_release -a 2>/dev/null')
+ end
+ if pattern.match(@@lsbdata)
$1
else
nil
diff --git a/lib/facter/manufacturer.rb b/lib/facter/manufacturer.rb
index 128241c..be36871 100644
--- a/lib/facter/manufacturer.rb
+++ b/lib/facter/manufacturer.rb
@@ -3,12 +3,34 @@
module Facter::Manufacturer
def self.dmi_find_system_info(name)
- dmiinfo = `/usr/sbin/dmidecode`
- info = dmiinfo.scan(/^\s*System Information(.*?)\n\S/m).join.split("\n").map { |line|
- line.split(":").map { |line2| line2.strip }
- }.reject { |array| array.empty? }
- val = info.find { |array| array[0] == name}
- return (val && val.size >= 2) ? val[1] : nil
+ return nil unless FileTest.exists?("/usr/sbin/dmidecode")
+
+ # Do not run the command more than every five seconds.
+ unless defined?(@data) and defined?(@time) and (Time.now.to_i - @time.to_i < 5)
+ @data = {}
+ type = nil
+ @time = Time.now
+ # It's *much* easier to just parse the whole darn file than
+ # to just match a chunk of it.
+ %x{/usr/sbin/dmidecode 2>/dev/null}.split("\n").each do |line|
+ case line
+ when /^(\S.+)$/
+ type = $1.chomp
+ @data[type] ||= {}
+ when /^\s+(\S.+): (\S.*)$/
+ unless type
+ next
+ end
+ @data[type][$1] = $2
+ end
+ end
+ end
+
+ if data = @data["System Information"]
+ data[name]
+ else
+ nil
+ end
end
end