-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-platform-fingerprint.py
312 lines (237 loc) · 10.9 KB
/
get-platform-fingerprint.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import os
import time
import subprocess
import argparse
import json
import xmltodict
from lxml import etree
def get_lscpu_info():
lscpu_info = {}
try:
lscpu_output = (subprocess.check_output("lscpu", shell=True).strip()).decode()
lscpu_lines = lscpu_output.split('\n')
for line in lscpu_lines:
if line.startswith('Architecture:'):
lscpu_info["Architecture"] = line.split('Architecture:')[-1].strip()
elif line.startswith('CPU(s):'):
lscpu_info["CPUs"] = line.split('CPU(s):')[-1].strip()
elif line.startswith('Thread(s) per core:'):
lscpu_info["Threads per core"] = line.split('Thread(s) per core:')[-1].strip()
elif line.startswith('Core(s) per socket:'):
lscpu_info["Cores per socket"] = line.split('Core(s) per socket:')[-1].strip()
elif line.startswith('Socket(s):'):
lscpu_info["Sockets"] = line.split('Socket(s):')[-1].strip()
elif line.startswith('NUMA node(s):'):
lscpu_info["NUMA nodes"] = line.split('NUMA node(s):')[-1].strip()
elif line.startswith('Vendor ID:'):
lscpu_info["Vendor ID"] = line.split('Vendor ID:')[-1].strip()
elif line.startswith('Model name:'):
lscpu_info["Model name"] = line.split('Model name:')[-1].strip()
elif line.startswith('NUMA node'):
splitted_line = line.split(':')
lscpu_info[splitted_line[0]] = splitted_line[-1].strip()
elif 'cache:' in line:
splitted_line = line.split(':')
lscpu_info[splitted_line[0]] = splitted_line[-1].strip()
except subprocess.CalledProcessError as e:
print(e.output)
return lscpu_info
def get_intel_pstate_info():
files = ["status", "num_pstates", "max_perf_pct", "min_perf_pct", "turbo_pct"]
pstate_info = {}
try:
if os.path.isfile("/sys/devices/system/cpu/intel_pstate/no_turbo"):
turbo_output = (subprocess.check_output("cat /sys/devices/system/cpu/intel_pstate/no_turbo",
shell=True).strip()).decode()
pstate_info["Turbo Boost"] = "Enabled" if turbo_output == "0" else "Disabled"
for file in files:
if not os.path.isfile(f'/sys/devices/system/cpu/intel_pstate/{file}'):
continue
pstate_info[file.replace("_", " ", ).title()] = (
subprocess.check_output(f"cat /sys/devices/system/cpu/intel_pstate/{file}",
shell=True).strip()).decode()
except subprocess.CalledProcessError as e:
print(e.output)
return pstate_info
def get_cpu_frequency_and_microcode_info(cpus):
files = ["cpuinfo_max_freq", "cpuinfo_min_freq", "base_frequency", "scaling_max_freq", "scaling_min_freq",
"scaling_cur_freq", "scaling_driver", "scaling_governor"]
freq_info = {}
microcode_info = {}
try:
for i in range(0, int(cpus)):
tmp = {}
for file in files:
if not os.path.isfile(f"/sys/devices/system/cpu/cpu{i}/cpufreq/{file}"):
continue
tmp[file.replace("_", " ", ).title()] = (
subprocess.check_output(f"cat /sys/devices/system/cpu/cpu{i}/cpufreq/{file}",
shell=True).strip()).decode()
freq_info[f"Cpu {i}"] = tmp
if not os.path.isfile(f"/sys/devices/system/cpu/cpu{i}/microcode/version"):
continue
microcode_info[f"Cpu {i}"] = {}
microcode_info[f"Cpu {i}"]["Version"] = (
subprocess.check_output(f"cat /sys/devices/system/cpu/cpu{i}/microcode/version",
shell=True).strip()).decode()
except subprocess.CalledProcessError as e:
print(e.output)
return freq_info, microcode_info
def get_caches_info(path):
files = ["coherency_line_size", "level", "number_of_sets", "physical_line_partition", "size",
"type", "ways_of_associativity"]
caches_info = {}
try:
listdir = os.listdir(path)
for name in sorted(listdir):
if "index" in name and os.path.isdir(f'{path}/{name}'):
caches_info[name] = {}
for file in files:
if not os.path.isfile(f'{path}/{name}/{file}'):
continue
caches_info[name][file.replace("_", " ", ).title()] = (
subprocess.check_output(f'cat {path}/{name}/{file}', shell=True).strip()).decode()
except subprocess.CalledProcessError as e:
print(e.output)
return caches_info
def get_nic_info():
networks = []
try:
network_output = (
subprocess.check_output("lshw -class network -xml -sanitize", shell=True).strip()).decode()
xslt_root = etree.fromstring(network_output)
networks_list = xslt_root.findall("node[@class='network']")
for e in networks_list:
network = xmltodict.parse(etree.tostring(e), dict_constructor=dict)
networks.append(network.get("node"))
except subprocess.CalledProcessError as e:
print(e.output)
return networks
def get_memory_info():
memories = []
try:
memory_output = (
subprocess.check_output("lshw -class memory -xml -sanitize", shell=True).strip()).decode()
xslt_root = etree.fromstring(memory_output)
memories_list = xslt_root.findall("node[@class='memory']")
for e in memories_list:
memory = xmltodict.parse(etree.tostring(e), dict_constructor=dict)
memories.append(memory.get("node"))
except subprocess.CalledProcessError as e:
print(e.output)
memory_devices = []
memory = {}
try:
memory_output = (
subprocess.check_output("dmidecode -t memory", shell=True).strip()).decode()
memory_output_lines = memory_output.split("\n")
for i,line in enumerate(memory_output_lines):
if "Handle " in line:
if len(memory) != 0:
memory_devices.append(memory)
memory = {
"Description": line,
"Type": memory_output_lines[i+1] if i+1 < len(memory_output_lines) else "-"
}
if ": " in line:
splitted_line = line.split(": ")
memory[splitted_line[0].strip().title()] = splitted_line[-1].strip()
if len(memory) != 0:
memory_devices.append(memory)
except subprocess.CalledProcessError as e:
print(e.output)
return {"Memory": memories, "Memory Devices": memory_devices}
def get_storage_info():
storages = []
try:
storage_output = (
subprocess.check_output("lshw -class storage -class disk -xml -sanitize", shell=True).strip()).decode()
xslt_root = etree.fromstring(storage_output)
disks_list = xslt_root.findall("node/node[@class='disk']")
for e in disks_list:
disk = xmltodict.parse(etree.tostring(e), dict_constructor=dict)
storages.append(disk.get("node"))
except subprocess.CalledProcessError as e:
print(e.output)
return storages
def get_sst_info(sst_command):
sst = {}
try:
datetime_str = time.strftime("%Y%m%d-%H%M%S")
filename = f'out-sst-info-{datetime_str}.json'
subprocess.check_output(f'{sst_command} -o {filename} -f json perf-profile info', shell=True)
if not os.path.isfile(filename):
return {}
f = open(filename, )
sst = json.load(f)
f.close()
subprocess.check_output(f'rm {filename}', shell=True)
except subprocess.CalledProcessError as e:
print(e.output)
return sst
def get_cpu_info():
cpu_info = get_lscpu_info()
try:
ht_output = (subprocess.check_output("cat /sys/devices/system/cpu/smt/active", shell=True).strip()).decode()
cpu_info["Hyperthreading"] = "Enabled" if ht_output == "1" else "Disabled"
except subprocess.CalledProcessError as e:
print(e.output)
if os.path.isdir('/sys/devices/system/cpu/intel_pstate'):
cpu_info["Intel Pstate"] = get_intel_pstate_info()
if "CPUs" in cpu_info and cpu_info["CPUs"] != "" and os.path.isdir('/sys/devices/system/cpu/cpu0/cpufreq/'):
cpu_info["Frequency"], cpu_info["Microcode"] = get_cpu_frequency_and_microcode_info(cpu_info["CPUs"])
return cpu_info
def get_bios_version():
bios_version = ""
try:
bios_version = (subprocess.check_output("dmidecode -s bios-version", shell=True).strip()).decode()
except subprocess.CalledProcessError as e:
print(e.output)
return bios_version
def get_cmdline():
cmdline = ""
try:
cmdline = (subprocess.check_output("cat /proc/cmdline", shell=True).strip()).decode()
except subprocess.CalledProcessError as e:
print(e.output)
return cmdline
def get_distr_info():
distr = {}
try:
lsb_output = (subprocess.check_output("cat /etc/lsb-release", shell=True).strip()).decode()
lsb_lines = lsb_output.split("\n")
for line in lsb_lines:
distr[line.split("=")[0].replace("_", " ").title()] = line.split("=")[-1]
except subprocess.CalledProcessError as e:
print(e.output)
return distr
def get_kernel():
kernel = ""
try:
kernel = (subprocess.check_output("uname -r", shell=True).strip()).decode()
except subprocess.CalledProcessError as e:
print(e.output)
return kernel
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--sst_executable', help='The path of the sst (speed select technology) tool executable')
parser.add_argument('--output', help='The path of the output file')
args = parser.parse_args()
sst_command = args.sst_executable if args.sst_executable is not None else "intel-speed-select"
total_info = {"Cpu Info": get_cpu_info(), "BIOS Version": get_bios_version()}
if os.path.isdir('/sys/devices/system/cpu/cpu0/cache'):
total_info["Caches Info"] = get_caches_info('/sys/devices/system/cpu/cpu0/cache')
total_info["Command Line"] = get_cmdline()
total_info["Distribution Info"] = get_distr_info()
total_info["Kernel Version"] = get_kernel()
total_info["Network Info"] = get_nic_info()
total_info["Memory Info"] = get_memory_info()
total_info["Storage Info"] = get_storage_info()
sst = get_sst_info(sst_command)
total_info["Intel SST"] = sst if sst != {} else "Not-supported"
datetime_str = time.strftime("%Y%m%d-%H%M%S")
filename = args.output if args.output is not None and args.output != "" \
else f"./platform_fingerprint_{datetime_str}.json"
with open(filename, 'w') as f:
json.dump(total_info, f)
print(f"Check the file: {filename}")