Use Python pyshpere to get vsphere VM CPU Month usage statistics

Source: Internet
Author: User
Tags memory usage cpu usage

Ref

Https://www.slideshare.net/alanrenouf/vsphere-apis-for-performance-monitoring
https://groups.google.com/ Forum/#!topic/pysphere/z1k8kkshtze
Https://groups.google.com/forum/#!topic/pysphere/jjUGi-0WqyE
HTTPS ://github.com/codekoala/pysphere/blob/master/pysphere/vi_performance_manager.py

To be continue

Every month, there are always two days, need to organize the monthly virtual machine running state, of which, the most unbearable is, from the Vsphere interface to each virtual machine CPU, MEM and other such data read out, really uncomfortable.

After a day of study and collation, the following code can be used to give Python, using the Pysphere module, the vcenter inside the virtual machine for CPU utilization and memory usage to obtain and print.

# coding:utf-8 ' virtual machine business CPU usage and memory usage gain, after getting the combination of Excel can be directly out of the icon.
The program runs at the end of each month. Written by:zzlyzq@126.com Create time:2018-02-27 "' Import pysphere host=" 172.16.10.1 "user=" ADMINISTRATOR@VSPH  Ere.local "password=" xxx "from Pysphere import viserver from pysphere.resources import vimservice_services as VI #Create An object to work with Server = Viserver () #Connect to the server import SSL Default_context = Ssl._create_default_https _context Try: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.G Et_server_type (), Server.get_api_version ()) #Get the list of all VMs (their VMX paths) in the vcenter this are powered on
. #vmlist = Server.get_registered_vms (status= ' Poweredon ') vmnamelist={} #Vmlist = Server.get_registered_vms (status= ' Poweredon ') vmlist = [] # xxx Vmlist.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 of the counters and their current values to 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 ':", ' Mem.vmmemctl ': ", ' Mem.overhead ': 102, ' mem.swapinrate ':", "Cpu.usage": 2, ' Mem.usag E ': ' Disk.maxtotallatency ': "mem.consumed": "The", "Disk.usage ':", "disk.used": 281, ' Cpu.usagemhz ': 6, ' Net.usag E ': 143, ' mem.swapoutrate ': sys.uptime, ' disk.provisioned ': 282, ' disk.unshared ': 284} ' #pr
        int 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.numberwriteaver  Aged '] #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 =c ountervalues[' 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 # Release to vcenter connection Server.disconnect ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.