how to get vm uptime through libvirt api

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. -- Best Regards, Jiatong Shen

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

Thank you very much for reply. Initially my thought of uptime is equal to executing to `uptime` inside a virtual machine. as for the second case, where virtual machine is paused for a period, does libvirt expose an api to get how long qemu process has existed? thank you. Best, Norman On Tue, Jul 26, 2022 at 3:16 PM Michal Prívozník <mprivozn@redhat.com> wrote:
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
-- Best Regards, Jiatong Shen

On 7/26/22 09:48, Jiatong Shen wrote:
Thank you very much for reply. Initially my thought of uptime is equal to executing to `uptime` inside a virtual machine. as for the second case, where virtual machine is paused for a period, does libvirt expose an api to get how long qemu process has existed?
No, it doesn't. But that should be pretty easy to implement into virDomainListGetStats()/virConnectGetAllDomainStats(). Alternatively, libvirt emits an event when a domain is started, so you may use that to record the time when the domain was started. Michal

thank you very much for reply. I have got another question which is off topic. Am I able to get vm's process id through libvirt api? Thank you very much. Best, Norman On Tue, Jul 26, 2022 at 4:04 PM Michal Prívozník <mprivozn@redhat.com> wrote:
On 7/26/22 09:48, Jiatong Shen wrote:
Thank you very much for reply. Initially my thought of uptime is equal to executing to `uptime` inside a virtual machine. as for the second case, where virtual machine is paused for a period, does libvirt expose an api to get how long qemu process has existed?
No, it doesn't. But that should be pretty easy to implement into virDomainListGetStats()/virConnectGetAllDomainStats(). Alternatively, libvirt emits an event when a domain is started, so you may use that to record the time when the domain was started.
Michal
-- Best Regards, Jiatong Shen

On 7/26/22 10:07, Jiatong Shen wrote:
thank you very much for reply. I have got another question which is off topic. Am I able to get vm's process id through libvirt api?
Unfortunately, no. We specifically try to avoid telling users this information so that they are not tempted to go behind libvirt's back and interact with QEMU directly (which can lead to a split brain scenario - libvirt thinks guest is in a certain state, but in fact it's in a different state). But as I said, it should be fairly easy to extend stats to report when a domain was started. If you don't feel comfortable providing a patch for that I can try. Michal

On Tue, Jul 26, 2022 at 10:27:19AM +0200, Michal Prívozník wrote:
On 7/26/22 10:07, Jiatong Shen wrote:
thank you very much for reply. I have got another question which is off topic. Am I able to get vm's process id through libvirt api?
Unfortunately, no. We specifically try to avoid telling users this information so that they are not tempted to go behind libvirt's back and interact with QEMU directly (which can lead to a split brain scenario - libvirt thinks guest is in a certain state, but in fact it's in a different state).
But as I said, it should be fairly easy to extend stats to report when a domain was started. If you don't feel comfortable providing a patch for that I can try.
Note, the time since a guest was started on the host is NOT the same as guest uptime. Guest uptime resets on every warm reboot the guest OS does, while QEMU stays running. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Tue, Jul 26, 2022 at 10:27:19AM +0200, Michal Prívozník wrote:
On 7/26/22 10:07, Jiatong Shen wrote:
thank you very much for reply. I have got another question which is off topic. Am I able to get vm's process id through libvirt api?
Unfortunately, no. We specifically try to avoid telling users this information so that they are not tempted to go behind libvirt's back and interact with QEMU directly (which can lead to a split brain scenario - libvirt thinks guest is in a certain state, but in fact it's in a different state).
But as I said, it should be fairly easy to extend stats to report when a domain was started. If you don't feel comfortable providing a patch for that I can try.
Note, the time since a guest was started on the host is NOT the same as guest uptime. Guest uptime resets on every warm reboot the guest OS does, while QEMU stays running.
thank you very much for the reminder. Best, Norman On Tue, Jul 26, 2022 at 4:31 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
On Tue, Jul 26, 2022 at 10:27:19AM +0200, Michal Prívozník wrote:
On 7/26/22 10:07, Jiatong Shen wrote:
thank you very much for reply. I have got another question which is off topic. Am I able to get vm's process id through libvirt api?
Unfortunately, no. We specifically try to avoid telling users this information so that they are not tempted to go behind libvirt's back and interact with QEMU directly (which can lead to a split brain scenario - libvirt thinks guest is in a certain state, but in fact it's in a different state).
But as I said, it should be fairly easy to extend stats to report when a domain was started. If you don't feel comfortable providing a patch for that I can try.
Note, the time since a guest was started on the host is NOT the same as guest uptime. Guest uptime resets on every warm reboot the guest OS does, while QEMU stays running.
With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
-- Best Regards, Jiatong Shen

Thank you very much for reply. I'll read the code and try to provide patch . Best, Norman On Tue, Jul 26, 2022 at 4:27 PM Michal Prívozník <mprivozn@redhat.com> wrote:
On 7/26/22 10:07, Jiatong Shen wrote:
thank you very much for reply. I have got another question which is off topic. Am I able to get vm's process id through libvirt api?
Unfortunately, no. We specifically try to avoid telling users this information so that they are not tempted to go behind libvirt's back and interact with QEMU directly (which can lead to a split brain scenario - libvirt thinks guest is in a certain state, but in fact it's in a different state).
But as I said, it should be fairly easy to extend stats to report when a domain was started. If you don't feel comfortable providing a patch for that I can try.
Michal
-- Best Regards, Jiatong Shen
участники (3)
-
Daniel P. Berrangé
-
Jiatong Shen
-
Michal Prívozník