0

Is there any way to get the cpu usage and memory usage of a vm in KVM without connecting to the guest through SSH? I mean, how does the Virtual Machine Manager get the CPU usage (graph)? I need the percentage of the cpu usage and memory as well. Does anyone know how to communicate with kvm through libvirt? I just really need to get the cpu usage and memory without SSH as much as possible.

Scenario: I am trying to build a set up that contains load balancer(host) + 3 servers(VMs) then it would notify me the cpu usage of the 3 servers so that if I need to provision another server, I would know when.

Thanks for you help. Really appreciate it.

user3752288
  • 11
  • 1
  • 4
  • You're probably going to get better answers on serverfault.com, and they might be about SNMP. Good luck. – Anders R. Bystrup Jun 18 '14 at 12:14
  • With KVM virtualization, every virtual machine is simply a process in the host's process list, so `top` or other "standard" tools will tell you CPU/memory usage for that process/VM. If you want graphs, `nmon` may be a good place to start. – twalberg Jun 18 '14 at 19:25
  • Thanks for the info @twalberg. But I'm just really interested in getting the CPU usage of the VMs. Is there a way to extract the cpu usage from the command line (through the top command)? Or an easier way to understand my question would be, how does KVM produces the graph of cpu usage? Thanks again. Appreciate it! – user3752288 Jun 19 '14 at 11:07

1 Answers1

2

You can use the virDomainGetInfo function from libvirt to get information about the memory and cpu time consumed by a domain.

Here's a very simple example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <libvirt/libvirt.h>

int main(int argc, char **argv) {
    virConnectPtr   c;
    virDomainPtr    d;
    virDomainInfo   di;
    int res;
    int domid;

    domid = atoi(argv[1]);

    c = virConnectOpen(NULL);
    d = virDomainLookupByID(c, domid);
    res = virDomainGetInfo(d, &di);

    printf("res = %d\n", res);
    printf("memory used = %ld\n", di.memory);
    printf("cpu time used = %ld\n", di.cpuTime);
}

UPDATE

I'm sure that if you examined the source for, e.g., virt-manager, you will find that it uses exactly these functions to generate the various graphs to which you refer in your question. You can determine cpu "usage" by seeing how much the "cpu time" value changes over time.

If you're just looking for something like the % of the host cpu that domain is using, you can just use ps. That is, if the corresponding qemu process is 19650, you can run:

$ ps -p 19650 -o pid,%cpu
  PID %CPU
19650 10.2
larsks
  • 277,717
  • 41
  • 399
  • 399
  • I can get the cpu time by using the command "virsh cpu-stats". I need the cpu usage. Is there any libvirt function that I can use so that I can get the cpu usage and the memory usage? :) Thanks @larsks! – user3752288 Jun 19 '14 at 11:19
  • This answer *does* provide you with the libvirt functions to determine cpu and memory usage. I've updated it to include an alternative method for getting the % host cpu consumed by a guest. If you are asking about `virsh` commands, that's not a programming question and should probably be asked over on [superuser](http://superuser.com/) or [serverfault](http://serverfault.com/). – larsks Jun 19 '14 at 11:45
  • Okay. Everything is clear now. Just finished consulting with our adviser. You are correct, I think that virsh isn't really a programming but correct me if I'm wrong, virsh is using libvirt to get the cpu time, memory usage, etc? And my main problema now is, how do I get the cpu usage of the Guest VM using libvirt? Do I create a program using the libvirt API to get exactly the same what the KVM is displaying(I'm referring to the graph)? Or there is a command in virsh to get that? I'm sorry if my questions are the same, I still couldn't grasp the whole thing. Anyway, thanks a lot @larsks! – user3752288 Jun 19 '14 at 13:14