当前位置:网站首页>CMDB 腾讯云部分实现
CMDB 腾讯云部分实现
2022-08-04 05:33:00 【上海_运维_Q先生】
1. CMDB 腾讯云成品效果
最近由于上海疫情一直远程办公,路上通勤时间节约出来了可不能浪费.慢慢摸索做了个CMDB,从此告别用Excle管理服务器.
老年运维0基础入门Vue,自己也感觉到很多判断可能写的比较冗杂,慢慢改进吧.
先上效果图,一部分机器打了码.学渗透的小伙伴就别拿这个来练手了,网上机器多得是.请手下留情
2. 腾讯 云API
https://console.cloud.tencent.com/api/explorer
DescribeInstances 必要参数是Region,在页面上因为已经做过登录,所以不需要传AK,但通过SDK调用secret_id,secret_key就是必须的.
当发起调用后我们就能拿到了这样一个json的返回
因为我们传了InstanceIds,所以只返回了一个实例的内容.真实情况下不会单独查询,而是通过分页方式取到所有instance的清单,然后通过清单去遍历出每一个实例的配置.
{
"Response": {
"InstanceSet": [
{
"CPU": 8,
"CamRoleName": "CAM-xxxx-xxxxx",
"CreatedTime": "2020-10-26T02:50:33Z",
"DataDisks": [
{
"CdcId": null,
"DeleteWithInstance": true,
"DiskId": "disk-xxxxxxx",
"DiskSize": 500,
"DiskType": "CLOUD_PREMIUM",
"Encrypt": false,
"KmsKeyId": null,
"SnapshotId": null,
"ThroughputPerformance": 0
}
],
"DisasterRecoverGroupId": "ps-xxxxxx",
"ExpiredTime": "2022-10-26T02:50:33Z",
"GPUInfo": null,
"HpcClusterId": "",
"IPv6Addresses": null,
"ImageId": "img-xxxxx",
"InstanceChargeType": "PREPAID",
"InstanceId": "ins-xxxxxxx",
"InstanceName": "xxx-xxxxxxx-xxx",
"InstanceState": "RUNNING",
"InstanceType": "S5.2XLARGE32",
"InternetAccessible": {
"BandwidthPackageId": null,
"InternetChargeType": null,
"InternetMaxBandwidthOut": 0,
"PublicIpAssigned": null
},
"IsolatedSource": "NOTISOLATED",
"LatestOperation": "AssociateSecurityGroups",
"LatestOperationRequestId": "xxxxxxxxxxxxxxxxx",
"LatestOperationState": "SUCCESS",
"LicenseType": "TencentCloud",
"LoginSettings": {
"KeepImageLogin": null,
"KeyIds": null,
"Password": null
},
"Memory": 32,
"OsName": "CentOS 7.7 64位",
"Placement": {
"HostId": null,
"HostIds": null,
"HostIps": null,
"ProjectId": 1213517,
"Zone": "ap-shanghai-x"
},
"PrivateIpAddresses": [
"10.1xx.1xx.2"
],
"PublicIpAddresses": null,
"RdmaIpAddresses": null,
"RenewFlag": "NOTIFY_AND_AUTO_RENEW",
"RestrictState": "NORMAL",
"SecurityGroupIds": [
"sg-f6hr2yau",
"sg-b3s07de6",
"sg-dz364bxk"
],
"StopChargingMode": "NOT_APPLICABLE",
"SystemDisk": {
"CdcId": null,
"DiskId": "disk-xxxxxx",
"DiskSize": 50,
"DiskType": "CLOUD_PREMIUM"
},
"Tags": [
{
"Key": "OMO",
"Value": ""
}
],
"Uuid": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"VirtualPrivateCloud": {
"AsVpcGateway": false,
"Ipv6AddressCount": null,
"PrivateIpAddresses": null,
"SubnetId": "subnet-iintkmd1",
"VpcId": "vpc-xxxxxx"
}
}
],
"RequestId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"TotalCount": 1
}
}
可以看到,Json返回了一堆数据,我们只需要去删选出几个我们感兴趣的值,通过赋值的方式一一存入对应位置.
比如:Placement.Zone,CPU,Memory,DataDisks下的DiskSize等
3. 通过python将数据整理并更新
先通过FORM传入secret_id,secret_key,region等必要参数,通过查询,保存所有实例清单.
当然可要考虑到地域没有实例的可能性.
secret_id = request.data.get('secret_id')
secret_key = request.data.get('secret_key')
region_id = request.data.get('region')
instance_result = cloud.instance_list(region_id)
instance_list = []
if instance_result.code == 200:
instance_list = instance_result.InstanceSet
if instance_result.TotalCount == 0:
res = {
'code': 500, 'msg': '该区域未发现云主机,请重新选择!'}
return Response(res)
elif instance_result.code == 500:
res = {
'code': 500, 'msg': '%s' %instance_result['msg']}
return Response(res)
通过整理实例清单内容,将值一一保存更新.
for host in instance_list:
zone = host.Placement.Zone
instance_id = host.InstanceId # 实例ID
instance_name = host.InstanceName # 机器名称
os_version = host.OsName
private_ip = host.PrivateIpAddresses # 私网ip
public_ip = host.PublicIpAddresses # 公网IP
cpu = "%s核" %host.CPU # CPU核数
memory = "%sG" %host.Memory # 内存容量
size = host.SystemDisk.DiskSize # host.SystemDisk.DiskSize系统盘
data_list = host.DataDisks # host.DataDisks数据盘列表
if data_list:
for d in data_list:
size += d.DiskSize
disk = str(size) + "G"
create_date = time.strftime("%Y-%m-%d",time.strptime(host.CreatedTime, "%Y-%m-%dT%H:%M:%SZ"))
# 创建服务器
idc_name = zone_dict[zone]
idc = Idc.objects.get(name=idc_name)]
# 如果instance_id不存在才创建
data = {
'idc': idc,
'name': instance_name,
'hostname': instance_id,
'ssh_ip': private_ip[0],
'ssh_port': ssh_port,
'machine_type': 'cloud_vm',
'os_version': os_version,
'public_ip': public_ip,
'private_ip': private_ip,
'cpu_num': cpu,
'memory': memory,
'disk': disk,
'put_shelves_date': create_date,
# 'expire_datetime': expired_time,
'is_verified': 'verified'}
server = Server.objects.filter(hostname=instance_id)
if not server:
server = Server.objects.create(**data)
# 分组多对多
group = ServerGroup.objects.get(id=server_group_id) # 根据id查询分组
# 将服务器添加到分组
server.server_group.add(group)
else:
server.update(**data)
4. Vue实现展示
<el-table-column type="selection" width="55"/> <!--启用多选-->
<el-table-column prop="name" label="名称" width="130" fixed="left" sortable v-if="showColumn.name"/>
<el-table-column prop="hostname" label="主机名" width="130" sortable v-if="showColumn.hostname"/>
<el-table-column prop="ssh_ip" label="SSH IP" width="120" sortable v-if="showColumn.ssh_ip"/>
<el-table-column prop="idc" label="IDC机房" sortable v-if="showColumn.idc" width="130px">
<template #default="scope">
<img src="../../assets/aliyun.png" style="width: 18px;height: 18px;" v-if="scope.row.idc.provider == '阿里云'">
<img src="../../assets/tencent.png" style="width: 18px;height: 18px;" v-else-if="scope.row.idc.provider == '腾讯云'">
<el-icon :size="18" color="#409EFC" v-else><office-building /></el-icon>
{
{
scope.row.idc.city}}-{
{
scope.row.idc.name }}
</template>
</el-table-column>
<el-table-column prop="server_group" label="主机分组" width="70px" sortable v-if="showColumn.server_group">
<template #default="scope">
<span v-for="group in scope.row.server_group" :key="group.id">
<el-tag type="success" v-if="group.name == '正式'">{
{
group.name }}</el-tag>
<el-tag type="warning" v-else-if="group.name == '测试'">{
{
group.name }}</el-tag>
<el-tag type="primary" v-else-if="group.name == 'UAT'">{
{
group.name }}</el-tag>
<el-tag type="danger" v-else-if="group.name == '待下线'">{
{
group.name }}</el-tag>
<el-tag type="info" v-else-if="group.name == '已下线'">{
{
group.name }}</el-tag>
</span >
</template>
</el-table-column>
<el-table-column prop="machine_type" label="机器类型" width="80px" sortable v-if="showColumn.machine_type">
<template #default="scope">
<span v-if="scope.row.machine_type == 'vm'">虚拟机</span>
<span v-else-if="scope.row.machine_type == 'cloud_vm'">云主机</span>
<span v-else>物理机</span>
</template>
</el-table-column>
<el-table-column prop="os_version" label="系统版本" width="200px" sortable :show-overflow-tooltip="true" v-if="showColumn.os_version"/>
<el-table-column prop="private_ip" label="内网IP" width="140px" sortable v-if="showColumn.private_ip">
<template #default="scope">
<el-tag v-for="(ip,index) in scope.row.private_ip" :key="index">{
{
ip }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="public_ip" label="公网IP" width="140px" sortable v-if="showColumn.public_ip">
<template #default="scope">
<el-tag v-for="(ip,index) in scope.row.public_ip" :key="index">{
{
ip }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="cpu_num" label="CPU" width="80" sortable v-if="showColumn.cpu_num"/>
<el-table-column prop="cpu_model" label="CPU型号" sortable v-if="showColumn.cpu_model"/>
<el-table-column prop="memory" label="内存" width="80" sortable v-if="showColumn.memory"/>
<el-table-column prop="disk" label="硬盘" width="80" sortable v-if="showColumn.disk">
</el-table-column>
<el-table-column prop="put_shelves_date" label="上架日期" sortable v-if="showColumn.put_shelves_date"/>
<el-table-column prop="off_shelves_date" label="下架日期" sortable v-if="showColumn.off_shelves_date"/>
<el-table-column prop="expire_datetime" label="租约过期时间" sortable v-if="showColumn.expire_datetime"/>
<el-table-column prop="is_verified" label="SSH状态" width="120" sortable v-if="showColumn.is_verified">
<template #default="scope">
<el-tag type="success" v-if="scope.row.is_verified == 'verified'">已验证</el-tag>
<el-tag type="warning" v-else>未验证</el-tag>
</template>
</el-table-column>
<el-table-column prop="note" label="备注" v-if="showColumn.note"/>
<el-table-column prop="update_time" label="更新时间" sortable v-if="showColumn.update_time"/>
<el-table-column prop="create_time" label="创建时间" sortable v-if="showColumn.create_time"/>
5. axios部分
methods: {
getData() {
.....
this.$axios.get('/cmdb/server/',{
params: this.urlParams})
.then(res => {
// console.log(res.data);
this.tableData = res.data.data;
this.total =res.data.count;
})
},
边栏推荐
猜你喜欢
随机推荐
枚举和联合(自定义类型)-C语言
键盘扫描码
桌面右键的NVIDIA去除与恢复
Unity Day01
用chrome dev tools 强制js注入
自学网络安全你为什么一学就废?
Uos统信系统 Postfix-smtps & Dovecot-imaps
An abstract class, internal classes and interfaces
网络安全求职指南
益智小游戏- 算算总共多少正方形
基于Webrtc和Janus的多人视频会议系统开发6 - 从Janus服务器订阅媒体流
第九篇 ApplicationContext初始化
网络安全工程师们改不掉的“老毛病”
动态内存管理-C语言
结构体传参-C语言
Multi-threaded sequential output
并发概念基础:线程安全与线程间通信
冰歇webshell初探
基于语音识别的QT设计的csgo互动类视频游戏
Design and implementation of legal aid platform based on asp.net (with project link)