VMware Cloud Community
spride
Enthusiast
Enthusiast

Get runtime CPU & memory usage for a running VM

For something that seems so easy, I'm having a hard time figuring this out.

All I want to do is get the current CPU and memory usage of a running VM, but I can't seem to get the information.  I have a simple script that does the following:

var maxCpu = vm.runtime.maxCpuUsage;

var maxMem = vm.runtime.maxMemoryUsage;

var curCpu = vm.summary.quickStats.overallCpuUsage;

var curMem = vm.summary.quickStats.guestMemoryUsage;

System.log("max cpu  = "+maxCpu);

System.log("max mem  = "+maxMem);

System.log("cur cpu  = "+curCpu);

System.log("cur mem  = "+curMem);

var cpuUsage = (curCpu * 100.0) / maxCpu;

var memUsage = (curMem * 100.0) / maxMem;

System.log("cpu usg  = "+cpuUsage);

System.log("mem usg  = "+memUsage);

The "vm" is the VcVirtualMachine object which is an input variable.  VMtools is running on the VM.  When I run the script and select a VM, the cpuUsage shows "0" while the memUsage shows "10" for a particular VM.  I then log into the VM and run "stress" to stress the CPU and/or memory.  I then run the orchestrator script again and the values for cpuUsage and memUsage remain unchanged.  Even if I let the VM stress for several minutes and try running the orchestrator script above, the values still remain unchanged.  I can see the CPU and memory being stressed in the vSphere client so I know the "stress" program is running correctly.  In fact vSphere shows a CPU and memory alarm for the VM after a while, but still I get the same values when I run the script above.

Anyone know what is happening and can give me a solution so I can get the actual runtime values for the CPU and memory usage of a VM in orchestrator?  Much appreciated.

0 Kudos
1 Reply
mschubi
Enthusiast
Enthusiast

Hello spride,

Yes, the values are not updated well. (in my opinion it has to do with proxy objects...)

In the API documentation for VirtualMachineQuickStats you can read:


"This data object type does not support notification, for scalability reasons.

Therefore, changes in QuickStats do not generate property collector updates.

To monitor statistics values, use the statistics and alarms modules instead."

Solution is usage of statistics modules.

A good starting point is:

https://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.PerformanceManager.ht...

and

mighty virtualization: vCO - get performance data from VM & build graph

best regards,

Mike

0 Kudos