use python pyshpere to get vsphere vm cpu month usage statistics

來源:互聯網
上載者:User

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()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.