Skip to content

Monitor guest activity using the agent #835

@afbjorklund

Description

@afbjorklund

Description

I am looking for a feature, where I can see the CPU usage and RAM usage of an instance. Preferrably disk usage too, if possible.

However, I could not find this to be easilly accessible in QEMU the same way that it is in VirtualBox or Docker or what have you ?

One workaround that I was considering earlier, would be to run a small program in the guest to output such information.

It would not be as accurate ("inside") as quering the hypervisor ("outside"), but it would be "good enough" for my purposes.


I made a proof-of-concept, using gopsutil. What you do you think about the idea ?

package main

import (
	"fmt"
	"path/filepath"
	"time"

	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/host"
	"github.com/shirou/gopsutil/v3/mem"
)

func main() {
	_, _ = cpu.Percent(1*time.Second, true)
	h, _ := host.Info()
	p, _ := disk.Partitions(false)

	fmt.Printf("Host: %v\n", h.Hostname)

	mountpoint := "/"
	for _, part := range p {
		device, err := filepath.EvalSymlinks(part.Device)
		if err != nil {
			continue
		}
		if part.Fstype == "squashfs" { // skip /snap
			continue
		}
		fmt.Printf("%s %s\n", device, part.Mountpoint)
		if device == "/dev/vda1" {
			mountpoint = part.Mountpoint
		}
	}

	for {
		t := time.Now()
		c, _ := cpu.Percent(0, false)
		v, _ := mem.VirtualMemory()
		d, _ := disk.Usage(mountpoint)

		fmt.Printf("%v\n", t.Format(time.RFC3339Nano))
		fmt.Printf("Cpu: %.2f%%\n", c[0])
		fmt.Printf("Mem: %.2f%%\n", v.UsedPercent)
		fmt.Printf("Disk: %.2f%%\n", d.UsedPercent)

		time.Sleep(1 * time.Second)
	}
}

Probably would use JSON lines, rather than text. So that it can be parsed, and graphed, on the lima client side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions