通过调用Vcenter-Api获取Vcenter中服务器信息
文章目录
- 通过调用Vcenter-Api获取Vcenter中服务器信息
- 1. 获取Vmware API帮助文档
- 2. 获取访问凭证
- 3. 获取服务器清单
- 4. 获取服务器更多信息
- 5. 获取虚机更多信息
- 6. 获取磁盘信息
- 7. 获取操作系统相关
1. 获取Vmware API帮助文档
https://developer.vmware.com/apis/vsphere-automation/latest/
2. 获取访问凭证
文档对应url:https://developer.vmware.com/apis/vsphere-automation/latest/cis/api/session/post/
注意这里获取凭证应该是使用post方式
将调用地址复制出来:https://{api_host}/api/session
将{api_host}替换成我们实际Vcenter地址192.168.0.200
替换后就成了https://192.168.0.200/api/session
方法设置为POST
3. 获取服务器清单
url替换成https://192.168.0.200/api/vcenter/vm
方法改为GET
Headers中key为vmware-api-session-id
value为我们刚才拿到的token
这样我们就拿到了这个Vc上的服务器清单
4. 获取服务器更多信息
以下内容是通过https://192.168.0.200/api/vcenter/vm查询到的某一个服务器的信息
{
"memory_size_MiB": 8192,
"vm": "vm-12311",
"name": "TEST-WEAPP",
"power_state": "POWERED_ON",
"cpu_count": 2
},
key | 含义 |
---|---|
memory_size_MiB | 虚拟机总内存 |
vm | 虚拟机ID(这个后续会用到) |
name | 虚拟机在Vcenter中的名字 |
power_state | 电源状态 |
cpu_count | 虚拟机CPU个数 |
5. 获取虚机更多信息
方法GET,URL https://192.168.0.200/api/vcenter/vm/vm-12311
以下是返回的部分内容
{
"instant_clone_frozen": false,
"cdroms": {
"16000": {
"start_connected": false,
"backing": {
"iso_file": "[back01] ISO/Windows Server/2016_64.ISO",
"type": "ISO_FILE"
},
"allow_guest_control": true,
"label": "CD/DVD drive 1",
"state": "CONNECTED",
"type": "SATA",
"sata": {
"bus": 0,
"unit": 0
}
}
},
"memory": {
"hot_add_increment_size_MiB": 4,
"size_MiB": 8192,
"hot_add_enabled": true,
"hot_add_limit_MiB": 131072
},
略
"cpu": {
"hot_remove_enabled": false,
"count": 2,
"hot_add_enabled": true,
"cores_per_socket": 1
},
"scsi_adapters": {
"1000": {
"pci_slot_number": 160,
"scsi": {
"bus": 0,
"unit": 7
},
"label": "SCSI controller 0",
"sharing": "NONE",
"type": "LSILOGICSAS"
}
},
"power_state": "POWERED_ON",
"floppies": {},
"disks": {
"2000": {
"scsi": {
"bus": 0,
"unit": 0
},
"backing": {
"vmdk_file": "[data01] TEST-WEAPP/TEST-WEAPP-000001.vmdk",
"type": "VMDK_FILE"
},
"label": "Hard disk 1",
"type": "SCSI",
"capacity": 53687091200
},
"2001": {
"scsi": {
"bus": 0,
"unit": 1
},
"backing": {
"vmdk_file": "[data01] TEST-WEAPP/TEST-WEAPP_1-000001.vmdk",
"type": "VMDK_FILE"
},
"label": "Hard disk 2",
"type": "SCSI",
"capacity": 107374182400
}
},
"nvme_adapters": {},
"name": "TEST-WEAPP",
网卡信息,略
"boot": {
"delay": 0,
"retry_delay": 10000,
"enter_setup_mode": false,
"type": "BIOS",
"retry": false
},
"serial_ports": {},
"boot_devices": [],
"guest_OS": "WINDOWS_9_SERVER_64",
"hardware": {
"upgrade_policy": "NEVER",
"upgrade_status": "NONE",
"version": "VMX_19"
}
}
我们可以在这段json中拿到大部分关于这台服务器的信息,比如:
KEY | 含义 |
---|---|
cdroms | 关于虚拟光驱的内容,可以看到我们这里用了ISO |
memory | 内存详细信息 |
cpu | cpu相关信息 |
nics | 网卡的一些信息,这里我隐藏掉了 |
disks | 磁盘的一些信息 |
6. 获取磁盘信息
这里我们拿到了部分磁盘的信息,磁盘的总大小,还有存储位置
但和我们实际想要的内容是有差距的
"disks": {
"2000": {
"scsi": {
"bus": 0,
"unit": 0
},
"backing": {
"vmdk_file": "[data01] TEST-WEAPP/TEST-WEAPP-000001.vmdk",
"type": "VMDK_FILE"
},
"label": "Hard disk 1",
"type": "SCSI",
"capacity": 53687091200
},
"2001": {
"scsi": {
"bus": 0,
"unit": 1
},
"backing": {
"vmdk_file": "[data01] TEST-WEAPP/TEST-WEAPP_1-000001.vmdk",
"type": "VMDK_FILE"
},
"label": "Hard disk 2",
"type": "SCSI",
"capacity": 107374182400
}
},
我们更像要的内容是,盘符,分区格式,磁盘总大小,剩余空间这些信息.
我们可以用GET方式取https://192.168.0.200/api/vcenter/vm/vm-12311/guest/local-filesystem
{
"C:\\": {
"mappings": [
{
"disk": "2000"
}
],
"free_space": 16884203520,
"filesystem": "NTFS",
"capacity": 53159653376
},
"D:\\": {
"mappings": [
{
"disk": "2001"
}
],
"free_space": 60841975808,
"filesystem": "NTFS",
"capacity": 107371032576
}
}
KEY | 含义 |
---|---|
free_space | 可用空间KB |
filesystem | 文件系统格式 |
capacity | 分区总大小KB |
C:\或D:\ | Windows盘符 |
/home或则/data | linux盘符 |
现在我们可以很简单的通过遍历json进行简单的加工就能获取到我们想要的具体数据
7. 获取操作系统相关
get访问https://192.168.0.200/api/vcenter/vm/vm-12311/guest/identity
{
"full_name": {
"args": [],
"default_message": "Microsoft Windows Server 2016 (64-bit)",
"id": "vmsg.guestos.windows444rver64Guest.label"
},
"name": "WINDOWS_9_SERVER_64",
"ip_address": "192.168.2.25",
"family": "WINDOWS",
"host_name": "TEST-WEAPP"
}
KEY | 含义 |
---|---|
default_message | 操作系统版本 |
ip_address | 服务器ip地址 |
family | 操作系统类型 |
host_name | 服务器名 |
至此已经通过调用Vcenter-Api获取到了大部分Vcenter中服务器信息
后续完善到之前CMDB中去.总的来说Vcenter的信息需要多次从API中取出,会比云服务器稍微麻烦一点点