CMDB运维自动化

最近用flask写了一个CMDB资产管理工具,结合云平台做到自动化更新以及自动适配报警模版。这里只提供部分代码以及截图

CMDB运维自动化_第1张图片CMDB运维自动化_第2张图片

这里是放置自动更新资产的模块,当有新的资源增加的时候,定时器会定期拉取线上资源并录入数据库,同时通过线上监控API加入监控,完全不用人工参与附属部分代码:

def auto_update_rds():
result = []
for zone in zones:
#获取rds所有InstanceId
nums = get_rds_totalcount(zone)
if nums > 0:
for i in range(nums):
lists = get_rds_instanceid(zone, i+1)
for line in lists:
date = (
line.get('DBInstanceId')
)
result.append(date)

#获取数据库下所有InstanceId
db_ins = ModifyDatabase()
rds_id = db_ins.get_mysql_ecs_id('ali_rds')
rds_lists = []
for i in range(len(rds_id)):
rds_lists.append(rds_id[i][0])
i = i + 1
diff1 = list(set(result).difference(set(rds_lists)))
#获取未录入数据库的InstanceId
print ('未录入的rds数量:' + str(len(diff1)))

if len(diff1) > 0:
#封装好的监控接口
cms = auto_add_cms()
type = "RDS"
for L in range(len(diff1)):
#自动录入数据库
rds_info = get_rds_info('cn-hangzhou',  diff1[L])
db_ins = ModifyDatabase()
db_ins.insert_rds_table(**rds_info)
#自动加入监控,根据不同的描述加入到不同的监控组
if 'prod' in rds_info['name'].lower():
GroupId = xxxxx
cms_list = cms.get_all_cms_list(GroupId)
#判断监控中是否存在相同InstanceId
if rds_info['InstanceId'] not in cms_list:
zone = rds_info['region']
InstanceId = rds_info['InstanceId']
cms.add_cms(zone, GroupId, InstanceId, type)
else:
GroupId = xxxxx
cms_list = cms.get_all_cms_list(GroupId)
if rds_info['InstanceId'] not in cms_list:
zone = rds_info['region']
InstanceId = rds_info['InstanceId']
cms.add_cms(zone, GroupId, InstanceId, type)

diff2 = list(set(rds_lists).difference(set(result)))
# 自动删除功能
if len(diff2) > 0:
print('rds过期数量:' + str(len(diff2)))
db_ins.delete_resource_info("ali_rds", "instance_id", ",".join(diff2))
#定时器
global timer
timer = threading.Timer(86400, auto_update_rds)
timer.start()

下面介绍路由信息(这里主要分享:返回给前端的数据接口):
@app.route("/getresource", methods=["GET"])
env = request.args["Env"]
res = request.args["Res"]
app.logger.debug("[getresource]In {} for resource {}".format(env, res))
db_ins = ModifyDatabase()
data = []
if env == "Ali":
if res == "Ecs":
infos = db_ins.get_product_info("ali_vm")
data = formatAliEcs(infos)
elif res == "Rds":
infos = db_ins.get_product_info("ali_rds")
data = formatAliRds(infos)
elif res == "Slb":
infos = db_ins.get_product_info("ali_slb")
data = formatAliSlb(infos)
elif res == "Mongo":
infos = db_ins.get_product_info("ali_mongo")
data = formatAliMongo(infos)
elif res == "Redis":
infos = db_ins.get_product_info("ali_redis")
data = formatAliRedis(infos)
elif res == "Memcache":
infos = db_ins.get_product_info("ali_memcache")
data = formatAliMemcache(infos)
elif res == "Cdn":
infos = db_ins.get_product_info("ali_cdn")
data = formatAliCdn(infos)
elif env == "Inner":
if res == "Ecs":
infos = db_ins.get_product_info("inner_vm")
data = formatInnerVm(infos)
elif res == "Pm":
infos = db_ins.get_product_info("innerserver")
data = formatInnerPm(infos)
elif res == "Switch":
infos = db_ins.get_product_info("innerswitch")
data = formatInnerSwitch(infos)
elif res == "Domain":
infos = db_ins.get_product_info("domain")
data = formatInnerDomain(infos)
r = json.dumps(data)
return Response(
response=r,
mimetype="application/json",
status=200

)

前端传过来两个参数分别对应是内部资源还是云平台的资源以及资源类型,然后从数据库获取资源、格式化成json返回给前端展示

效果:

CMDB运维自动化_第3张图片

https://fgk.pw/i/v0NCUpl2039

你可能感兴趣的:(CMDB运维自动化)