summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2007-07-17 20:03:35 +0000
committerluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2007-07-17 20:03:35 +0000
commitf35ee22e8dbfa267d3e44e90c42cde0cd197e1d5 (patch)
tree008a39c6e03bcbaaabdde02a6d03495b5b0496ba
parent20986d92f34679c563e61452cada690375355920 (diff)
downloadfacter-f35ee22e8dbfa267d3e44e90c42cde0cd197e1d5.tar.gz
facter-f35ee22e8dbfa267d3e44e90c42cde0cd197e1d5.tar.xz
facter-f35ee22e8dbfa267d3e44e90c42cde0cd197e1d5.zip
Drastically speeding up the lsb data retrieval, and refactoring the dmidecode data so it is a bit cleaner and does not produce extraneous output or errors
git-svn-id: http://reductivelabs.com/svn/facter/trunk@213 1f5c1d6a-bddf-0310-8f58-fc49e503516a
-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