Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I register event callbacks using the libvirt Python module with a QEMU backend?

I would like to write some code to monitor events for domains running under QEMU, managed by libvirt. However, trying to register an event handler yields the following error:

>>> import libvirt
>>> conn = libvirt.openReadOnly('qemu:///system')
>>> conn.domainEventRegister(callback, None)
libvir: Remote error : this function is not supported by the connection driver: no event support

("callback" in this case is a stub function that simply prints its arguments.)

The examples I've been able to find regarding libvirt's event handling don't seem to be specific as to which backend hypervisors support which features. Is this expected to work for QEMU backends?

I'm running a Fedora 16 system, which includes libvirt 0.9.6 and qemu-kvm 0.15.1.

For folks finding themselves here via <searchengine>:

UPDATE 2013-10-04

Many months and a few Fedora releases later, the event-test.py code in the libvirt git repository runs correctly on Fedora 19.

like image 349
larsks Avatar asked Jan 06 '12 18:01

larsks


1 Answers

Make sure you have registered in the libvirt event loop (or set up your own) before registering for events.

There is a nice example of event handling shipped with the libvirt source (file is called event-test.py). I'm attaching an example based on that code;

import libvirt
import time
import threading

def callback(conn, dom, event, detail, opaque):
    print "EVENT: Domain %s(%s) %s %s" % (dom.name(),
                                          dom.ID(),
                                          event,
                                          detail)

eventLoopThread = None

def virEventLoopNativeRun():
    while True:
        libvirt.virEventRunDefaultImpl()

def virEventLoopNativeStart():
    global eventLoopThread
    libvirt.virEventRegisterDefaultImpl()
    eventLoopThread = threading.Thread(target=virEventLoopNativeRun,
                                       name="libvirtEventLoop")
    eventLoopThread.setDaemon(True)
    eventLoopThread.start()

if __name__ == '__main__':

    virEventLoopNativeStart()

    conn = libvirt.openReadOnly('qemu:///system')

    conn.domainEventRegister(callback, None)
    conn.setKeepAlive(5, 3)

    while conn.isAlive() == 1:
        time.sleep(1)

Good luck!

//Seto

like image 126
Setomidor Avatar answered Sep 19 '22 02:09

Setomidor