01
PART
知识回顾
本篇文章,我们重点介绍服务器熟悉webapi接口的实现。
前期知识储备和回顾,请参考以下文章
01)运维天地-服务器01-X86-PCServer属性介绍
02)运维天地-服务器02-X86-PCServer资产信息获取
03)运维天地-服务器03-X86-PCServer CPU信息获取
04)运维天地-服务器04-X86-PCServer内存信息获取
05)运维天地-服务器05-X86-PCServer磁盘信息获取
06)运维天地-服务器06-X86-PCServer网口信息获取
07)运维天地-服务器07-X86-PCServer系统信息获取
08)运维天地-服务器08-X86-PCServer IP信息获取
特别说明:本文的服务器特指X86-PCServer硬件服务器
02
PART
X86-PCServer属性webapi实现
A
webapi脚本
#!/usr/bin/python
#-*- coding: utf-8 -*-
#作者:王志国 电话:189103 个人微信: it_beijing 个人微信公众号: 大话IT架构
#desc:欢迎关注:个人微信公众号---大话IT架构
#date: 2020.04.02
from wsgiref.simple_server import make_server
import json
import commands
import BrandInfo as brand
import CpuInfo as cpu
import MemInfo as memory
import StorageInfo as disk
import NetInterfaceInfo as net
import OsInfo as os
import IpInfo as ip
manufacturer = brand.BrandInfo().get_manufacturer()
if manufacturer in ("VMWARE", "KVM"):
mechine_type = "vm"
cpuinfo = cpu.CpuInfo().get_all()
cpu_type = cpuinfo[0]
cpu_num = cpuinfo[1]
status, output = commands.getstatusoutput("cat /proc/meminfo | grep -i memtotal")
output = output.split(":")[1].strip().split()
if output[1] == "kB":
mem_size = int(output[0])/1000000
netinfo = net.NetInterfaceInfo().get_all()
net_10g_num = netinfo[1]
net_1g_num = netinfo[2]
osinfo = os.OsInfo().get_all()
os_type = osinfo[0]
os_detail = osinfo[1]
product_ip = ip.IpInfo().get_product_ip()
hardware_info = {}
hardware_info['cpu_type'] = cpu_type
hardware_info['cpu_num'] = cpu_num
hardware_info['mem_size'] = cpu_num
hardware_info['net_10g_num'] = net_10g_num
hardware_info['net_1g_num'] = net_1g_num
hardware_info['os_type'] = os_type
hardware_info['os_detail'] = os_detail
hardware_info['product_ip'] = product_ip
else:
mechine_type = "server"
brandinfo = brand.BrandInfo().get_all()
manufacturer = brandinfo[0]
product_name = brandinfo[1]
serial_number = brandinfo[2]
cpuinfo = cpu.CpuInfo().get_all()
cpu_type = cpuinfo[0]
cpu_num = cpuinfo[1]
meminfo = memory.MemInfo().get_all()
mem_ddr = meminfo[0]
mem_speed = meminfo[1]
mem_size = meminfo[2]
mem_num = meminfo[3]
mem_slot_num = meminfo[4]
diskinfo = disk.StorageInfo().get_all()
raid_adapter = diskinfo[0]
disk_type1 = diskinfo[1][0]
disk_type1_num = diskinfo[1][1]
disk_type2 = diskinfo[1][2]
disk_type2_num = diskinfo[1][3]
disk_type3 = diskinfo[1][4]
disk_type3_num = diskinfo[1][5]
netinfo = net.NetInterfaceInfo().get_all()
net_10g_num = netinfo[1]
net_1g_num = netinfo[2]
osinfo = os.OsInfo().get_all()
os_type = osinfo[0]
os_detail = osinfo[1]
ipinfo = ip.IpInfo().get_all()
product_ip = ipinfo[0]
ipmi_ip = ipinfo[1]
hardware_info = {}
hardware_info['mechine_type'] = mechine_type
hardware_info['manufacturer'] = manufacturer
hardware_info['product_name'] = product_name
hardware_info['serial_number'] = serial_number
hardware_info['cpu_type'] = cpu_type
hardware_info['cpu_num'] = cpu_num
hardware_info['mem_ddr'] = mem_ddr
hardware_info['mem_speed'] = mem_speed
hardware_info['mem_size'] = mem_size
hardware_info['mem_sum'] = mem_num
hardware_info['mem_slot_sum'] = mem_slot_num
hardware_info['raid_adapter'] = raid_adapter
hardware_info['disk_type1'] = disk_type1
hardware_info['disk_type1_num'] = disk_type1_num
hardware_info['disk_type2'] = disk_type2
hardware_info['disk_type2_num'] = disk_type2_num
hardware_info['disk_type3'] = disk_type3
hardware_info['disk_type3_num'] = disk_type3_num
hardware_info['net_10g_num'] = net_10g_num
hardware_info['net_1g_num'] = net_1g_num
hardware_info['os_type'] = os_type
hardware_info['os_detail'] = os_detail
hardware_info['product_ip'] = product_ip
hardware_info['ipmi_ip'] = ipmi_ip
def application(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
result = json.dumps(hardware_info)
return [result]
ip = '0.0.0.0'
port = 10000
httpd = make_server(ip, port, application)
print("作者:王志国 电话:189103 个人微信: it_beijing 个人微信公众号: 大话IT架构")
print("欢迎关注:个人微信公众号---大话IT架构")
print("server is started, port is 10000...")
httpd.serve_forever()
B
获取方法概述
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有