python-获取EXSI宿主机上虚拟机资源信息
1. python-获取EXSI宿主机上虚拟机资源信息
-
安装包准备
# 所需安装包版本 python==3.8.4 pyVim==0.0.21 pyvmomi==6.7.1 # 安装命令: pip install pyVim==0.0.21 pyvmomi==6.7.1
-
编写获取
#!/usr/bin/python3 #coding:utf-8 """ 获取所有的vcenter相关信息 包括exsi的硬件资源信息和vmware客户端的硬件分配信息 """ from pyVmomi import vim from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL import atexit import argparse def get_args(): parser = argparse.ArgumentParser( description='Arguments for talking to vCenter') parser.add_argument('-s', '--host', required=True, action='store', help='vSpehre service to connect to') parser.add_argument('-o', '--port', type=int, default=443, action='store', help='Port to connect on') parser.add_argument('-u', '--user', required=True, action='store', help='User name to use') parser.add_argument('-p', '--password', required=True, action='store', help='Password to use') args = parser.parse_args() return args def get_obj(content, vimtype, name=None): ''' 列表返回,name 可以指定匹配的对象 ''' container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True) obj = [ view for view in container.view] return obj def main(): esxi_host = {} args = get_args() # connect this thing si = SmartConnectNoSSL( host=args.host, user=args.user, pwd=args.password, port=args.port) # disconnect this thing atexit.register(Disconnect, si) content = si.RetrieveContent() esxi_obj = get_obj(content, [vim.HostSystem]) for esxi in esxi_obj: esxi_host[esxi.name] = {'esxi_info':{},'datastore':{}, 'network': {}, 'vm': {}} esxi_host[esxi.name]['esxi_info']['厂商'] = esxi.summary.hardware.vendor esxi_host[esxi.name]['esxi_info']['型号'] = esxi.summary.hardware.model for i in esxi.summary.har