dmidecode收集硬件信息命令

dmidecode

dmidecode 使用 SMBIOS/DMI 标准,在Linux下收集相关硬件信息

# 特殊使用方式
# 如 直接使用dmidecode 得出如下结果
Handle 0x0001, DMI type 1, 27 bytes
System Information
	Manufacturer: VMware, Inc.
	Product Name: VMware Virtual Platform
	Version: None
	Serial Number: VMware-56 4d 83 35 b6 1a ca 95-e6 c4 c9 f3 b6 e8 69 cd
	UUID: 564D8335-B61A-CA95-E6C4-C9F3B6E869CD
	Wake-up Type: Power Switch
	SKU Number: Not Specified
	Family: Not Specified

Handle 0x0004, DMI type 4, 35 bytes
Processor Information
	Socket Designation: CPU socket #0
	Type: Central Processor
	Family: Unknown
	Manufacturer: GenuineIntel
	ID: C3 06 03 00 FF FB AB 1F
	Version: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz

# 获取系统平台   dmidecode -s system-product-name     只需要前面 加-s  system-key小写空格改为-
# 需要注意的是 看它换行来决定它的前缀 如 processor information 那就是 -s processor-名称
# 如 获取cpu版本    dmidecode -s processor-version | head -n 1 只需要取第一个

获取CPU型号

]# dmidecode -t processor | grep Version | head -n 1
    Version: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz

获取服务器型号

]# dmidecode | grep 'Product Name'
    Product Name: VMware Virtual Platform
    Product Name: 440BX Desktop Reference Platform

获取主机序列号

]# dmidecode -s system-serial-number
VMware-56 4d 83 35 b6 1a ca 95-e6 c4 c9 f3 b6 e8 69 cd

查看内存的汇总信息

]# dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range | grep -v "No Module Installed"
    Size: 2048 MB

查看主板支持的最大内存

]# dmidecode|grep 'Maximum Capacity'
    Maximum Capacity: 1 TB

Linux系统查看内存规格信息

]# dmidecode|grep -A16 'Memory Device'|grep 'Speed'
Speed: 1333 MHz
Speed: 1333 MHz
Speed: 1333 MHz
Speed: 1333 MHz

查看CPU信息

]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    2
座:                 1
NUMA 节点:         1
厂商 ID:           GenuineIntel
CPU 系列:          6
型号:              60
型号名称:        Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
步进:              3
CPU MHz:             3597.915
BogoMIPS:            7195.83
超管理器厂商:  VMware
虚拟化类型:     完全
L1d 缓存:          32K
L1i 缓存:          32K
L2 缓存:           256K
L3 缓存:           8192K
NUMA 节点0 CPU:    0,1

 获取系统信息

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import subprocess
import platform
import psutil


def command(com):
    # 执行命令, 返回获取的结果
    result = subprocess.Popen(com,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              shell=True)
    _getData = str(result.stdout.read().strip(), 'utf-8')  # 将 bytes 转换成 str格式并且去掉\n
    return _getData


def sysinfo():
    # 当前磁盘采集的只支持如下磁盘类型
    _dick_type = ['NTFS', 'XFS', 'EXT3', 'EXT4']
    # 只获取  内存,cpu, 机器, 磁盘的固定结果
    dic = {"memory": {}, "cpu": {}, "machine": {}, "disk": {}}

    # ------- 内存  固定值: 总大小, 内存的规格如 speed: 1333Mhz
    dic["memory"].update({'memTotal': round(psutil.virtual_memory().total / 1024 / 1024 / 1024, 2)})  # 内存的总大小
    memType = command("dmidecode|grep -A16 'Memory Device'|grep 'Speed' | head -n 1 | cut -d ':' -f 2")
    dic["memory"].update({"type": memType})

    # 固定值:  物理数, 逻辑核数, cpu型号
    dic["cpu"].update({"cpuCount": psutil.cpu_count(logical=False)})  # 获取CPU的物理数
    dic["cpu"].update({"cpuLogicalCount": psutil.cpu_count()})  # 线程数
    # 获取CPU型号
    version = command("dmidecode -s processor-version | head -n 1")
    dic['cpu'].update({"cpuVersion": version})

    # 固定值: 序列号, 服务器型号,操作系统, 制造商, 资产编号, 系统版本
    dic['machine'].update({"serialNum": command('dmidecode -s system-serial-number')})  # 序列号
    dic['machine'].update({"serverProduct": command("dmidecode -s system-product-name")})  # 服务器型号
    dic['machine'].update({"Manufacturer": command("dmidecode -s system-manufacturer")})  # 制造商
    dic['machine'].update({"UUID": command("dmidecode -s system-uuid")})  # 序列号
    dic["machine"].update({'system': platform.system()})  # 操作系统
    dic["machine"].update({'platform': platform.platform()})  # 系统版本
    dic["machine"].update({'sysname': platform.node()})  # 主机名称

    for sdick in psutil.disk_partitions():
        # 判断类型是否存在列表中
        if sdick.fstype.upper() in _dick_type:
            #  [{'/dev/mapper/centos-root': '/'},]  逻辑名称, 挂载位置
            size = round(psutil.disk_usage(sdick.mountpoint).total / 1024 / 1024 / 1024, 2)
            dic['disk'].update({sdick.device: size})
            # dic[sdick.device] = size  # {'/dev/mapper/centos-root': 47.44, '/dev/sda1': 0.49}

    return dic
s = sysinfo()
print(s)

猜你喜欢

转载自blog.csdn.net/u010304195/article/details/100129054