01
PART
知识回顾
本篇文章,我们重点介绍通过webapi获取服务器熟悉并存入mysql数据库。
前期知识储备和回顾,请参考以下文章
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信息获取
09)运维天地-服务器09-X86-PCServer属性webapi接口实现
特别说明:本文的服务器特指X86-PCServer硬件服务器
02
PART
X86-PCServer属性webapi获取并入库
A
调用webapi和入库脚本
代码语言:javascript复制#!/usr/bin/python
#-*- coding: utf-8 -*-
#作者:王志国 电话:1891037 个人微信: it_beijing 个人微信公众号: 大话IT架构
#desc:欢迎关注:个人微信公众号---大话IT架构
#date: 2020.04.06
import requests
import ast
import pymysql
url = "http://192.144.231.34:10000"
result = requests.get(url)
hardware_info = ast.literal_eval(result.content.decode())
print(hardware_info)
if hardware_info['mechine_type'] == "vm":
mechine_type = hardware_info['mechine_type']
net_10g_num = hardware_info['net_10g_num']
net_1g_num = hardware_info['net_1g_num']
os_type = hardware_info['os_type']
os_detail = hardware_info['os_detail']
product_ip = hardware_info['product_ip']
values = (mechine_type, net_10g_num, net_1g_num,
os_type, os_detail, product_ip)
sql = """INSERT INTO vm (mechine_type, net_10g_num, net_1g_num, os_type, os_detail, product_ip)
VALUES (%s, %s, %s, %s, %s, %s)"""
else:
mechine_type = hardware_info['mechine_type']
manufacturer = hardware_info['manufacturer']
product_name = hardware_info['product_name']
serial_number = hardware_info['serial_number']
cpu_tpye = hardware_info['cpu_type']
cpu_num = hardware_info['cpu_num']
mem_ddr = hardware_info['mem_ddr']
mem_speed = hardware_info['mem_speed']
mem_size = hardware_info['mem_size']
mem_num = hardware_info['mem_sum']
mem_slot_num = hardware_info['mem_slot_sum']
raid_adapter = hardware_info['raid_adapter']
disk_type1 = hardware_info['disk_type1']
disk_type1_num = hardware_info['disk_type1_num']
disk_type2 = hardware_info['disk_type2']
disk_type2_num = hardware_info['disk_type2_num']
disk_type3 = hardware_info['disk_type3']
disk_type3_num = hardware_info['disk_type3_num']
net_10g_num = hardware_info['net_10g_num']
net_1g_num = hardware_info['net_1g_num']
os_type = hardware_info['os_type']
os_detail = hardware_info['os_detail']
product_ip = hardware_info['product_ip']
ipmi_ip = hardware_info['ipmi_ip']
values = (mechine_type, manufacturer, product_name, serial_number,
cpu_type, cpu_num, mem_ddr, mem_speed, mem_size, mem_num,
mem_slot_num, raid_adapter, disk_type1, disk_type1_num,
disk_type2, disk_type2_num, disk_type3, disk_type3_num,
net_10g_num, net_1g_num, os_type, os_detail, product_ip, ipmi_ip)
sql = """INSERT INTO server (mechine_type, manufacturer, product_name, serial_number,
cpu_type, cpu_num, mem_ddr, mem_speed, mem_size, mem_num,
mem_slot_num, raid_adapter, disk_type1, disk_type1_num,
disk_type2, disk_type2_num, disk_type3, disk_type3_num,
net_10g_num, net_1g_num, os_type, os_detail, product_ip, ipmi_ip)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
dbhost = '127.0.0.1'
dbuser = 'root'
dbpassword = '123456'
dbname = 'idc'
db = pymysql.connect(dbhost, dbuser, dbpassword, dbname, charset='utf8')
cursor = db.cursor()
try:
cursor.execute(sql, values)
db.commit()
except:
db.rollback()
B
数据库操作
数据库及建表sql语句
CREATE DATABASE IF NOT EXISTS idc DEFAULT CHARACTER SET utf8;
CREATE TABLE vm ( mechine_type VARCHAR(50), net_10g_num INT, net_1g_num INT, os_type VARCHAR(50), os_detail VARCHAR(100), product_ip VARCHAR(50) )
CREATE TABLE server ( mechine_type VARCHAR(50), high INT, manufacturer VARCHAR(50), product_name VARCHAR(50), serial_number VARCHAR(50) NOT NULL, cpu_type VARCHAR(100), cpu_num INT, mem_ddr VARCHAR(50), mem_speed VARCHAR(50), mem_size VARCHAR(50), mem_num INT, mem_slot_num INT, raid_adapter VARCHAR(50), disk_type1 VARCHAR(50), disk_type1_num INT, disk_type2 VARCHAR(50), disk_type2_num INT, disk_type3 VARCHAR(50), disk_type3_num INT, net_10g_num INT, net_1g_num INT, os_type VARCHAR(50), os_detail VARCHAR(100), product_ip VARCHAR(50), ipmi_ip VARCHAR(50),
asset_id INT, pur_contract_id INT, maint_contract_id INT, maint_start_time VARCHAR(50), maint_end_time VARCHAR(50), maint_company VARCHAR(50), maint_tel VARCHAR(50), pur_price FLOAT, pur_department VARCHAR(50), pur_name VARCHAR(50),
idc_supplier VARCHAR(50), idc_name VARCHAR(50), idc_row VARCHAR(50), rack VARCHAR(50), startU VARCHAR(50), status VARCHAR(50),
update_time DATE, comments VARCHAR(200) )DEFAULT CHARSET=utf8;