VMware Vcenter_API
介绍
本文主要通过调用Vcenter_API,获取其中的数据中心,集群,主机,网络,存储,虚拟机信息。
开发语言 python
- 使用官方sdk pyvmomi
文档
- 模块:https://github.com/vmware/pyvmomi
- 例子:https://github.com/vmware/pyvmomi-community-samples
安装:
pip install pyvmomi
pip install pyVim
自己总结的调用API:
# -*- coding: utf-8 -*-
from pyVim import connect
from pyVmomi import vim
import json
class VcenterApi(object):
”“”
收集Vcenter中数据中心,主机集群,主机,网络,虚拟机,的信息
“”“
def __init__(self, host, user, pwd):
self.si = connect.ConnectNoSSL(host=host, user=user, pwd=pwd)
self.content = self.si.RetrieveContent()
datacenter = self.content.rootFolder.childEntity[0]
self.datacentername = datacenter.name
print(self.datacentername)
def get_cluster_list(self):
"""
获取所有机器资源使用情况
1。CPU
2。内存
3。磁盘
:return:
"""
# 获取集群视图
objview = self.content.viewManager.CreateContainerView(self.content.rootFolder,[vim.ComputeResource],True)
# 获取集群对象
clusters = objview.view
# 销毁视图
objview.Destroy()
redata = []
for cluster in clusters:
summary = cluster.summary
cpuusage = 0
memusage = 0
vmcount = 0
for host in cluster.host:
# print "主机已使用cpu", host.summary.quickStats.overallCpuUsage
# print "主机已使用内存", host.summary.quickStats.overallMemoryUsage
cpuusage += host.summary.quickStats.overallCpuUsage
memusage += host.summary.quickStats.overallMemoryUsage
vmcount += len(host.vm)
totaldatastore = 0
datastorefree = 0
for datastore in cluster.datastore:
totaldatastore += datastore.summary.capacity
datastorefree += datastore.summary.freeSpace
# print("---------------------------------")
# print "集群名称:", cluster.name
# print "集群状态:", summary.overallStatus
# print "总主机数:", summary.numHosts
# print "vm数量:", vmcount
# print "cpu颗数:", summary.numCpuCores
# print "总cpu:%.2f GHz" % (summary.totalCpu / 1000.0)
# print "已使用cpu: %.2f GHz" % (cpuusage / 1000.0)
# print "总内存:%.2f GB" % (summary.totalMemory / 1024 / 1024 / 1024.0)
# print "已使用mem: %.2f GB" % (memusage / 1024.0)
# print "总存储: %.2f T" % (totaldatastore / 1024 / 1024 / 1024 / 1024.0)
# print "可用存储: %.2f T" % (datastorefree / 1024 / 1024 / 1024 / 1024.0)
clusterdata = {
"clustername": cluster.name,
"overallstatus": summary.overallStatus,
"numhosts": summary.numHosts,
"numcpucores": summary.numCpuCores,
"cputotal": "%.2f GHz" % (summary.totalCpu / 1000.0),
"cpuusage": "%.2f GHz" % (cpuusage / 1000.0),
"memtotal": "%.2f GB" % (summary.totalMemory / 1024 / 1024 / 1024.0),
"memusage": "%.2f GB" % (memusage / 1024.0),
"totaldatastore": "%.2f T" % (totaldatastore / 1024 / 1024 / 1024 / 1024.0),
"datastorefree": "%.2f T" % (datastorefree / 1024 / 1024 / 1024 / 1024.0),
"vmcount": vmcount,
"datacentername": self.datacentername,
}
redata.append(clusterdata)
return redata
def print_vm_info(self, virtual_machine):
"""