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) + "