python linux命令 lshw -json解读硬件信息

python linux命令 lshw -json解读硬件信息

python 编写读取ubuntu18.04命令lshw -json采集信息来解读硬件信息

有时间的同学可以写一个类,直接序列化,由于没时间,就采集主要字段存入数据库

代码如下

from django.shortcuts import render
from libs.utils import json_response
from .models import ServerHost, MasterBoard, CentralProcessingUnit, Memory, Storage, Display, NetWork,SASCard
import json
import datetime
import os


def import_lsplw_allfile(request):
    file_dir = "/Users/hxl/lshw/"
    file_list = os.listdir(file_dir)
    serverNum = 0
    for cur_file in file_list:
        path = os.path.join(file_dir, cur_file)
        if os.path.isfile(path):
            print(path)
            if path.endswith(".json"):
                serverNum += 1
                resolving_jsonfile(path)
    return json_response(str(serverNum) + " 服务器存入数据库!")


def import_lsplw_allfile_test(request):
    resolving_jsonfile("/Users/hxl/lshw/192.168.1.160_lshw.json")
    return json_response(str(1) + " 服务器存入数据库!")


def resolving_jsonfile(path):
    with open(path) as f:
        data = json.load(f)
        if(isinstance(data, list)):
            data = data[0]
        items = data.items()
        server_host = ServerHost()
        masterBoard = MasterBoard()
        centralProcessingUnitArray = []
        memoryArray = []
        storageArray = []
        displayArray = []
        netWorkArray = []
        sasCardArray = []
        server_host.json_data = data
        for server_key, server_value in items:
            if "id" == server_key:
                server_host._id = server_value
            elif "product" == server_key:
                server_host.product = server_value
            elif "vendor" == server_key:
                server_host.vendor = server_value
            elif "version" == server_key:
                server_host.version = server_value
            elif "serial" == server_key:
                server_host.serial = server_value
            elif "children" == server_key:
                for motherBoard_HardWare in server_value:
                    # MotherBoard
                    if 'id' in motherBoard_HardWare and "core" == motherBoard_HardWare["id"]:
                        masterBoard.json_data = motherBoard_HardWare
                        if "product" in motherBoard_HardWare:
                            masterBoard.product = motherBoard_HardWare["product"]
                        if "vendor" in motherBoard_HardWare:
                            masterBoard.vendor = motherBoard_HardWare["vendor"]
                        if "version" in motherBoard_HardWare:
                            masterBoard.version = motherBoard_HardWare["version"]
                        if "serial" in motherBoard_HardWare:
                            masterBoard.serial = motherBoard_HardWare["serial"]
                        if "children" in motherBoard_HardWare:
                            for motherBoard_HardWareParts in motherBoard_HardWare["children"]:
                                # CPU
                                if "class" in motherBoard_HardWareParts and "processor" == motherBoard_HardWareParts["class"]:
                                    cpu = CentralProcessingUnit()
                                    cpu.json_data = motherBoard_HardWareParts
                                    if "id" in motherBoard_HardWareParts:
                                        cpu._id = motherBoard_HardWareParts["id"]
                                    if "product" in motherBoard_HardWareParts:
                                        cpu.product = motherBoard_HardWareParts["product"]
                                    if "vendor" in motherBoard_HardWareParts:
                                        cpu.vendor = motherBoard_HardWareParts["vendor"]
                                    if "version" in motherBoard_HardWareParts:
                                        cpu.version = motherBoard_HardWareParts["version"]
                                    if "serial" in motherBoard_HardWareParts:
                                        cpu.serial = motherBoard_HardWareParts["serial"]
                                    centralProcessingUnitArray.append(cpu)
                                # MEMORY
                                elif "id" in motherBoard_HardWareParts and "class" in motherBoard_HardWareParts and "memory" == motherBoard_HardWareParts["id"] and "memory" == motherBoard_HardWareParts["class"]:
                                    if "children" in motherBoard_HardWareParts :
                                        for memory_firmware in motherBoard_HardWareParts["children"]:
                                                memory = Memory()
                                                memory.json_data = memory_firmware
                                                if "id" in memory_firmware:
                                                    memory._id = memory_firmware["id"]
                                                if "product" in memory_firmware:
                                                    memory.product = memory_firmware["product"]
                                                if "vendor" in memory_firmware:
                                                    memory.vendor = memory_firmware["vendor"]
                                                if "serial" in memory_firmware:
                                                    memory.serial = memory_firmware["serial"]
                                                if "size" in memory_firmware:
                                                        memory.size = str(
                                                            memory_firmware["size"]/1024/1024/1024) + "
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值