本文共 5447 字,大约阅读时间需要 18 分钟。
内存,CPU
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL import atexit import argparsedef 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):
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True) obj = [ view for view in container.view] return objdef 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.hardware.otherIdentifyingInfo: if isinstance(i, vim.host.SystemIdentificationInfo): esxi_host[esxi.name]['esxi_info']['SN'] = i.identifierValue esxi_host[esxi.name]['esxi_info']['处理器'] = '数量:%s 核数:%s 线程数:%s 频率:%s(%s) ' % (esxi.summary.hardware.numCpuPkgs, esxi.summary.hardware.numCpuCores, esxi.summary.hardware.numCpuThreads, esxi.summary.hardware.cpuMhz, esxi.summary.hardware.cpuModel) esxi_host[esxi.name]['esxi_info']['处理器使用率'] = '%.3f%%' % (float(esxi.summary.quickStats.overallCpuUsage) / (float(esxi.summary.hardware.numCpuPkgs * esxi.summary.hardware.numCpuCores * esxi.summary.hardware.cpuMhz)) * 100) esxi_host[esxi.name]['esxi_info']['内存(MB)'] = esxi.summary.hardware.memorySize/1024/1024 esxi_host[esxi.name]['esxi_info']['可用内存(MB)'] = '%.1f MB' % ((esxi.summary.hardware.memorySize/1024/1024) - esxi.summary.quickStats.overallMemoryUsage) esxi_host[esxi.name]['esxi_info']['内存使用率'] = '%.3f%%' % ((float(esxi.summary.quickStats.overallMemoryUsage)/ (float(esxi.summary.hardware.memorySize/1024/1024))) *100) esxi_host[esxi.name]['esxi_info']['系统'] = esxi.summary.config.product.fullName
f = open(args.host + '.txt', 'w')for host in esxi_host: print('ESXI IP:', host) f.write('ESXI IP: %s \n' % host) for hd in esxi_host[host]['esxi_info']: print(' %s: %s' % (hd, esxi_host[host]['esxi_info'][hd])) f.write(' %s: %s' % (hd, esxi_host[host]['esxi_info'][hd]))
f.close()
if name == 'main':
main()转载地址:http://taaql.baihongyu.com/