-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmidecode.py
132 lines (110 loc) · 2.99 KB
/
dmidecode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
__version__ = "0.8.1"
TYPE = {
0: 'bios',
1: 'system',
2: 'base board',
3: 'chassis',
4: 'processor',
7: 'cache',
8: 'port connector',
9: 'system slot',
10: 'on board device',
11: 'OEM strings',
#13: 'bios language',
15: 'system event log',
16: 'physical memory array',
17: 'memory_device',
19: 'memory array mapped address',
24: 'hardware security',
25: 'system power controls',
27: 'cooling device',
32: 'system boot',
41: 'onboard device',
}
def parse_dmi(content):
"""
Parse the whole dmidecode output.
Returns a list of tuples of (type int, value dict).
"""
info = []
lines = iter(content.strip().splitlines())
while True:
try:
line = lines.next()
except StopIteration:
break
if line.startswith('Handle 0x'):
typ = int(line.split(',', 2)[1].strip()[len('DMI type'):])
if typ in TYPE:
info.append((typ, _parse_handle_section(lines)))
return info
def _parse_handle_section(lines):
"""
Parse a section of dmidecode output
* 1st line contains address, type and size
* 2nd line is title
* line started with one tab is one option and its value
* line started with two tabs is a member of list
"""
data = {
'_title': lines.next().rstrip(),
}
for line in lines:
line = line.rstrip()
if line.startswith('\t\t'):
data[k].append(line.lstrip())
elif line.startswith('\t'):
k, v = [i.strip() for i in line.lstrip().split(':', 1)]
if v:
data[k] = v
else:
data[k] = []
else:
break
return data
def profile():
import os, sys
if os.isatty(sys.stdin.fileno()):
content = _get_output()
else:
content = sys.stdin.read()
info = parse_dmi(content)
_show(info)
def _get_output():
import subprocess
output = subprocess.check_output(
'PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin '
'sudo dmidecode', shell=True)
return output
def _show(info):
def _get(i):
return [v for j, v in info if j == i]
system = _get(1)[0]
print '%s %s (SN: %s, UUID: %s)' % (
system['Manufacturer'],
system['Product Name'],
system['Serial Number'],
system['UUID'],
)
for cpu in _get(4):
print '%s %s %s (Core: %s, Thead: %s)' % (
cpu['Manufacturer'],
cpu['Family'],
cpu['Max Speed'],
cpu['Core Count'],
cpu['Thread Count'],
)
cnt, total, unit = 0, 0, None
for mem in _get(17):
if mem['Size'] == 'No Module Installed':
continue
i, unit = mem['Size'].split()
cnt += 1
total += int(i)
print '%d memory stick(s), %d %s in total' % (
cnt,
total,
unit,
)
if __name__ == '__main__':
profile()