How can I get CPU usage of VM(KVM)

dommgifer picture dommgifer · Mar 25, 2016 · Viewed 12k times · Source

How can I get the cpu usage of vm in KVM like virt-manager? virt-manager monitoring vm cpu usage

Libvirt didn't provide API.

Does anyone know how to get vm cpu usage from host?

Answer

Fred Clift picture Fred Clift · Mar 30, 2016

If you have command-line access to the server, and you have the virsh command, you can use it to get stats.

There are several dom* subcommands that give you access to different things:

domifstat domain interface-device
       Get network interface stats for a running domain.

dommemstat domain [--period seconds] [[--config] [--live] | [--current]]
       Get memory stats for a running domain.


domstats [--raw] [--enforce] [--backing] [--state] [--cpu-total] [--balloon] [--vcpu] [--interface] [--block]
   [[--list-active] [--list-inactive] [--list-persistent] [--list-transient] [--list-running] [--list-paused]
   [--list-shutoff] [--list-other]] | [domain ...]
       Get statistics for multiple or all domains. Without any argument this command prints all available statistics for
       all domains.

So you might:

#virsh domstats --cpu-total server1
Domain: 'server1'
  cpu.time=144940157444
  cpu.user=65260000000
  cpu.system=14450000000

By polling that you can get the data you want.

Read the man page on virsh for more details.

edit: note that virsh is just a thin wrapper around the libvirt api - and this data is available via api calls also