Merge pull request #2934 from dangets/gpu_grain

Added basic gpu info grain for linux
This commit is contained in:
Thomas S Hatch 2012-12-18 08:58:38 -08:00
commit f63579afb4

View File

@ -15,6 +15,7 @@ as those returned here
# Import python libs
import os
import socket
import subprocess
import sys
import re
import platform
@ -124,6 +125,53 @@ def _linux_cpudata():
return grains
def _linux_gpu_data():
"""
num_gpus: int
gpus:
- vendor: nvidia|amd|ati|...
model: string
"""
# dominant gpu vendors to search for (MUST be lowercase)
known_vendors = ['nvidia', 'amd', 'ati', 'intel']
devs = []
try:
lspci = subprocess.Popen(['lspci', '-vmm'], stdout=subprocess.PIPE)
cur_dev = {}
for line in lspci.stdout:
# check for record-separating empty lines (only newline char)
if len(line) == 1:
if cur_dev.get('Class', '') == 'VGA compatible controller':
devs.append(cur_dev)
# XXX; may also need to search for "3D controller"
cur_dev = {}
continue
key, val = line.split(':', 1)
cur_dev[key.strip()] = val.strip()
except OSError:
pass
gpus = []
for gpu in devs:
vendor = gpu['Vendor'].lower()
vendor_lower = gpu['Vendor'].lower()
for name in known_vendors:
# search for an 'expected' vendor name in the string
if name in vendor_lower:
vendor = name
break
else:
# unknown vendor - just use the original string
vendor = gpu['Vendor']
gpus.append({'vendor': vendor, 'model': gpu['Device']})
grains = {}
grains['num_gpus'] = len(gpus)
grains['gpus'] = gpus
return grains
def _bsd_cpudata(osdata):
'''
Return cpu information for BSD-like systems
@ -487,7 +535,11 @@ def os_data():
'''
Return grains pertaining to the operating system
'''
grains = {}
grains = {
'num_gpus': 0,
'gpus': [],
}
try:
(grains['defaultlanguage'],
grains['defaultencoding']) = locale.getdefaultlocale()
@ -575,6 +627,7 @@ def os_data():
# traditional short names that Salt has used.
grains['os'] = _OS_NAME_MAP.get(shortname, distroname)
grains.update(_linux_cpudata())
grains.update(_linux_gpu_data())
elif grains['kernel'] == 'SunOS':
grains['os'] = 'Solaris'
if os.path.isfile('/etc/release'):
@ -792,3 +845,5 @@ def get_server_id():
# Provides:
# server_id
return {'server_id': abs(hash(__opts__.get('id', '')) % (2 ** 31))}