Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Listen for libvirt events
#!/usr/bin/python
import libvirt
import sys
# virDomainEventType is emitted during domain lifecycles (see libvirt.h)
VIR_DOMAIN_EVENT_MAPPING = {
0: "VIR_DOMAIN_EVENT_DEFINED",
1: "VIR_DOMAIN_EVENT_UNDEFINED",
2: "VIR_DOMAIN_EVENT_STARTED",
3: "VIR_DOMAIN_EVENT_SUSPENDED",
4: "VIR_DOMAIN_EVENT_RESUMED",
5: "VIR_DOMAIN_EVENT_STOPPED",
6: "VIR_DOMAIN_EVENT_SHUTDOWN",
7: "VIR_DOMAIN_EVENT_PMSUSPENDED",
}
# virDomainState
VIR_DOMAIN_STATE_MAPPING = {
0: "VIR_DOMAIN_NOSTATE",
1: "VIR_DOMAIN_RUNNING",
2: "VIR_DOMAIN_BLOCKED",
3: "VIR_DOMAIN_PAUSED",
4: "VIR_DOMAIN_SHUTDOWN",
5: "VIR_DOMAIN_SHUTOFF",
6: "VIR_DOMAIN_CRASHED",
7: "VIR_DOMAIN_PMSUSPENDED",
}
libvirt.virEventRegisterDefaultImpl()
def event_lifecycle_cb(conn, dom, event, detail, opaque):
print ""
print "=-" * 25
print "%s: event: %s (%s)" % (dom.name(), VIR_DOMAIN_EVENT_MAPPING.get(event, "?"), event)
print "%s: state: %s (%s)" % (dom.name(), VIR_DOMAIN_STATE_MAPPING.get(dom.state()[0], "?"), dom.state()[0])
print "=-" * 25
def conn_register_event_id_lifecycle(conn):
conn.domainEventRegisterAny(
None,
libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
event_lifecycle_cb,
conn)
# setup connection
conn=libvirt.open("xen:///")
#conn=libvirt.open("qemu:///system")
if conn == None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
# register events
conn_register_event_id_lifecycle(conn)
# event loop
while True:
libvirt.virEventRunDefaultImpl()
sys.exit(0)
@alexanderankin
Copy link

alexanderankin commented Dec 14, 2019

anyone know what the performance on this is - is this the intended use case?

@toabctl
Copy link
Author

toabctl commented Dec 18, 2019

anyone know what the performance on this is - is this the intended use case?

? This script was used to debug a problem with the libvirt+xen driver in OpenStack Nova. I was just interested in the events sent by libvirt. It's not about performance in any case...

@alexanderankin
Copy link

alexanderankin commented Dec 18, 2019

thanks for reply, i am trying to build a socketsio layer on top of libvirtd but all approaches seem to be like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment