I am writing this script to pull performance data from vcenter using python. If a counter does not exists for a guest, the script exists/breaks.
How would I check first that if the counter exists for a vm than assign the value:
Here is the script:
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
    if InitMyVariable.my_variable is None:
    cpu_usage=counterValues['cpu.usage']
    cpu_ready=counterValues['cpu.ready']
    total_memory=counterValues['mem.granted']
    memory_used=counterValues['mem.consumed']
    Memory_usage=counterValues['mem.usage']
    Memory_ballooned=counterValues['mem.vmmemctl']
    Memory_swapped=counterValues['mem.swapped']
    ReadLatency = counterValues['virtualDisk.totalReadLatency']
    WriteLatency = counterValues['virtualDisk.totalWriteLatency']
    #print them out.
    print "VM Name",vm.get_property('name')
    print "% CPU",cpu_usage
    print "CPU Ready",cpu_ready
    print "Total Memory",memory_used
    print "% Memory Used",Memory_usage
    print "Memory Ballooned",Memory_ballooned
    print "Memory Swapped",Memory_swapped
    print "Disk Read Latency",ReadLatency
    print "Disk Write Latency",WriteLatency
    print "——-"
server.disconnect()
This is the error:
Traceback (most recent call last):
  File "guest_perf.py", line 38, in <module>
    ReadLatency = counterValues['virtualDisk.totalReadLatency']
KeyError: 'virtualDisk.totalReadLatency'
 
     
     
    