193 lines
5.8 KiB
Python
193 lines
5.8 KiB
Python
'''
|
||
author : Hatcher W
|
||
Email: hcwang@semi.ac.cn
|
||
use: 检测通用计算平台的硬件信息
|
||
'''
|
||
from __future__ import print_function
|
||
from collections import OrderedDict
|
||
import subprocess
|
||
def cpuinfo():
|
||
"""
|
||
Return the information in /proc/cpuinfo
|
||
as a dictionary in the following format:
|
||
cpu_info['proc0']={...}
|
||
cpu_info['proc1']={...}
|
||
"""
|
||
|
||
cpuinfo = OrderedDict()
|
||
procinfo = OrderedDict()
|
||
|
||
nprocs = 0
|
||
with open('/proc/cpuinfo') as f:
|
||
for line in f:
|
||
if not line.strip():
|
||
# end of one processor
|
||
cpuinfo['proc%s' % nprocs] = procinfo
|
||
nprocs = nprocs + 1
|
||
# Reset
|
||
procinfo = OrderedDict()
|
||
else:
|
||
if len(line.split(':')) == 2:
|
||
procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
|
||
else:
|
||
procinfo[line.split(':')[0].strip()] = ''
|
||
|
||
return cpuinfo
|
||
|
||
|
||
def meminfo():
|
||
"""
|
||
Return the information in /proc/meminfo
|
||
as a dictionary
|
||
"""
|
||
|
||
meminfo = OrderedDict()
|
||
|
||
with open('/proc/meminfo') as f:
|
||
for line in f:
|
||
meminfo[line.split(':')[0]] = line.split(':')[1].strip()
|
||
return meminfo
|
||
def gpuinfo():
|
||
"""
|
||
Return the information in /proc/gpuinfo
|
||
as a dictionary
|
||
"""
|
||
|
||
gpuinfo = OrderedDict()
|
||
|
||
with open('/proc/gpuinfo') as f:
|
||
for line in f:
|
||
meminfo[line.split(':')[0]] = line.split(':')[1].strip()
|
||
return gpuinfo
|
||
def getosversion():
|
||
with open('/etc/issue') as fd:
|
||
for line in fd:
|
||
osver = line.strip()
|
||
break
|
||
return {'osver':osver}
|
||
def getBaseboardSerialnumber ():
|
||
p = subprocess.Popen(["sudo dmidecode -t 2 | grep Serial"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
|
||
data = p.stdout
|
||
lines = []
|
||
while True:
|
||
line = str(data.readline(), encoding="utf-8")
|
||
if line == '\n':
|
||
break
|
||
if line:
|
||
d = dict([line.strip().split(': ')])
|
||
lines.append(d)
|
||
else:
|
||
break
|
||
return lines
|
||
def getCpuId():
|
||
p = subprocess.Popen(["sudo dmidecode -t 4 | grep ID"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
|
||
data = p.stdout
|
||
lines = []
|
||
while True:
|
||
line = str(data.readline(), encoding="utf-8")
|
||
if line == '\n':
|
||
break
|
||
if line:
|
||
d = dict([line.strip().split(': ')])
|
||
lines.append(d)
|
||
else:
|
||
break
|
||
return lines
|
||
def getMachineNum():
|
||
p = subprocess.Popen(["sudo dmidecode | grep 'Product Name'"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
|
||
data = p.stdout
|
||
lines = []
|
||
while True:
|
||
line = str(data.readline(), encoding="utf-8")
|
||
if line == '\n':
|
||
break
|
||
if line:
|
||
d = dict([line.strip().split(': ')])
|
||
lines.append(d)
|
||
else:
|
||
break
|
||
return lines
|
||
def getGPU():
|
||
p = subprocess.Popen(["sudo lspci -vnn | grep VGA -A 12"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
|
||
data = p.stdout
|
||
print(data)
|
||
lines = []
|
||
while True:
|
||
line = str(data.readline(), encoding="utf-8")
|
||
print(type(line))
|
||
if line == '\n' :
|
||
break
|
||
if type(line)!="dict":
|
||
continue
|
||
if line:
|
||
d = dict([line.strip().split(': ')])
|
||
lines.append(d)
|
||
else:
|
||
break
|
||
return lines
|
||
if __name__ == '__main__':
|
||
#print(meminfo())
|
||
#gpuinfo = gpuinfo()
|
||
#getgpu = getGPU()
|
||
getmachinenum = getMachineNum()
|
||
getbaseboardserialnumber =getBaseboardSerialnumber()
|
||
getcpuid = getCpuId()
|
||
osver = getosversion()
|
||
meminfo = meminfo()
|
||
cpuinfo = cpuinfo()
|
||
#exit()
|
||
print(cpuinfo["proc0"]["model name"])
|
||
if cpuinfo["proc0"]["model name"] == "Intel(R) Xeon(R) Gold 6230 CPU @ 2.10GHz":
|
||
print(cpuinfo["proc0"]["model name"])
|
||
print("model name is ok")
|
||
else:
|
||
print(cpuinfo["proc0"]["model name"])
|
||
print("CPU model is wrong")
|
||
if getcpuid[0]['ID'] == '57 06 05 00 FF FB EB BF':
|
||
print(getcpuid[0]['ID'])
|
||
print("getcpuid is ok")
|
||
else:
|
||
print(getcpuid[0]['ID'])
|
||
print("getcpuid is wrong")
|
||
if cpuinfo["proc0"]["microcode"] == "0x5002f01":
|
||
print(cpuinfo["proc0"]["microcode"])
|
||
print("microcode is ok")
|
||
else:
|
||
print(cpuinfo["proc0"]["microcode"])
|
||
print("microcode is wrong")
|
||
if cpuinfo["proc0"]["vendor_id"] == "GenuineIntel":
|
||
print(cpuinfo["proc0"]["vendor_id"])
|
||
print("vendor_id is ok")
|
||
else:
|
||
print(cpuinfo["proc0"]["vendor_id"])
|
||
print("vendor_id is wrong")
|
||
if meminfo['MemTotal'] == "394851452 kB":
|
||
print(meminfo['MemTotal'])
|
||
print("MemTotal is ok")
|
||
else:
|
||
print(meminfo['MemTotal'])
|
||
print("MemTotal is wrong")
|
||
if osver['osver'] == 'Ubuntu 16.04.6 LTS \\n \\l':
|
||
print(osver['osver'])
|
||
print("osver is ok")
|
||
else:
|
||
print(osver['osver'])
|
||
print("osver is wrong")
|
||
if getbaseboardserialnumber[0]['Serial Number'] == 'VM194S002416':
|
||
print(getbaseboardserialnumber[0]['Serial Number'])
|
||
print("getbaseboardserialnumber is ok")
|
||
else:
|
||
print(getbaseboardserialnumber[0]['Serial Number'])
|
||
print("getbaseboardserialnumber is wrong")
|
||
if getmachinenum[0]['Product Name'] == 'SYS-4029GP-TRT':
|
||
print(getmachinenum[0]['Product Name'])
|
||
print("Product Name is ok")
|
||
else:
|
||
print(getmachinenum[0]['Product Name'])
|
||
print("Product Name is wrong")
|
||
#print(cpuinfo)
|
||
#for processor in cpuinfo.keys():
|
||
#print(cpuinfo[processor]['model name'])
|
||
|
||
|