ref
https://www.slideshare.net/alanrenouf/vsphere-apis-for-performance-monitoringhttps://groups.google.com/forum/#!topic/pysphere/Z1K8KKshTZEhttps://groups.google.com/forum/#!topic/pysphere/jjUGi-0WqyEhttps://github.com/codekoala/pysphere/blob/master/pysphere/vi_performance_manager.py
to be continue
每個月,總有那麼兩天,需要去整理月度虛擬機器運行狀態,其中,最讓人受不了的是,從vsphere介面裡面把每個虛擬機器的cpu、mem等這些資料讀出來,確實難受。
經過一天的學習和整理,有如下代碼,可以用來給予python的、使用pysphere模組,對vcenter裡面的虛擬機器進行cpu使用率和記憶體使用量率進行擷取並列印。
# coding:utf-8'''虛擬機器業務cpu使用率和記憶體使用量率擷取,擷取後,結合excel可以直接出表徵圖。該程式每個月底運行一次。Written By : zzlyzq@126.comCreate Time: 2018-02-27''''''import pysphereHOST="172.16.10.1"USER="administrator@vsphere.local"PASSWORD="xxx"from pysphere import VIServerfrom pysphere.resources import VimService_services as VI#Create an object to work withserver = VIServer()#Connect to the serverimport ssldefault_context = ssl._create_default_https_contexttry: ssl._create_default_https_context = ssl._create_unverified_context server.connect(HOST,USER,PASSWORD) print "Connected to {} {}".format(server.get_server_type(), server.get_api_version())finally: #ssl._create_default_https_context = default_context print "vserver: AFTER CONNECTION - [{}] [{}]".format(server.get_server_type(), server.get_api_version())#Get the list of all VMs (their VMX paths) in the vCenter that are powered on.#vmlist = server.get_registered_vms(status='poweredOn')vmNameList={}#vmlist = server.get_registered_vms(status='poweredOn')vmlist = []# xxxvmlist.append('[10K] RCHB-W2012-01/RCHB-W2012-01.vmx')vmlist.append('[10K] RCHB-W2012-02/RCHB-W2012-02.vmx')'''vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()vmlist.append()'''for vmpath in vmlist: pm = server.get_performance_manager() #Get an actual VM object from the path vm = server.get_vm_by_path(vmpath) #Get the managed object reference for the VM, because the performance manager only accepts MoRefs mor = vm._mor #Get all the counters and their current values for that VM. #counterValues = pm.get_entity_counters(mor,interval=3) counterValues = pm.get_entity_statistic(mor,['cpu.usage', 'mem.usage','disk.usage','disk.used'],interval=3) #print counterValues ''' {'cpu.ready': 12, 'mem.vmmemctl': 90, 'mem.overhead': 102, 'mem.swapinRate': 85, 'cpu.usage': 2, 'mem.usage': 24, 'disk.maxTotalLatency': 133, 'mem.consumed': 98, 'disk.usage': 125, 'disk.used': 281, 'cpu.usagemhz': 6, 'net.usage': 143, 'mem.swapoutRate': 86, 'sys.uptime': 155, 'disk.provisioned': 282, 'disk.unshared': 284} ''' #print counterValues #Do some quick math on the values. #They come to us in a convienent dictionary form. #Values are descrobed here: http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/virtual_disk_counters.html #IOPs = counterValues['virtualDisk.numberReadAveraged'] + counterValues['virtualDisk.numberWriteAveraged'] #BandwidthKBs = counterValues['virtualDisk.read'] + counterValues['virtualDisk.write'] #ReadLatency = counterValues['virtualDisk.totalReadLatency'] #WriteLatency = counterValues['virtualDisk.totalWriteLatency'] #CpuReady=counterValues['cpu.ready'] #CpuUsage=counterValues['cpu.usage'] #MemUsage=counterValues['mem.usage'] #MemCtl=counterValues['mem.vmmemctl'] #MemSwapIn=counterValues['mem.swapin'] #MemSwapOut=counterValues['mem.swapout'] #print them out. #print vm.get_property('name'),IOPs,BandwidthKBs,ReadLatency,WriteLatency,CpuReady,CpuUsage,MemUsage,MemCtl,MemSwapIn,MemSwapOut cpuUsage=int(counterValues[0].value)/100+1 memUsage=int(counterValues[1].value)/100+1 print vm.get_property('name'),cpuUsage,memUsage# 釋放到vcenter的串連server.disconnect()