我正在尝试从每个vms的vcenter中提取性能数据。我有以下脚本。当我尝试打印每个客人的值时,我得到每个vms的相同值。我做错了什么想法?
# coding: utf-8
from pysphere import VIServer
from pysphere.resources import VimService_services as VI
HOST="vcenter1.example.com"
USER="user_id"
PASSWORD="passwd123"
#Create an object to work with
server = VIServer()
#Connect to the server
server.connect(HOST,USER,PASSWORD)
#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
if "cpu.usage" in counterValues:
Cpu_usage=counterValues['cpu.usage']
else:
pass
if "cpu.ready" in counterValues:
Cpu_ready=counterValues['cpu.ready']
else:
pass
if "mem.granted" in counterValues:
Memory_total=counterValues['mem.granted']
else:
pass
if "mem.consumed" in counterValues:
Memory_used=counterValues['mem.consumed']
else:
pass
if "mem.usage" in counterValues:
Memory_usage=counterValues['mem.usage']
else:
pass
if "mem.vmmemctl" in counterValues:
Memory_ballooned=counterValues['mem.vmmemctl']
else:
pass
if "mem.swapped" in counterValues:
Memory_swapped=counterValues['mem.swapped']
else:
pass
if "virtualDisk.totalReadLatency" in counterValues:
Disk_read_latency=counterValues['virtualDisk.totalReadLatency']
else:
pass
if "virtualDisk.totalWriteLatency" in counterValues:
Disk_write_latency=counterValues['virtualDisk.totalWriteLatency']
else:
pass
#print them out.
print "VM Name",vm.get_property('name')
print "% CPU",Cpu_usage
print "CPU Ready",Cpu_ready
print "Total Memory",Memory_total
print "Total Used",Memory_used
print "% Memory Used",Memory_usage
print "Memory Ballooned",Memory_ballooned
print "Memory Swapped",Memory_swapped
print "Disk Read Latency",Disk_read_latency
print "Disk Write Latency",Disk_write_latency
print "——-"
server.disconnect()
这是我运行此脚本时得到的结果:
VM Name Test123
% CPU 2
CPU Ready 12
Total Memory 29
Total Used 98
% Memory Used 24
Memory Ballooned 90
Memory Swapped 70
Disk Read Latency 175
Disk Write Latency 176
——-
VM Name guest1
% CPU 2
CPU Ready 12
Total Memory 29
Total Used 98
% Memory Used 24
Memory Ballooned 90
Memory Swapped 70
Disk Read Latency 175
Disk Write Latency 176
——-
VM Name guest1
% CPU 2
CPU Ready 12
Total Memory 29
Total Used 98
% Memory Used 24
Memory Ballooned 90
Memory Swapped 70
Disk Read Latency 175
Disk Write Latency 176
——-
VM Name guest22
% CPU 2
CPU Ready 12
Total Memory 29
Total Used 98
% Memory Used 24
Memory Ballooned 90
Memory Swapped 70
Disk Read Latency 175
Disk Write Latency 176
——-
由于它为每个vm返回相同的数据,我继续打印出计数器值:
server = VIServer()
#Connect to the server
server.connect(HOST,USER,PASSWORD)
#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)
print counterValues
server.disconnect()
每个vms的性能值似乎相同:
{'mem.latency': 362, 'virtualDisk.write': 174, 'virtualDisk.numberReadAveraged': 171, 'mem.vmmemctltarget': 94, 'mem.compressionRate': 106, 'cpu.wait': 11, 'mem.overheadTouched': 368, 'mem.llSwapOutRate': 367, 'mem.active': 33, 'mem.vmmemctl': 90, 'mem.shared': 37, 'mem.zero': 41, 'rescpu.runpk1': 441, 'rescpu.actpk5': 436, 'disk.maxTotalLatency': 133, 'rescpu.runpk5': 443, 'rescpu.actpk1': 433, 'virtualDisk.readOIO': 313, 'sys.uptime': 155, 'mem.llSwapUsed': 369, 'virtualDisk.largeSeeks': 463, 'rescpu.runav15': 440, 'cpu.ready': 12, 'rescpu.actav1': 432, 'rescpu.runpk15': 445, 'mem.swapinRate': 85, 'power.power': 157, 'mem.usage': 24, 'mem.compressed': 105, 'rescpu.maxLimited15': 446, 'cpu.maxlimited': 342, 'mem.granted': 29, 'rescpu.runav5': 437, 'virtualDisk.numberWriteAveraged': 172, 'virtualDisk.writeLatencyUS': 465, 'cpu.run': 344, 'rescpu.sampleCount': 447, 'cpu.latency': 338, 'mem.swapped': 70, 'virtualDisk.readLatencyUS': 464, 'cpu.costop': 341, 'sys.heartbeat': 405, 'virtualDisk.totalWriteLatency': 176, 'rescpu.actav15': 438, 'rescpu.runav1': 434, 'datastore.maxTotalLatency': 184, 'rescpu.actav5': 435, 'cpu.demand': 340, 'mem.zipped': 360, 'mem.decompressionRate': 107, 'virtualDisk.totalReadLatency': 175, 'mem.zipSaved': 361, 'virtualDisk.writeIOSize': 460, 'virtualDisk.mediumSeeks': 462, 'mem.overheadMax': 358, 'mem.swapout': 350, 'mem.entitlement': 363, 'rescpu.samplePeriod': 448, 'virtualDisk.readIOSize': 459, 'cpu.idle': 13, 'virtualDisk.writeLoadMetric': 316, 'mem.consumed': 98, 'mem.overhead': 102, 'cpu.entitlement': 339, 'cpu.swapwait': 328, 'mem.activewrite': 357, 'mem.swapin': 346, 'power.energy': 159, 'virtualDisk.writeOIO': 314, 'cpu.system': 10, 'rescpu.maxLimited1': 442, 'cpu.usagemhz': 6, 'cpu.usage': 2, 'virtualDisk.smallSeeks': 461, 'mem.swaptarget': 74, 'rescpu.actpk15': 439, 'mem.swapoutRate': 86, 'virtualDisk.read': 173, 'rescpu.maxLimited5': 444, 'virtualDisk.readLoadMetric': 315, 'cpu.used': 14, 'sys.osUptime': 430, 'cpu.overlap': 343, 'mem.llSwapInRate': 366}
{'net.droppedRx': 394, 'mem.llSwapUsed': 369, 'rescpu.runav15': 440, 'cpu.usage': 2, 'mem.usage': 24, 'rescpu.maxLimited15': 446, 'rescpu.sampleCount': 447, 'cpu.costop': 341, 'datastore.numberWriteAveraged': 179, 'mem.swapout': 350, 'rescpu.samplePeriod': 448, 'cpu.idle': 13, 'mem.consumed': 98, 'net.broadcastTx': 399, 'net.droppedTx': 395, 'datastore.totalWriteLatency': 183, 'mem.swaptarget': 74, 'rescpu.actpk15': 439, 'cpu.overlap': 343, 'mem.vmmemctl': 90, 'net.packetsTx': 147, 'mem.overheadTouched': 368, 'mem.shared': 37, 'virtualDisk.readOIO': 313, 'rescpu.actpk5': 436, 'mem.compressionRate': 106, 'rescpu.actpk1': 433, 'net.bytesTx': 397, 'net.multicastTx': 401, 'sys.heartbeat': 405, 'cpu.ready': 12, 'net.usage': 143, 'rescpu.actav1': 432, 'rescpu.runpk15': 445, 'mem.swapinRate': 85, 'mem.compressed': 105, 'mem.vmmemctltarget': 94, 'net.received': 148, 'cpu.maxlimited': 342, 'virtualDisk.writeLatencyUS': 465, 'mem.swapped': 70, 'datastore.totalReadLatency': 182, 'net.broadcastRx': 398, 'mem.decompressionRate': 107, 'mem.llSwapInRate': 366, 'datastore.write': 181, 'cpu.swapwait': 328, 'rescpu.maxLimited1': 442, 'virtualDisk.smallSeeks': 461, 'cpu.demand': 340, 'mem.latency': 362, 'virtualDisk.numberReadAveraged': 171, 'cpu.wait': 11, 'mem.llSwapOutRate': 367, 'mem.entitlement': 363, 'net.multicastRx': 400, 'sys.uptime': 155, 'net.bytesRx': 396, 'mem.granted': 29, 'cpu.latency': 338, 'datastore.numberReadAveraged': 178, 'net.transmitted': 149, 'rescpu.runav5': 437, 'virtualDisk.totalWriteLatency': 176, 'rescpu.runav1': 434, 'virtualDisk.numberWriteAveraged': 172, 'mem.zipSaved': 361, 'virtualDisk.largeSeeks': 463, 'net.packetsRx': 146, 'virtualDisk.writeLoadMetric': 316, 'virtualDisk.mediumSeeks': 462, 'power.energy': 159, 'cpu.usagemhz': 6, 'virtualDisk.read': 173, 'rescpu.maxLimited5': 444, 'virtualDisk.readLoadMetric': 315, 'cpu.used': 14, 'sys.osUptime': 430, 'virtualDisk.write': 174, 'disk.maxTotalLatency': 133, 'datastore.read': 180, 'datastore.maxTotalLatency': 184, 'mem.zero': 41, 'rescpu.runpk1': 441, 'rescpu.runpk5': 443, 'virtualDisk.readLatencyUS': 464, 'power.power': 157, 'cpu.run': 344, 'virtualDisk.writeIOSize': 460, 'rescpu.actav15': 438, 'rescpu.actav5': 435, 'mem.zipped': 360, 'virtualDisk.totalReadLatency': 175, 'mem.overheadMax': 358, 'mem.active': 33, 'virtualDisk.readIOSize': 459, 'mem.overhead': 102, 'cpu.entitlement': 339, 'mem.activewrite': 357, 'mem.swapin': 346, 'virtualDisk.writeOIO': 314, 'cpu.system': 10, 'mem.swapoutRate': 86}
{'net.droppedRx': 394, 'mem.llSwapUsed': 369, 'rescpu.runav15': 440, 'cpu.usage': 2, 'mem.usage': 24, 'rescpu.maxLimited15': 446, 'rescpu.sampleCount': 447, 'cpu.costop': 341, 'datastore.numberWriteAveraged': 179, 'mem.swapout': 350, 'rescpu.samplePeriod': 448, 'cpu.idle': 13, 'mem.consumed': 98, 'net.broadcastTx': 399, 'net.droppedTx': 395, 'datastore.totalWriteLatency': 183, 'mem.swaptarget': 74, 'rescpu.actpk15': 439, 'cpu.overlap': 343, 'mem.vmmemctl': 90, 'net.packetsTx': 147, 'mem.overheadTouched': 368, 'mem.shared': 37, 'virtualDisk.readOIO': 313, 'rescpu.actpk5': 436, 'mem.compressionRate': 106, 'rescpu.actpk1': 433, 'net.bytesTx': 397, 'net.multicastTx': 401, 'sys.heartbeat': 405, 'cpu.ready': 12, 'net.usage': 143, 'rescpu.actav1': 432, 'rescpu.runpk15': 445, 'mem.swapinRate': 85, 'mem.compressed': 105, 'mem.vmmemctltarget': 94, 'net.received': 148, 'cpu.maxlimited': 342, 'virtualDisk.writeLatencyUS': 465, 'mem.swapped': 70, 'datastore.totalReadLatency': 182, 'net.broadcastRx': 398, 'mem.decompressionRate': 107, 'mem.llSwapInRate': 366, 'datastore.write': 181, 'cpu.swapwait': 328, 'rescpu.maxLimited1': 442, 'virtualDisk.smallSeeks': 461, 'cpu.demand': 340, 'mem.latency': 362, 'virtualDisk.numberReadAveraged': 171, 'cpu.wait': 11, 'mem.llSwapOutRate': 367, 'mem.entitlement': 363, 'net.multicastRx': 400, 'sys.uptime': 155, 'net.bytesRx': 396, 'mem.granted': 29, 'cpu.latency': 338, 'datastore.numberReadAveraged': 178, 'net.transmitted': 149, 'rescpu.runav5': 437, 'virtualDisk.totalWriteLatency': 176, 'rescpu.runav1': 434, 'virtualDisk.numberWriteAveraged': 172, 'mem.zipSaved': 361, 'virtualDisk.largeSeeks': 463, 'net.packetsRx': 146, 'virtualDisk.writeLoadMetric': 316, 'virtualDisk.mediumSeeks': 462, 'power.energy': 159, 'cpu.usagemhz': 6, 'virtualDisk.read': 173, 'rescpu.maxLimited5': 444, 'virtualDisk.readLoadMetric': 315, 'cpu.used': 14, 'sys.osUptime': 430, 'virtualDisk.write': 174, 'disk.maxTotalLatency': 133, 'datastore.read': 180, 'datastore.maxTotalLatency': 184, 'mem.zero': 41, 'rescpu.runpk1': 441, 'rescpu.runpk5': 443, 'virtualDisk.readLatencyUS': 464, 'power.power': 157, 'cpu.run': 344, 'virtualDisk.writeIOSize': 460, 'rescpu.actav15': 438, 'rescpu.actav5': 435, 'mem.zipped': 360, 'virtualDisk.totalReadLatency': 175, 'mem.overheadMax': 358, 'mem.active': 33, 'virtualDisk.readIOSize': 459, 'mem.overhead': 102, 'cpu.entitlement': 339, 'mem.activewrite': 357, 'mem.swapin': 346, 'virtualDisk.writeOIO': 314, 'cpu.system': 10, 'mem.swapoutRate': 86}
{'net.droppedRx': 394, 'mem.llSwapUsed': 369, 'rescpu.runav15': 440, 'cpu.usage': 2, 'mem.usage': 24, 'rescpu.maxLimited15': 446, 'rescpu.sampleCount': 447, 'cpu.costop': 341, 'datastore.numberWriteAveraged': 179, 'mem.swapout': 350, 'rescpu.samplePeriod': 448, 'cpu.idle': 13, 'mem.consumed': 98, 'net.broadcastTx': 399, 'net.droppedTx': 395, 'datastore.totalWriteLatency': 183, 'mem.swaptarget': 74, 'rescpu.actpk15': 439, 'cpu.overlap': 343, 'mem.vmmemctl': 90, 'net.packetsTx': 147, 'mem.overheadTouched': 368, 'mem.shared': 37, 'virtualDisk.readOIO': 313, 'rescpu.actpk5': 436, 'mem.compressionRate': 106, 'rescpu.actpk1': 433, 'net.bytesTx': 397, 'net.multicastTx': 401, 'sys.heartbeat': 405, 'cpu.ready': 12, 'net.usage': 143, 'rescpu.actav1': 432, 'rescpu.runpk15': 445, 'mem.swapinRate': 85, 'mem.compressed': 105, 'mem.vmmemctltarget': 94, 'net.received': 148, 'cpu.maxlimited': 342, 'virtualDisk.writeLatencyUS': 465, 'mem.swapped': 70, 'datastore.totalReadLatency': 182, 'net.broadcastRx': 398, 'mem.decompressionRate': 107, 'mem.llSwapInRate': 366, 'datastore.write': 181, 'cpu.swapwait': 328, 'rescpu.maxLimited1': 442, 'virtualDisk.smallSeeks': 461, 'cpu.demand': 340, 'mem.latency': 362, 'virtualDisk.numberReadAveraged': 171, 'cpu.wait': 11, 'mem.llSwapOutRate': 367, 'mem.entitlement': 363, 'net.multicastRx': 400, 'sys.uptime': 155, 'net.bytesRx': 396, 'mem.granted': 29, 'cpu.latency': 338, 'datastore.numberReadAveraged': 178, 'net.transmitted': 149, 'rescpu.runav5': 437, 'virtualDisk.totalWriteLatency': 176, 'rescpu.runav1': 434, 'virtualDisk.numberWriteAveraged': 172, 'mem.zipSaved': 361, 'virtualDisk.largeSeeks': 463, 'net.packetsRx': 146, 'virtualDisk.writeLoadMetric': 316, 'virtualDisk.mediumSeeks': 462, 'power.energy': 159, 'cpu.usagemhz': 6, 'virtualDisk.read': 173, 'rescpu.maxLimited5': 444, 'virtualDisk.readLoadMetric': 315, 'cpu.used': 14, 'sys.osUptime': 430, 'virtualDisk.write': 174, 'disk.maxTotalLatency': 133, 'datastore.read': 180, 'datastore.maxTotalLatency': 184, 'mem.zero': 41, 'rescpu.runpk1': 441, 'rescpu.runpk5': 443, 'virtualDisk.readLatencyUS': 464, 'power.power': 157, 'cpu.run': 344, 'virtualDisk.writeIOSize': 460, 'rescpu.actav15': 438, 'rescpu.actav5': 435, 'mem.zipped': 360, 'virtualDisk.totalReadLatency': 175, 'mem.overheadMax': 358, 'mem.active': 33, 'virtualDisk.readIOSize': 459, 'mem.overhead': 102, 'cpu.entitlement': 339, 'mem.activewrite': 357, 'mem.swapin': 346, 'virtualDisk.writeOIO': 314, 'cpu.system': 10, 'mem.swapoutRate': 86}
{'disk.numberWrite': 129, 'mem.vmmemctl': 90, 'mem.vmmemctltarget': 94, 'cpu.wait': 11, 'disk.commands': 380, 'disk.read': 130, 'mem.shared': 37, 'cpu.system': 10, 'mem.zero': 41, 'rescpu.runpk1': 441, 'rescpu.actpk5': 436, 'rescpu.runpk5': 443, 'rescpu.actpk1': 433, 'sys.uptime': 155, 'sys.heartbeat': 156, 'cpu.ready': 12, 'net.usage': 143, 'rescpu.actav1': 432, 'rescpu.runpk15': 445, 'mem.swapinRate': 85, 'cpu.usage': 2, 'mem.usage': 24, 'rescpu.maxLimited15': 446, 'net.received': 148, 'disk.usage': 125, 'mem.granted': 29, 'rescpu.sampleCount': 447, 'mem.swapped': 70, 'disk.write': 131, 'net.transmitted': 149, 'rescpu.runav5': 437, 'rescpu.runav15': 440, 'rescpu.actav15': 438, 'rescpu.runav1': 434, 'rescpu.actav5': 435, 'mem.swapout': 350, 'mem.active': 33, 'rescpu.samplePeriod': 448, 'net.packetsRx': 146, 'mem.consumed': 98, 'net.packetsTx': 147, 'mem.overhead': 102, 'cpu.swapwait': 328, 'disk.numberRead': 128, 'mem.swapin': 346, 'disk.commandsAborted': 134, 'cpu.usagemhz': 6, 'mem.swaptarget': 74, 'rescpu.actpk15': 439, 'mem.swapoutRate': 86, 'rescpu.maxLimited5': 444, 'cpu.used': 14, 'rescpu.maxLimited1': 442, 'disk.busResets': 135}
这怎么可能?
答案 0 :(得分:0)
您可以清理代码,它可能会修复您的错误。如果没有,你有一个更好的起始位置来调试...
from pysphere import VIServer
from pysphere.resources import VimService_services as VI
HOST="vcenter1.example.com"
USER="user_id"
PASSWORD="passwd123"
PARAMETER_LIST = {'cpu.usage': 'CPU usage',
'cpu.ready': 'CPU ready',
'mem.granted': 'Total memory'} # etc.
def get_parameters(vmpath):
"""Obtain machine name and parameters for a given vm"""
pm = server.get_performance_manager()
vm = server.get_vm_by_path(vmpath)
mor = vm._mor
parameters = pm.get_entity_counters(mor)
return vm.get_property('name'), parameters
def print_parameters(machine_name, parameters):
"""Pretty print the parameters for a machine"""
print(machine_name)
for parameter, description in PARAMETER_LIST.items():
parameter_value = parameters.get(parameter, 'N/A')
print("{0}: {1}".format(description, parameter_value))
print('-'*5)
server = VIServer()
server.connect(HOST,USER,PASSWORD)
vmlist = server.get_registered_vms(status='poweredOn')
for vmpath in vmlist:
print("Asking machine with vmpath '{0}'".format(vmpath))
machine_name, parameters = get_parameters(vmpath)
print_parameters(machine_name, parameters)
server.disconnect()