Skip to main content

This parse dmidecode output to JSON text

Project description

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-build package 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.0\n"
        "Getting SMBIOS data from sysfs.\n"
        "SMBIOS 2.7 present.\n"
        "\n"
        "Handle 0x0003, DMI type 2, 17 bytes\n"
        "Base Board Information\n"
        "\tManufacturer: Intel Corporation\n"
        "\tProduct Name: S2600WT2R\n"
        "\tVersion: H21573-372\n"
        "\tSerial Number: BQWL81150522\n"
        "\tAsset Tag: Base Board Asset Tag\n"
        "\tFeatures:\n"
        "\t\tBoard is a hosting board\n"
        "\t\tBoard is replaceable\n"
        "\tLocation In Chassis: Part Component\n"
        "\tChassis Handle: 0x0000\n"
        "\tType: Motherboard\n"
        "\tContained Object Handles: 0\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.

    CPU1:
        Family: Xeon
        Version: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
        Voltage: 1.8 V
        Speed: 2200 MHz/4000 MHz
        Status: Populated, Enabled
        Core: 10/10
        Thread: 20
    CPU2:
        Family: Xeon
        Version: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
        Voltage: 1.8 V
        Speed: 2200 MHz/4000 MHz
        Status: Populated, Enabled
        Core: 10/10
        Thread: 20

    @param: dmidecode: DmiDecode object
    @return: text of CPU information
    """
    text = ""

    for id, name in dmidecoder.sections:
        def getFirst(*args):
            vals = dmidecoder.getProp(*args, id=id, 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 dmiparser module as a script (use python3 -m dmiparser instead of dmiparser command).

dmidecoder

This run dmidecode and print the output as JSON text.

sudo dmidecoder
sudo dmidecoder --arguments "-t 4" --format

Tip: you can run dmiparser.dmidecoder module as a script (use python3 -m dmiparser.dmidecoder instead of dmidecoder command).

Development

Test

tox

Format

black -l 120 ./dmiparser/ ./tests/

License

MIT LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dmiparser-5.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

dmiparser-5.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file dmiparser-5.1.tar.gz.

File metadata

  • Download URL: dmiparser-5.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for dmiparser-5.1.tar.gz
Algorithm Hash digest
SHA256 3f80fc6336abf3fe59b88409b2170ec7327107e32b3dfb5f196a83782de4e4bd
MD5 10cba017fa2e5c43490829642629905d
BLAKE2b-256 338d5e577e4aea3a9d92430906cca61cc36d98c13796be4792ee479ebfca16bd

See more details on using hashes here.

File details

Details for the file dmiparser-5.1-py3-none-any.whl.

File metadata

  • Download URL: dmiparser-5.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for dmiparser-5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3395cc59f80477483dc1a0cf11d42e4245f5fd6ccaf40a28056bbf7d2b51e53c
MD5 04143090094f27406344a5f5bcd5ca4b
BLAKE2b-256 a49bdf2aa5734dfe17f82ad9eab85c770bd1aa3905326e181a99c0ebcae3cfec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page