Monitoring KVM Virtual Machines: How to Check CPU and Memory Usage with Virsh Commands


2 views

When working with KVM virtual machines on Ubuntu, the virsh command provides essential monitoring capabilities. While virsh doesn't directly show real-time CPU and memory consumption like traditional system monitors, we can extract this information through several approaches.

First, list all running VMs:

virsh list --all

This shows VM IDs and their current states, which is essential before checking resource usage.

For CPU monitoring, use:

virsh cpu-stats [domain-id/name]

Example output for a VM named "ubuntu-vm":

virsh cpu-stats ubuntu-vm
CPU0:
cpu_time       123456.789s
user_time      98765.432s
system_time    24691.357s

For memory information:

virsh dommemstat [domain-id/name]

Example with detailed output:

virsh dommemstat ubuntu-vm
actual 2097152
swap_in 0
swap_out 0
major_fault 1024
minor_fault 876543

For a more interactive view, install virt-top:

sudo apt install virt-top
virt-top

This provides a top-like interface showing CPU and memory usage for all VMs.

Create a bash script to monitor multiple VMs:

#!/bin/bash
for vm in $(virsh list --name --state-running); do
    echo "=== $vm ==="
    virsh dommemstat "$vm"
    virsh cpu-stats "$vm"
    echo
done

For historical tracking, configure libvirt to log statistics:

sudo systemctl edit libvirtd
[Service]
Environment="LIBVIRT_DEBUG=1"

Then check logs with journalctl -u libvirtd

1. Statistics are from the hypervisor perspective
2. Results show allocated resources, not necessarily actual usage
3. For per-process details, connect to the VM directly


When managing KVM virtual machines on Ubuntu systems, the virsh command-line tool provides essential monitoring capabilities. While virsh doesn't directly output CPU and memory consumption metrics like traditional monitoring tools, we can combine several virsh commands with other utilities to achieve comprehensive monitoring.

The most straightforward command to check VM stats is:


virsh domstats --cpu --mem

This provides basic CPU and memory statistics for all running VMs. For a specific VM:


virsh domstats [domain_name_or_id] --cpu --mem

For more detailed CPU metrics, use:


virsh cpu-stats [domain_name_or_id]

Memory information can be retrieved with:


virsh dominfo [domain_name_or_id] | grep -E 'CPU$s$|Max memory|Used memory'

For continuous monitoring, combine virsh with watch:


watch -n 1 "virsh domstats --cpu --mem"

This refreshes the stats every second, similar to top for physical machines.

For scriptable monitoring, process the output with tools like awk:


virsh domstats | awk -F= '/cpu.time/{cpu=$2} /balloon.maximum/{mem_max=$2} /balloon.current/{mem_cur=$2} END{print "CPU:",cpu,"Memory:",mem_cur"/"mem_max}'

For more detailed metrics, access the libvirt API directly:


virsh qemu-monitor-command [domain_name] --hmp "info status"
virsh qemu-monitor-command [domain_name] --hmp "info balloon"

Here's a bash script to monitor all VMs:


#!/bin/bash
for vm in $(virsh list --name --state-running); do
    echo "VM: $vm"
    virsh dominfo $vm | grep -E 'CPU$s$|Max memory|Used memory'
    virsh cpu-stats $vm
    echo "------------------------"
done