-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathdmidecode.py
229 lines (196 loc) · 6.38 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
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
import logging
import re as _re
import subprocess as _subprocess
import sys
from netbox_agent.misc import is_tool
_handle_re = _re.compile("^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$")
_in_block_re = _re.compile("^\\t\\t(.+)$")
_record_re = _re.compile("\\t(.+):\\s+(.+)$")
_record2_re = _re.compile("\\t(.+):$")
_type2str = {
0: "BIOS",
1: "System",
2: "Baseboard",
3: "Chassis",
4: "Processor",
5: "Memory Controller",
6: "Memory Module",
7: "Cache",
8: "Port Connector",
9: "System Slots",
10: " On Board Devices",
11: " OEM Strings",
12: " System Configuration Options",
13: " BIOS Language",
14: " Group Associations",
15: " System Event Log",
16: " Physical Memory Array",
17: " Memory Device",
18: " 32-bit Memory Error",
19: " Memory Array Mapped Address",
20: " Memory Device Mapped Address",
21: " Built-in Pointing Device",
22: " Portable Battery",
23: " System Reset",
24: " Hardware Security",
25: " System Power Controls",
26: " Voltage Probe",
27: " Cooling Device",
28: " Temperature Probe",
29: " Electrical Current Probe",
30: " Out-of-band Remote Access",
31: " Boot Integrity Services",
32: " System Boot",
33: " 64-bit Memory Error",
34: " Management Device",
35: " Management Device Component",
36: " Management Device Threshold Data",
37: " Memory Channel",
38: " IPMI Device",
39: " Power Supply",
40: " Additional Information",
41: " Onboard Devices Extended Information",
42: " Management Controller Host Interface",
}
_str2type = {}
for type_id, type_str in _type2str.items():
_str2type[type_str] = type_id
def parse(output=None):
"""
parse the full output of the dmidecode
command and return a dic containing the parsed information
"""
if output:
buffer = output
else:
buffer = _execute_cmd()
if isinstance(buffer, bytes):
buffer = buffer.decode("utf-8")
_data = _parse(buffer)
return _data
def get_by_type(data, type_id):
"""
filter the output of dmidecode per type
0 BIOS
1 System
2 Baseboard
3 Chassis
4 Processor
5 Memory Controller
6 Memory Module
7 Cache
8 Port Connector
9 System Slots
10 On Board Devices
11 OEM Strings
12 System Configuration Options
13 BIOS Language
14 Group Associations
15 System Event Log
16 Physical Memory Array
17 Memory Device
18 32-bit Memory Error
19 Memory Array Mapped Address
20 Memory Device Mapped Address
21 Built-in Pointing Device
22 Portable Battery
23 System Reset
24 Hardware Security
25 System Power Controls
26 Voltage Probe
27 Cooling Device
28 Temperature Probe
29 Electrical Current Probe
30 Out-of-band Remote Access
31 Boot Integrity Services
32 System Boot
33 64-bit Memory Error
34 Management Device
35 Management Device Component
36 Management Device Threshold Data
37 Memory Channel
38 IPMI Device
39 Power Supply
40 Additional Information
41 Onboard Devices Extended Information
42 Management Controller Host Interface
"""
if isinstance(type_id, str):
type_id = _str2type.get(type_id)
if type_id is None:
return None
result = []
for entry in data.values():
if entry["DMIType"] == type_id:
result.append(entry)
return result
def _execute_cmd():
if not is_tool("dmidecode"):
logging.error(
"Dmidecode does not seem to be present on your system. Add it your path or "
"check the compatibility of this project with your distro."
)
sys.exit(1)
return _subprocess.check_output(
[
"dmidecode",
],
stderr=_subprocess.PIPE,
)
def _parse(buffer):
output_data = {}
# Each record is separated by double newlines
split_output = buffer.split("\n\n")
for record in split_output:
record_element = record.splitlines()
# Entries with less than 3 lines are incomplete / inactive; skip them
if len(record_element) < 3:
continue
handle_data = _handle_re.findall(record_element[0])
if not handle_data:
continue
handle_data = handle_data[0]
dmi_handle = handle_data[0]
output_data[dmi_handle] = {}
output_data[dmi_handle]["DMIType"] = int(handle_data[1])
output_data[dmi_handle]["DMISize"] = int(handle_data[2])
# Okay, we know 2nd line == name
output_data[dmi_handle]["DMIName"] = record_element[1]
in_block_elemet = ""
in_block_list = ""
# Loop over the rest of the record, gathering values
for i in range(2, len(record_element), 1):
if i >= len(record_element):
break
# Check whether we are inside a \t\t block
if in_block_elemet != "":
in_block_data = _in_block_re.findall(record_element[i])
if in_block_data:
if not in_block_list:
in_block_list = [in_block_data[0]]
else:
in_block_list.append(in_block_data[0])
output_data[dmi_handle][in_block_elemet] = in_block_list
continue
else:
# We are out of the \t\t block; reset it again, and let
# the parsing continue
in_block_elemet = ""
record_data = _record_re.findall(record_element[i])
# Is this the line containing handle identifier, type, size?
if record_data:
output_data[dmi_handle][record_data[0][0]] = record_data[0][1]
continue
# Didn't findall regular entry, maybe an array of data?
record_data2 = _record2_re.findall(record_element[i])
if record_data2:
# This is an array of data - let the loop know we are inside
# an array block
in_block_elemet = record_data2[0]
in_block_list = ""
continue
if not output_data:
raise ParseError("Unable to parse 'dmidecode' output")
return output_data
class ParseError(Exception):
pass