-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw_info_collector.py
303 lines (282 loc) · 14.9 KB
/
hw_info_collector.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
auther : freeyssu@gmail.com
purpose : collect hw meta info - chassis, system, cpu, mainboard, dimm, gpu, psu, nic, transceiver
usage : python hw_info_collector.py
todo : hw utilization, ipmi sensor info
'''
import os
import logging
import logging.handlers
import subprocess
import re
import traceback
import time
import sys
import json
class HWInfoCollector():
def __init__(self, debug: bool = False):
self._logger = logging.getLogger('HWInfoCollector')
self._logger.setLevel(logging.DEBUG if debug else logging.INFO)
self._logger_formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(funcName)s | %(message)s')
self._logger_stream_handler = logging.StreamHandler()
self._logger_stream_handler.setFormatter(self._logger_formatter)
self._logger.addHandler(self._logger_stream_handler)
if os.geteuid() != 0:
self._logger.error('Root privileges required.')
raise Exception('Root privileges required.')
if sys.version_info[0] != 3:
self._logger.error('Python version 3 required.')
raise Exception('Python version 3 required.')
def run_command(self, command: str):
'''
Return `CompletedProcess` object after completed run process
'''
self._logger.debug('cmd : {}'.format(command))
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
self._logger.debug('cmd result: {}'.format(result))
return result
def run_command_print_result(self, command: str):
'''
Display all stdout/stderr and wait for process exit\n
Return process return code (`int`)
'''
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
while True:
output = process.stdout.readline().decode().strip()
if output == '' and process.poll() is not None:
break
if output:
print(output)
time.sleep(0.01)
return_code = process.poll()
return return_code
def get_multiple_items_from_stdout(self, regex: str, string: str, re_flag: re.RegexFlag = re.M):
'''
Return seached `list` of string by regex.\n
"regex" arg has to include a group expression
'''
try:
return [line for line in re.findall(regex, string, re_flag)]
except Exception:
self._logger.error('Cannot get multipe items with regex {}.\n{}'.format(regex, traceback.format_exc()))
return []
def get_single_item_from_stdout(self, regex: str, string: str, re_flag: re.RegexFlag = 0):
'''
"regex" arg has to include a group expression\n
Return a seached `str` by regex.
'''
try:
if re.search(regex, string, re_flag):
return re.search(regex, string, re_flag).group(1).strip()
except Exception:
self._logger.error('Cannot get a single item with regex {}.\n{}'.format(regex, traceback.format_exc()))
return 'N/A'
def get_hw_meta_info(self):
'''
Collect all hw meta infomation
Return `dict` object
'''
all_hw_info = {'exception': []}
# chassis info
target_component = 'chassis'
try:
cmd_result = self.run_command('dmidecode -t 3').stdout.decode().strip()
component_info = {
'brand': self.get_single_item_from_stdout(r'Manufacturer:\s*(.*)', cmd_result).strip(),
'serial_number': self.get_single_item_from_stdout(r'Serial Number:\s*(.*)', cmd_result).strip()
}
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# chassis/system info
target_component = 'system'
try:
cmd_result = self.run_command('dmidecode -t 1').stdout.decode().strip()
component_info = {
'brand': self.get_single_item_from_stdout(r'Manufacturer:\s*(.*)', cmd_result).strip(),
'model': self.get_single_item_from_stdout(r'Product Name:\s*(.*)', cmd_result).strip(),
'serial_number': self.get_single_item_from_stdout(r'Serial Number:\s*(.*)', cmd_result).strip()
}
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# cpu info
target_component = 'cpu'
try:
cmd_result = self.run_command('dmidecode -t 4').stdout.decode().strip()
component_info = {
'brand': self.get_single_item_from_stdout(r'Manufacturer:\s*(.*)', cmd_result).strip(),
'model': self.get_single_item_from_stdout(r'Version:\s*(.*)', cmd_result).strip(),
'core': self.get_single_item_from_stdout(r'Core Count:\s*(.*)', cmd_result).strip(),
'thread': self.get_single_item_from_stdout(r'Thread Count:\s*(.*)', cmd_result).strip()
}
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# mainboard info
target_component = 'mainboard'
try:
cmd_result = self.run_command('dmidecode -t 2').stdout.decode().strip()
component_info = {
'brand': self.get_single_item_from_stdout(r'Manufacturer:\s*(.*)', cmd_result).strip(),
'model': self.get_single_item_from_stdout(r'Product Name:\s*(.*)', cmd_result).strip(),
'serial_number': self.get_single_item_from_stdout(r'Serial Number:\s*(.*)', cmd_result).strip()
}
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# dimmm info
target_component = 'dimm'
try:
cmd_result = self.run_command('dmidecode -t 17 -q').stdout.decode().strip()
component_info = {}
for dimm_info in re.split('^\n', cmd_result, flags=re.M):
if re.search(r'No Module Installed', dimm_info):
continue
component_info.update({
self.get_single_item_from_stdout(r'^\s*Locator: (.*)', dimm_info, re.M): {
'size': self.get_single_item_from_stdout(r'Size: (.*)', dimm_info),
'brand': self.get_single_item_from_stdout(r'Manufacturer:\s*(.*)', dimm_info),
'model': self.get_single_item_from_stdout(r'Part Number:\s*(.*)', dimm_info),
'serial_number': self.get_single_item_from_stdout(r'Serial Number:\s*(.*)', dimm_info)
}
})
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# gpu info
# one of nvidia-smi or nvflash must be available
target_component = 'gpu'
try:
component_info = {}
gpu_util = None
if self.run_command('nvflash --list').returncode == 0:
self._logger.info('nvflash is available.')
cmd_result = self.run_command('nvflash --list').stdout.decode().strip()
gpu_pci_list = self.get_multiple_items_from_stdout(r'S:.*,B:(.*),D:(.*),F:.*', cmd_result)
for gpu_pci in gpu_pci_list:
pci_id = '{}:{}'.format(gpu_pci[0], gpu_pci[1])
cmd_result = self.run_command(
'./nvflash --rdobd --pcisegbus={}'.format(pci_id)).stdout.decode().strip()
component_info.update({
pci_id: {
'model': self.get_single_item_from_stdout(r'MarketingName:\s*(.*)', cmd_result).strip(),
'serial_number':
self.get_single_item_from_stdout(r'BoardSerialNumber:\s*(.*)', cmd_result).strip()
}
})
cmd_result = self.run_command(
'./nvflash --version --pcisegbus={}:{}'.format(gpu_pci[0], gpu_pci[1])).stdout.decode().strip()
component_info[pci_id].update({
'vbios': self.get_single_item_from_stdout(r'Version\s*:\s*(.*)', cmd_result).strip(),
'inforom': self.get_single_item_from_stdout(r'InfoROM Version\s*:\s*(.*)', cmd_result).strip()
})
elif self.run_command('nvidia-smi --list-gpus').returncode == 0:
self._logger.info('nvidia-smi is available.')
cmd_result = self.run_command(
'nvidia-smi --query-gpu=name,serial,pci.bus_id,vbios_version,inforom.img,memory.total '
'--format=csv,noheader').stdout.decode().strip()
gpu_info_list = self.get_multiple_items_from_stdout(
r'(.*), (.*), 00000000:(.*)\.0, (.*), (.*), (.*)', cmd_result)
for gpu_info in gpu_info_list:
component_info.update({
gpu_info[2]: {
'model': gpu_info[0],
'serial_number': gpu_info[1],
'vbios': gpu_info[3],
'inforom': gpu_info[4],
'memory': gpu_info[5]
}
})
else:
raise Exception('One of nvidia-smi or nvflash must be available.')
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# psu info
target_component = 'psu'
try:
cmd_result = self.run_command('dmidecode -t 39 -q').stdout.decode().strip()
component_info = {}
for dimm_info in re.split('^\n', cmd_result, flags=re.M):
component_info.update({
self.get_single_item_from_stdout(r'Location: (.*)', dimm_info): {
'brand': self.get_single_item_from_stdout(r'Manufacturer:\s*(.*)', dimm_info),
'model': self.get_single_item_from_stdout(r'Name:\s*(.*)', dimm_info),
'power': self.get_single_item_from_stdout(r'Max Power Capacity:\s*(.*)', dimm_info),
'serial_number': self.get_single_item_from_stdout(r'Serial Number:\s*(.*)', dimm_info)
}
})
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
# nic info
# lshw package required
target_component = 'nic'
try:
if self.run_command('lshw -version').returncode != 0:
raise Exception('lshw package required to collect NIC info.')
cmd_result = self.run_command('lshw -c network').stdout.decode().strip()
component_info = {}
for nic_info in re.split(r'\*\-network:.*', cmd_result)[1:]:
if re.search(r'virtual', self.get_single_item_from_stdout(r'product: (.*)', nic_info), re.I):
continue
nic_name = self.get_single_item_from_stdout(r'logical name: (.*)', nic_info)
pci_id = self.get_single_item_from_stdout(r'bus info: pci@0000:(.*)', nic_info)
component_info.update({
nic_name: {
'brand': self.get_single_item_from_stdout(r'vendor:\s*(.*)', nic_info),
'model': self.get_single_item_from_stdout(r'product:\s*(.*)', nic_info),
'mac': self.get_single_item_from_stdout(r'serial:\s*(.*)', nic_info),
'pci_id': pci_id,
'firmware': self.get_single_item_from_stdout(r'firmware=(\S*)', nic_info)
}
})
cmd_result = self.run_command('lspci -vv -s {}'.format(pci_id)).stdout.decode().strip()
component_info[nic_name]['serial_number'] = self.get_single_item_from_stdout(
r'Serial number:\s*(.*)', cmd_result)
if self.get_single_item_from_stdout(r'Part number:\s*(.*)', cmd_result) != 'N/A':
component_info[nic_name]['model'] = self.get_single_item_from_stdout(
r'Part number: (.*)', cmd_result)
cmd_result = self.run_command('ethtool -m {}'.format(nic_name)).stdout.decode().strip()
if self.get_single_item_from_stdout(r'Vendor name\s*:\s*(.*)', cmd_result) != 'N/A':
component_info[nic_name]['transceiver_brand'] = self.get_single_item_from_stdout(
r'Vendor name\s*:\s*(.*)', cmd_result)
component_info[nic_name]['transceiver_model'] = self.get_single_item_from_stdout(
r'Vendor PN\s*:\s*(.*)', cmd_result)
component_info[nic_name]['transceiver_type'] = self.get_single_item_from_stdout(
r'Transceiver type\s*:\s*(.*)', cmd_result)
all_hw_info[target_component] = component_info
except Exception as e:
all_hw_info['exception'].append({
target_component: traceback.format_exc()
})
self._logger.error('Cannot collect {} info.\n{}'.format(target_component, traceback.format_exc()))
return all_hw_info
if __name__ == '__main__':
hw_info_collector = HWInfoCollector()
hw_info = hw_info_collector.get_hw_meta_info()
print(json.dumps(hw_info, indent=2))