how to get vm uptime through libvirt api

Michal Prívozník mprivozn at redhat.com
Tue Jul 26 07:15:59 UTC 2022


On 7/26/22 08:49, Jiatong Shen wrote:
> Hello community,
> 
>    I would like to know if there is an api to get a virtual machine's
> uptime. Thank you in advance for the help.

There's no such API because not even qemu guest agent has an explicit
API for that. However, it has an API to execute binaries:

# virsh qemu-agent-command --pretty $dom '{"execute":"guest-exec",
"arguments":{"path":"uptime", "capture-output":true}}'
{
  "return": {
    "pid": 1174
  }
}


# virsh qemu-agent-command --pretty $dom
'{"execute":"guest-exec-status", "arguments":{"pid":1174}}'
{
  "return": {
    "exitcode": 0,
    "out-data":
"IDA5OjEwOjU4IHVwIDggbWluLCAgMSB1c2VyLCAgbG9hZCBhdmVyYWdlOiAwLjAwLCAwLjA5LCAwLjA4Cg==",
    "exited": true
  }
}


$ echo
"IDA5OjEwOjU4IHVwIDggbWluLCAgMSB1c2VyLCAgbG9hZCBhdmVyYWdlOiAwLjAwLCAwLjA5LCAwLjA4Cg=="
| base64 -d
 09:10:58 up 8 min,  1 user,  load average: 0.00, 0.09, 0.08


But it also depends what exactly do you mean by 'uptime'. Because if I
start a guest, then pause it for 5 minutes and then let it run again,
what should 'uptime' refer to?

Michal



More information about the libvirt-users mailing list