前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >运维天地-服务器09-X86-PCServer属性webapi接口实现

运维天地-服务器09-X86-PCServer属性webapi接口实现

作者头像
大话IT架构
发布2020-04-15 15:53:40
3080
发布2020-04-15 15:53:40
举报
文章被收录于专栏:大话IT架构大话IT架构

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脚本

代码语言:javascript
复制
#!/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

获取方法概述

  1. 通过python实现一个webapi server
  2. 通过浏览器或者curl获取X86-PCServer属性
  3. 下一章我们实现webapi获取数据并存入数据库
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-04-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话IT架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档