cleanup to dmi fact discovery

When invoking dmidecode, first use module.get_bin_path() and secondly
use module.run_command.
Remove sub function execute() from get_dmi_facts().
Simplify get_dmi_facts() by only using two mechanisms to determine dmi
facts:  first try /sys/devices/virtual/dmi and if not available, use
dmidecode executable.
This commit is contained in:
Stephen Fromm 2013-05-29 20:59:15 -07:00 committed by Michael DeHaan
parent 3f95f15bb4
commit 44cdd2dc0f

View file

@ -29,11 +29,6 @@ import socket
import struct import struct
import datetime import datetime
import getpass import getpass
if not os.path.exists('/sys/devices/virtual/dmi/id/product_name'):
try:
import dmidecode
except ImportError:
import subprocess import subprocess
DOCUMENTATION = ''' DOCUMENTATION = '''
@ -463,13 +458,10 @@ class LinuxHardware(Hardware):
self.facts['processor_cores'] = 'NA' self.facts['processor_cores'] = 'NA'
def get_dmi_facts(self): def get_dmi_facts(self):
''' learn dmi facts from system
def execute(cmd): Try /sys first for dmi related facts.
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) If that is not available, fall back to dmidecode executable '''
(out, err) = p.communicate()
if p.returncode or err:
return None
return out.rstrip()
if os.path.exists('/sys/devices/virtual/dmi/id/product_name'): if os.path.exists('/sys/devices/virtual/dmi/id/product_name'):
# Use kernel DMI info, if available # Use kernel DMI info, if available
@ -484,16 +476,16 @@ class LinuxHardware(Hardware):
"Rack Mount Chassis", "Sealed-case PC", "Multi-system", "Rack Mount Chassis", "Sealed-case PC", "Multi-system",
"CompactPCI", "AdvancedTCA", "Blade" ] "CompactPCI", "AdvancedTCA", "Blade" ]
DMI_DICT = dict( DMI_DICT = {
bios_date = '/sys/devices/virtual/dmi/id/bios_date', 'bios_date': '/sys/devices/virtual/dmi/id/bios_date',
bios_version = '/sys/devices/virtual/dmi/id/bios_version', 'bios_version': '/sys/devices/virtual/dmi/id/bios_version',
form_factor = '/sys/devices/virtual/dmi/id/chassis_type', 'form_factor': '/sys/devices/virtual/dmi/id/chassis_type',
product_name = '/sys/devices/virtual/dmi/id/product_name', 'product_name': '/sys/devices/virtual/dmi/id/product_name',
product_serial = '/sys/devices/virtual/dmi/id/product_serial', 'product_serial': '/sys/devices/virtual/dmi/id/product_serial',
product_uuid = '/sys/devices/virtual/dmi/id/product_uuid', 'product_uuid': '/sys/devices/virtual/dmi/id/product_uuid',
product_version = '/sys/devices/virtual/dmi/id/product_version', 'product_version': '/sys/devices/virtual/dmi/id/product_version',
system_vendor = '/sys/devices/virtual/dmi/id/sys_vendor', 'system_vendor': '/sys/devices/virtual/dmi/id/sys_vendor'
) }
for (key,path) in DMI_DICT.items(): for (key,path) in DMI_DICT.items():
data = get_file_content(path) data = get_file_content(path)
@ -508,46 +500,28 @@ class LinuxHardware(Hardware):
else: else:
self.facts[key] = 'NA' self.facts[key] = 'NA'
elif 'dmidecode' in sys.modules.keys():
# Use python dmidecode, if available
DMI_DICT = dict(
bios_date = '/dmidecode/BIOSinfo/ReleaseDate',
bios_version = '/dmidecode/BIOSinfo/BIOSrevision',
form_factor = '/dmidecode/ChassisInfo/ChassisType',
product_name = '/dmidecode/SystemInfo/ProductName',
product_serial = '/dmidecode/SystemInfo/SerialNumber',
product_uuid = '/dmidecode/SystemInfo/SystemUUID',
product_version = '/dmidecode/SystemInfo/Version',
system_vendor = '/dmidecode/SystemInfo/Manufacturer',
)
dmixml = dmidecode.dmidecodeXML()
dmixml.SetResultType(dmidecode.DMIXML_DOC)
xmldoc = dmixml.QuerySection('all')
dmixp = xmldoc.xpathNewContext()
for (key,path) in DMI_DICT.items():
try:
data = dmixp.xpathEval(path)
if len(data) > 0:
self.facts[key] = data[0].get_content()
else:
self.facts[key] = 'Error'
except:
self.facts[key] = 'NA'
else: else:
# Fall back to using dmidecode, if available # Fall back to using dmidecode, if available
dmi_bin = module.get_bin_path('dmidecode')
self.facts['bios_date'] = execute('dmidecode -s bios-release-date') or 'NA' DMI_DICT = {
self.facts['bios_version'] = execute('dmidecode -s bios-version') or 'NA' 'bios_date': 'bios-release-date',
self.facts['form_factor'] = execute('dmidecode -s chassis-type') or 'NA' 'bios_version': 'bios-version',
self.facts['product_name'] = execute('dmidecode -s system-product-name') or 'NA' 'form_factor': 'chassis-type',
self.facts['product_serial'] = execute('dmidecode -s system-serial-number') or 'NA' 'product_name': 'system-product-name',
self.facts['product_uuid'] = execute('dmidecode -s system-uuid') or 'NA' 'product_serial': 'system-serial-number',
self.facts['product_version'] = execute('dmidecode -s system-version') or 'NA' 'product_uuid': 'system-uuid',
self.facts['system_vendor'] = execute('dmidecode -s system-manufacturer') or 'NA' 'product_version': 'system-version',
'system_vendor': 'system-manufacturer'
}
for (k, v) in DMI_DICT.items():
if dmi_bin is not None:
(rc, out, err) = module.run_command('%s -s %s' % (dmi_bin, v))
if rc == 0:
self.facts[k] = out.rstrip()
else:
self.facts[k] = 'NA'
else:
self.facts[k] = 'NA'
def get_mount_facts(self): def get_mount_facts(self):
self.facts['mounts'] = [] self.facts['mounts'] = []