Cloud, DevOps, Evangelism

Some Quick pySphere Code Demos

A couple requests came in over the internal EMC vSpecialist lists last week to demonstrate 2 things with the vSphere API.

The first was a request for a demonstration of how to extract individual VM performance disk performance data.  Here's a python script using the pysphere library to accomplish that:

# Import the pysphere library (makes python talk to vsphere)</p>

from pysphere import VIServer
from pysphere.resources import VimService_services as VI

#Create an object to work with
server = VIServer()

#Connect to the server
server.connect("IP.ad.dre.ss", "Administrator", "AReallyStrongPassword")

#Get the list of all VMs (their VMX paths) in the vCenter that are powered on.
vmlist = server.get_registered_vms(status='poweredOn')

#For each path....
for vmpath in vmlist:
#Get the current performance manager object (it changes, so we can't just instatiate it once)
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)
#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']
#print them out.
print "VM Name",vm.get_property('name')
print " IOPs",IOPs
print " Bandwidth(KB/s):",BandwidthKBs
print " Read Latency (ms):",ReadLatency
print " Write Latency (ms):",WriteLatency
print "-------"</h6>

The next was a request to show the datastore overprovisioning rate:

from __future__ import division
#Use Python 3.0's division (default to long division, not integer)
from pysphere import *
# Import the pysphere library (makes python talk to vsphere)
#Create an object to work with
server = VIServer()
#Connect to the server
server.connect("IP.addy.goes.here", "Administrator", "P@ssword1!")
for ds, name in server.get_datastores().items():
#Get all the datastores and their names.
props = VIProperty(server, ds)
#Create a object that contains all the properties
capacity = props.summary.capacity
free = props.summary.freeSpace
uncommitted = props.summary.uncommitted
#Per the API docs: "Total additional storage space, in bytes, potentially used by all virtual machines on this datastore. The server periodically updates this value."
used = capacity - free
overprov = used + uncommitted
#Combine whats currently used with what could be used.
overprovPercent = (overprov / capacity) * 100
print "Datastore", name
print " ","Capacity (MB) : ",capacity / 1024 / 1024
print " ","Free (MB) : ",free / 1024 / 1024
print " ","Used (MB) : ",used / 1024 / 1024
print " ","OverProvisioning (MB) : ",overprov / 1024 / 1024
print " ","OverProvisioning % : ",overprovPercent
except AttributeError:
#Not all datastores have this property (NFS, for example), so we put it in a try/catch to prevent errors.