python-dmiparser
About
This parse dmidecode output to JSON text.
Installation
PyPI
pip3 install -U dmiparser
RPM
git clone https://github.com/Arondight/python-dmiparser.git
cd ./python-dmiparser/
python3 ./setup.py bdist --format=rpm
sudo dnf install ./dist/dmiparser-*.noarch.rpm
Tip: Requires the
rpm-buildpackage in your Linux distribution.
Usage
Python 3 script
DmiParser
This accepts a str (with the output of dmidecode) as argument and converts it to JSON text.
#!/usr/bin/env python3
import json
from dmiparser import DmiParser
from functools import partial
def report(*args: str) -> None:
"""report texts with format
@param args: text string
"""
br = lambda e: print("-" * e)
brn = partial(br, 80)
brn()
for e in args:
print(e)
brn()
if "__main__" == __name__:
text = (
"# dmidecode 3.6\n"
"Getting SMBIOS data from sysfs.\n"
"SMBIOS 3.7.0 present.\n"
"\n"
"Handle 0x0000, DMI type 0, 26 bytes\n"
"Platform Firmware Information\n"
"\tVendor: American Megatrends International, LLC.\n"
"\tVersion: 3A09.QCT001\n"
"\tRelease Date: 11/14/2024\n"
"\tAddress: 0xF0000\n"
"\tRuntime Size: 64 KiB\n"
"\tROM Size: 64 MiB\n"
"\tCharacteristics:\n"
"\t\tPCI is supported\n"
"\t\tFirmware is upgradeable\n"
"\t\tFirmware shadowing is allowed\n"
"\t\tBoot from CD is supported\n"
"\t\tSelectable boot is supported\n"
"\t\tFirmware ROM is socketed\n"
"\t\tEDD is supported\n"
"\t\tACPI is supported\n"
"\t\tBIOS boot specification is supported\n"
"\t\tTargeted content distribution is supported\n"
"\t\tUEFI is supported\n"
"\tPlatform Firmware Revision: 5.35\n"
"\tEmbedded Controller Firmware Revision: 3.9\n"
"\n"
)
# parser = DmiParser(text)
parser = DmiParser(text, sort_keys=True, indent=2)
parsedStr = str(parser) # get str
parsedObj = json.loads(str(parser)) # get object
report(parsedStr, parsedObj)
DmiDecoder (the default wrapper)
This run dmidecode and converting the output of the command to JSON text.
from dmiparser.dmidecoder import DmiDecoder
from functools import partial
def report(*args: str) -> None:
"""report texts with format
@param args: text string
"""
br = lambda e: print("-" * e)
brn = partial(br, 80)
brn()
for e in args:
print(e)
brn()
def getCpuInfo(dmidecoder) -> str:
"""Get CPU information, will return text like below.
CPU_0:
Family: Xeon
Version: Intel(R) Xeon(R) 6767P
Voltage: 1.8 V
Speed: 2400 MHz/3900 MHz
Status: Populated, Enabled
Core: 64/64
Thread: 128
CPU_1:
Family: Xeon
Version: Intel(R) Xeon(R) 6767P
Voltage: 1.8 V
Speed: 2400 MHz/3900 MHz
Status: Populated, Enabled
Core: 64/64
Thread: 128
@param dmidecoder: DmiDecoder object
@return: text of CPU information
"""
text = ""
for sid, name in dmidecoder.sections:
def getFirst(*args):
vals = dmidecoder.getProp(*args, id=sid, name=name)
return vals[0] if len(vals) > 0 else None
text += "{}:\n".format(getFirst("Socket Designation"))
text += "\tFamily: {}\n".format(getFirst("Family"))
text += "\tVersion: {}\n".format(getFirst("Version"))
text += "\tVoltage: {}\n".format(getFirst("Voltage"))
text += "\tSpeed: {}/{}\n".format(getFirst("Current Speed"), getFirst("Max Speed"))
text += "\tStatus: {}\n".format(getFirst("Status"))
text += "\tCore: {}/{}\n".format(getFirst("Core Enabled"), getFirst("Core Count"))
text += "\tThread: {}\n".format(getFirst("Thread Count"))
return text
if "__main__" == __name__:
# dmidecoder = DmiDecoder()
dmidecoder4 = DmiDecoder("-t 4", sort_keys=True, indent=2) # Type 4 is Processor
report(dmidecoder4.text, str(dmidecoder4.data), getCpuInfo(dmidecoder4))
Tip: Superuser permissions are required here to run
dmidecode.
Executable command
dmiparser
This read output of dmidecode from pipe and print it as JSON text.
sudo dmidecode | dmiparser
sudo dmidecode -t 4 | dmiparser --format
sudo dmidecode >/tmp/dmidecode.txt
dmiparser </tmp/dmidecode.txt
Tip: you can run
dmiparsermodule as a script (usepython3 -m dmiparserinstead ofdmiparsercommand).
dmidecoder
This run dmidecode and print the output as JSON text.
sudo dmidecoder
sudo dmidecoder --arguments "-t 4" --format
Tip: you can run
dmiparser.dmidecodermodule as a script (usepython3 -m dmiparser.dmidecoderinstead ofdmidecodercommand).
Development
Test
tox
Format
black -l 120 ./dmiparser/ ./tests/
License
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dmiparser-6.0.tar.gz.
File metadata
- Download URL: dmiparser-6.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91c37c26d36cab264b4e608561aeecf3101cdc95851bc202829ce9f868b73d28
|
|
| MD5 |
d367f1539062fbd8e0da3deb20589950
|
|
| BLAKE2b-256 |
8e576d0a63b142efdf9975cadb9388708651d51275850b6311647c3a8a93c7ea
|
File details
Details for the file dmiparser-6.0-py3-none-any.whl.
File metadata
- Download URL: dmiparser-6.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1246d196f9f1192d30d1af8b648d1369bc3a8e6fdfdae25307144c361a8f7253
|
|
| MD5 |
3c56abf766d018efb7042e1ebfbbb84c
|
|
| BLAKE2b-256 |
e587473cf2af387d20644132ac7527295bbf551b132625969e91d33b91d2b289
|