2008-10-31 10:13:45 +00:00
|
|
|
#!/usr/bin/python -u
|
2009-10-09 12:05:10 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#################################################################################
|
|
|
|
# Start off by implementing a general purpose event loop for anyones use
|
|
|
|
#################################################################################
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
import os
|
2008-10-31 10:13:45 +00:00
|
|
|
import libvirt
|
|
|
|
import select
|
2009-10-09 12:05:10 +00:00
|
|
|
import errno
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
|
2011-03-23 14:30:30 +00:00
|
|
|
# For the sake of demonstration, this example program includes
|
|
|
|
# an implementation of a pure python event loop. Most applications
|
|
|
|
# would be better off just using the default libvirt event loop
|
|
|
|
# APIs, instead of implementing this in python. The exception is
|
|
|
|
# where an application wants to integrate with an existing 3rd
|
|
|
|
# party event loop impl
|
|
|
|
#
|
|
|
|
# Change this to 'False' to make the demo use the native
|
|
|
|
# libvirt event loop impl
|
|
|
|
use_pure_python_event_loop = True
|
|
|
|
|
2011-01-05 21:47:22 +00:00
|
|
|
do_debug = False
|
|
|
|
def debug(msg):
|
|
|
|
global do_debug
|
|
|
|
if do_debug:
|
|
|
|
print msg
|
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
#
|
|
|
|
# This general purpose event loop will support waiting for file handle
|
|
|
|
# I/O and errors events, as well as scheduling repeatable timers with
|
|
|
|
# a fixed interval.
|
|
|
|
#
|
|
|
|
# It is a pure python implementation based around the poll() API
|
|
|
|
#
|
|
|
|
class virEventLoopPure:
|
|
|
|
# This class contains the data we need to track for a
|
|
|
|
# single file handle
|
|
|
|
class virEventLoopPureHandle:
|
|
|
|
def __init__(self, handle, fd, events, cb, opaque):
|
|
|
|
self.handle = handle
|
|
|
|
self.fd = fd
|
|
|
|
self.events = events
|
|
|
|
self.cb = cb
|
|
|
|
self.opaque = opaque
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self.handle
|
|
|
|
|
|
|
|
def get_fd(self):
|
|
|
|
return self.fd
|
|
|
|
|
|
|
|
def get_events(self):
|
|
|
|
return self.events
|
|
|
|
|
|
|
|
def set_events(self, events):
|
|
|
|
self.events = events
|
|
|
|
|
|
|
|
def dispatch(self, events):
|
|
|
|
self.cb(self.handle,
|
|
|
|
self.fd,
|
|
|
|
events,
|
2011-06-16 00:02:27 +00:00
|
|
|
self.opaque)
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
# This class contains the data we need to track for a
|
|
|
|
# single periodic timer
|
|
|
|
class virEventLoopPureTimer:
|
|
|
|
def __init__(self, timer, interval, cb, opaque):
|
|
|
|
self.timer = timer
|
|
|
|
self.interval = interval
|
|
|
|
self.cb = cb
|
|
|
|
self.opaque = opaque
|
|
|
|
self.lastfired = 0
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self.timer
|
|
|
|
|
|
|
|
def get_interval(self):
|
|
|
|
return self.interval
|
|
|
|
|
|
|
|
def set_interval(self, interval):
|
|
|
|
self.interval = interval
|
|
|
|
|
|
|
|
def get_last_fired(self):
|
|
|
|
return self.lastfired
|
|
|
|
|
|
|
|
def set_last_fired(self, now):
|
|
|
|
self.lastfired = now
|
|
|
|
|
|
|
|
def dispatch(self):
|
|
|
|
self.cb(self.timer,
|
2011-06-16 00:02:27 +00:00
|
|
|
self.opaque)
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
|
2011-01-05 21:47:22 +00:00
|
|
|
def __init__(self):
|
2009-10-09 12:05:10 +00:00
|
|
|
self.poll = select.poll()
|
|
|
|
self.pipetrick = os.pipe()
|
2011-01-27 12:54:27 +00:00
|
|
|
self.pendingWakeup = False
|
|
|
|
self.runningPoll = False
|
2009-10-09 12:05:10 +00:00
|
|
|
self.nextHandleID = 1
|
|
|
|
self.nextTimerID = 1
|
|
|
|
self.handles = []
|
|
|
|
self.timers = []
|
|
|
|
self.quit = False
|
|
|
|
|
|
|
|
# The event loop can be used from multiple threads at once.
|
|
|
|
# Specifically while the main thread is sleeping in poll()
|
|
|
|
# waiting for events to occur, another thread may come along
|
|
|
|
# and add/update/remove a file handle, or timer. When this
|
|
|
|
# happens we need to interrupt the poll() sleep in the other
|
|
|
|
# thread, so that it'll see the file handle / timer changes.
|
|
|
|
#
|
|
|
|
# Using OS level signals for this is very unreliable and
|
|
|
|
# hard to implement correctly. Thus we use the real classic
|
|
|
|
# "self pipe" trick. A anonymous pipe, with one end registered
|
|
|
|
# with the event loop for input events. When we need to force
|
|
|
|
# the main thread out of a poll() sleep, we simple write a
|
|
|
|
# single byte of data to the other end of the pipe.
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Self pipe watch %d write %d" %(self.pipetrick[0], self.pipetrick[1]))
|
2009-10-09 12:05:10 +00:00
|
|
|
self.poll.register(self.pipetrick[0], select.POLLIN)
|
|
|
|
|
|
|
|
|
2012-10-11 16:31:20 +00:00
|
|
|
# Calculate when the next timeout is due to occur, returning
|
2009-10-09 12:05:10 +00:00
|
|
|
# the absolute timestamp for the next timeout, or 0 if there is
|
|
|
|
# no timeout due
|
|
|
|
def next_timeout(self):
|
|
|
|
next = 0
|
|
|
|
for t in self.timers:
|
|
|
|
last = t.get_last_fired()
|
|
|
|
interval = t.get_interval()
|
|
|
|
if interval < 0:
|
|
|
|
continue
|
|
|
|
if next == 0 or (last + interval) < next:
|
|
|
|
next = last + interval
|
|
|
|
|
|
|
|
return next
|
|
|
|
|
|
|
|
# Lookup a virEventLoopPureHandle object based on file descriptor
|
|
|
|
def get_handle_by_fd(self, fd):
|
|
|
|
for h in self.handles:
|
|
|
|
if h.get_fd() == fd:
|
|
|
|
return h
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Lookup a virEventLoopPureHandle object based on its event loop ID
|
|
|
|
def get_handle_by_id(self, handleID):
|
|
|
|
for h in self.handles:
|
|
|
|
if h.get_id() == handleID:
|
|
|
|
return h
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# This is the heart of the event loop, performing one single
|
|
|
|
# iteration. It asks when the next timeout is due, and then
|
|
|
|
# calcuates the maximum amount of time it is able to sleep
|
|
|
|
# for in poll() pending file handle events.
|
|
|
|
#
|
|
|
|
# It then goes into the poll() sleep.
|
|
|
|
#
|
|
|
|
# When poll() returns, there will zero or more file handle
|
|
|
|
# events which need to be dispatched to registered callbacks
|
|
|
|
# It may also be time to fire some periodic timers.
|
|
|
|
#
|
|
|
|
# Due to the coarse granularity of schedular timeslices, if
|
|
|
|
# we ask for a sleep of 500ms in order to satisfy a timer, we
|
2012-10-11 16:31:20 +00:00
|
|
|
# may return up to 1 schedular timeslice early. So even though
|
2009-10-09 12:05:10 +00:00
|
|
|
# our sleep timeout was reached, the registered timer may not
|
|
|
|
# technically be at its expiry point. This leads to us going
|
|
|
|
# back around the loop with a crazy 5ms sleep. So when checking
|
|
|
|
# if timeouts are due, we allow a margin of 20ms, to avoid
|
|
|
|
# these pointless repeated tiny sleeps.
|
|
|
|
def run_once(self):
|
|
|
|
sleep = -1
|
2011-01-27 12:54:27 +00:00
|
|
|
self.runningPoll = True
|
2012-07-19 01:49:41 +00:00
|
|
|
try:
|
|
|
|
next = self.next_timeout()
|
|
|
|
debug("Next timeout due at %d" % next)
|
|
|
|
if next > 0:
|
|
|
|
now = int(time.time() * 1000)
|
|
|
|
if now >= next:
|
|
|
|
sleep = 0
|
|
|
|
else:
|
|
|
|
sleep = (next - now) / 1000.0
|
|
|
|
|
|
|
|
debug("Poll with a sleep of %d" % sleep)
|
|
|
|
events = self.poll.poll(sleep)
|
|
|
|
|
|
|
|
# Dispatch any file handle events that occurred
|
|
|
|
for (fd, revents) in events:
|
|
|
|
# See if the events was from the self-pipe
|
|
|
|
# telling us to wakup. if so, then discard
|
|
|
|
# the data just continue
|
|
|
|
if fd == self.pipetrick[0]:
|
|
|
|
self.pendingWakeup = False
|
|
|
|
data = os.read(fd, 1)
|
|
|
|
continue
|
|
|
|
|
|
|
|
h = self.get_handle_by_fd(fd)
|
|
|
|
if h:
|
|
|
|
debug("Dispatch fd %d handle %d events %d" % (fd, h.get_id(), revents))
|
|
|
|
h.dispatch(self.events_from_poll(revents))
|
2009-10-09 12:05:10 +00:00
|
|
|
|
2012-07-19 01:49:41 +00:00
|
|
|
now = int(time.time() * 1000)
|
|
|
|
for t in self.timers:
|
|
|
|
interval = t.get_interval()
|
|
|
|
if interval < 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
want = t.get_last_fired() + interval
|
|
|
|
# Deduct 20ms, since scheduler timeslice
|
|
|
|
# means we could be ever so slightly early
|
|
|
|
if now >= (want-20):
|
|
|
|
debug("Dispatch timer %d now %s want %s" % (t.get_id(), str(now), str(want)))
|
|
|
|
t.set_last_fired(now)
|
|
|
|
t.dispatch()
|
|
|
|
|
|
|
|
except (os.error, select.error), e:
|
|
|
|
if e.args[0] != errno.EINTR:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self.runningPoll = False
|
2011-01-27 12:54:27 +00:00
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
# Actually the event loop forever
|
|
|
|
def run_loop(self):
|
|
|
|
self.quit = False
|
|
|
|
while not self.quit:
|
|
|
|
self.run_once()
|
|
|
|
|
|
|
|
def interrupt(self):
|
2011-01-27 12:54:27 +00:00
|
|
|
if self.runningPoll and not self.pendingWakeup:
|
|
|
|
self.pendingWakeup = True
|
|
|
|
os.write(self.pipetrick[1], 'c')
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Registers a new file handle 'fd', monitoring for 'events' (libvirt
|
|
|
|
# event constants), firing the callback cb() when an event occurs.
|
|
|
|
# Returns a unique integer identier for this handle, that should be
|
|
|
|
# used to later update/remove it
|
|
|
|
def add_handle(self, fd, events, cb, opaque):
|
|
|
|
handleID = self.nextHandleID + 1
|
|
|
|
self.nextHandleID = self.nextHandleID + 1
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
h = self.virEventLoopPureHandle(handleID, fd, events, cb, opaque)
|
|
|
|
self.handles.append(h)
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
self.poll.register(fd, self.events_to_poll(events))
|
|
|
|
self.interrupt()
|
2009-05-28 11:02:11 +00:00
|
|
|
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Add handle %d fd %d events %d" % (handleID, fd, events))
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
return handleID
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
# Registers a new timer with periodic expiry at 'interval' ms,
|
|
|
|
# firing cb() each time the timer expires. If 'interval' is -1,
|
|
|
|
# then the timer is registered, but not enabled
|
|
|
|
# Returns a unique integer identier for this handle, that should be
|
|
|
|
# used to later update/remove it
|
|
|
|
def add_timer(self, interval, cb, opaque):
|
|
|
|
timerID = self.nextTimerID + 1
|
|
|
|
self.nextTimerID = self.nextTimerID + 1
|
|
|
|
|
|
|
|
h = self.virEventLoopPureTimer(timerID, interval, cb, opaque)
|
|
|
|
self.timers.append(h)
|
|
|
|
self.interrupt()
|
|
|
|
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Add timer %d interval %d" % (timerID, interval))
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
return timerID
|
|
|
|
|
|
|
|
# Change the set of events to be monitored on the file handle
|
|
|
|
def update_handle(self, handleID, events):
|
|
|
|
h = self.get_handle_by_id(handleID)
|
|
|
|
if h:
|
|
|
|
h.set_events(events)
|
|
|
|
self.poll.unregister(h.get_fd())
|
|
|
|
self.poll.register(h.get_fd(), self.events_to_poll(events))
|
|
|
|
self.interrupt()
|
|
|
|
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Update handle %d fd %d events %d" % (handleID, h.get_fd(), events))
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
# Change the periodic frequency of the timer
|
|
|
|
def update_timer(self, timerID, interval):
|
|
|
|
for h in self.timers:
|
|
|
|
if h.get_id() == timerID:
|
2013-02-07 07:22:01 +00:00
|
|
|
h.set_interval(interval)
|
2009-10-09 12:05:10 +00:00
|
|
|
self.interrupt()
|
|
|
|
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Update timer %d interval %d" % (timerID, interval))
|
2009-10-09 12:05:10 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Stop monitoring for events on the file handle
|
|
|
|
def remove_handle(self, handleID):
|
|
|
|
handles = []
|
|
|
|
for h in self.handles:
|
|
|
|
if h.get_id() == handleID:
|
|
|
|
self.poll.unregister(h.get_fd())
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Remove handle %d fd %d" % (handleID, h.get_fd()))
|
2009-10-09 12:05:10 +00:00
|
|
|
else:
|
|
|
|
handles.append(h)
|
|
|
|
self.handles = handles
|
|
|
|
self.interrupt()
|
|
|
|
|
|
|
|
# Stop firing the periodic timer
|
|
|
|
def remove_timer(self, timerID):
|
|
|
|
timers = []
|
|
|
|
for h in self.timers:
|
|
|
|
if h.get_id() != timerID:
|
|
|
|
timers.append(h)
|
2011-01-05 21:47:22 +00:00
|
|
|
debug("Remove timer %d" % timerID)
|
2009-10-09 12:05:10 +00:00
|
|
|
self.timers = timers
|
|
|
|
self.interrupt()
|
|
|
|
|
|
|
|
# Convert from libvirt event constants, to poll() events constants
|
|
|
|
def events_to_poll(self, events):
|
|
|
|
ret = 0
|
|
|
|
if events & libvirt.VIR_EVENT_HANDLE_READABLE:
|
|
|
|
ret |= select.POLLIN
|
|
|
|
if events & libvirt.VIR_EVENT_HANDLE_WRITABLE:
|
|
|
|
ret |= select.POLLOUT
|
|
|
|
if events & libvirt.VIR_EVENT_HANDLE_ERROR:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= select.POLLERR
|
2009-10-09 12:05:10 +00:00
|
|
|
if events & libvirt.VIR_EVENT_HANDLE_HANGUP:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= select.POLLHUP
|
2009-10-09 12:05:10 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
# Convert from poll() event constants, to libvirt events constants
|
|
|
|
def events_from_poll(self, events):
|
2013-02-07 07:22:01 +00:00
|
|
|
ret = 0
|
2009-10-09 12:05:10 +00:00
|
|
|
if events & select.POLLIN:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= libvirt.VIR_EVENT_HANDLE_READABLE
|
2009-10-09 12:05:10 +00:00
|
|
|
if events & select.POLLOUT:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= libvirt.VIR_EVENT_HANDLE_WRITABLE
|
2009-10-09 12:05:10 +00:00
|
|
|
if events & select.POLLNVAL:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= libvirt.VIR_EVENT_HANDLE_ERROR
|
2009-10-09 12:05:10 +00:00
|
|
|
if events & select.POLLERR:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= libvirt.VIR_EVENT_HANDLE_ERROR
|
2009-10-09 12:05:10 +00:00
|
|
|
if events & select.POLLHUP:
|
2013-02-07 07:22:01 +00:00
|
|
|
ret |= libvirt.VIR_EVENT_HANDLE_HANGUP
|
|
|
|
return ret
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
# Now glue an instance of the general event loop into libvirt's event loop
|
|
|
|
###########################################################################
|
|
|
|
|
|
|
|
# This single global instance of the event loop wil be used for
|
|
|
|
# monitoring libvirt events
|
2011-01-05 21:47:22 +00:00
|
|
|
eventLoop = virEventLoopPure()
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
# This keeps track of what thread is running the event loop,
|
|
|
|
# (if it is run in a background thread)
|
|
|
|
eventLoopThread = None
|
|
|
|
|
|
|
|
|
|
|
|
# These next set of 6 methods are the glue between the official
|
|
|
|
# libvirt events API, and our particular impl of the event loop
|
|
|
|
#
|
|
|
|
# There is no reason why the 'virEventLoopPure' has to be used.
|
|
|
|
# An application could easily may these 6 glue methods hook into
|
|
|
|
# another event loop such as GLib's, or something like the python
|
|
|
|
# Twisted event framework.
|
|
|
|
|
|
|
|
def virEventAddHandleImpl(fd, events, cb, opaque):
|
|
|
|
global eventLoop
|
|
|
|
return eventLoop.add_handle(fd, events, cb, opaque)
|
|
|
|
|
|
|
|
def virEventUpdateHandleImpl(handleID, events):
|
|
|
|
global eventLoop
|
|
|
|
return eventLoop.update_handle(handleID, events)
|
|
|
|
|
|
|
|
def virEventRemoveHandleImpl(handleID):
|
|
|
|
global eventLoop
|
|
|
|
return eventLoop.remove_handle(handleID)
|
|
|
|
|
|
|
|
def virEventAddTimerImpl(interval, cb, opaque):
|
|
|
|
global eventLoop
|
|
|
|
return eventLoop.add_timer(interval, cb, opaque)
|
|
|
|
|
|
|
|
def virEventUpdateTimerImpl(timerID, interval):
|
|
|
|
global eventLoop
|
|
|
|
return eventLoop.update_timer(timerID, interval)
|
|
|
|
|
|
|
|
def virEventRemoveTimerImpl(timerID):
|
|
|
|
global eventLoop
|
|
|
|
return eventLoop.remove_timer(timerID)
|
|
|
|
|
|
|
|
# This tells libvirt what event loop implementation it
|
|
|
|
# should use
|
|
|
|
def virEventLoopPureRegister():
|
|
|
|
libvirt.virEventRegisterImpl(virEventAddHandleImpl,
|
|
|
|
virEventUpdateHandleImpl,
|
|
|
|
virEventRemoveHandleImpl,
|
|
|
|
virEventAddTimerImpl,
|
|
|
|
virEventUpdateTimerImpl,
|
|
|
|
virEventRemoveTimerImpl)
|
|
|
|
|
|
|
|
# Directly run the event loop in the current thread
|
|
|
|
def virEventLoopPureRun():
|
|
|
|
global eventLoop
|
|
|
|
eventLoop.run_loop()
|
|
|
|
|
2011-03-23 14:30:30 +00:00
|
|
|
def virEventLoopNativeRun():
|
|
|
|
while True:
|
|
|
|
libvirt.virEventRunDefaultImpl()
|
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
# Spawn a background thread to run the event loop
|
|
|
|
def virEventLoopPureStart():
|
|
|
|
global eventLoopThread
|
|
|
|
virEventLoopPureRegister()
|
|
|
|
eventLoopThread = threading.Thread(target=virEventLoopPureRun, name="libvirtEventLoop")
|
|
|
|
eventLoopThread.setDaemon(True)
|
|
|
|
eventLoopThread.start()
|
|
|
|
|
2011-03-23 14:30:30 +00:00
|
|
|
def virEventLoopNativeStart():
|
|
|
|
global eventLoopThread
|
|
|
|
libvirt.virEventRegisterDefaultImpl()
|
|
|
|
eventLoopThread = threading.Thread(target=virEventLoopNativeRun, name="libvirtEventLoop")
|
|
|
|
eventLoopThread.setDaemon(True)
|
|
|
|
eventLoopThread.start()
|
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Everything that now follows is a simple demo of domain lifecycle events
|
|
|
|
##########################################################################
|
2008-10-31 10:13:45 +00:00
|
|
|
def eventToString(event):
|
2010-04-22 15:31:05 +00:00
|
|
|
eventStrings = ( "Defined",
|
|
|
|
"Undefined",
|
2008-10-31 10:13:45 +00:00
|
|
|
"Started",
|
|
|
|
"Suspended",
|
|
|
|
"Resumed",
|
2011-12-05 12:59:13 +00:00
|
|
|
"Stopped",
|
2012-09-06 15:00:43 +00:00
|
|
|
"Shutdown",
|
2013-08-14 21:06:25 +00:00
|
|
|
"PMSuspended",
|
|
|
|
"Crashed" )
|
2013-02-07 07:22:01 +00:00
|
|
|
return eventStrings[event]
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2010-04-22 15:31:05 +00:00
|
|
|
def detailToString(event, detail):
|
|
|
|
eventStrings = (
|
|
|
|
( "Added", "Updated" ),
|
2012-09-06 15:02:47 +00:00
|
|
|
( "Removed", ),
|
2012-08-08 14:17:31 +00:00
|
|
|
( "Booted", "Migrated", "Restored", "Snapshot", "Wakeup" ),
|
2012-11-06 17:39:18 +00:00
|
|
|
( "Paused", "Migrated", "IOError", "Watchdog", "Restored", "Snapshot", "API error" ),
|
2012-08-08 14:17:31 +00:00
|
|
|
( "Unpaused", "Migrated", "Snapshot" ),
|
2011-12-05 12:59:13 +00:00
|
|
|
( "Shutdown", "Destroyed", "Crashed", "Migrated", "Saved", "Failed", "Snapshot"),
|
2012-09-06 15:02:47 +00:00
|
|
|
( "Finished", ),
|
2013-08-14 21:06:25 +00:00
|
|
|
( "Memory", "Disk" ),
|
|
|
|
( "Panicked", )
|
2010-04-22 15:31:05 +00:00
|
|
|
)
|
|
|
|
return eventStrings[event][detail]
|
|
|
|
|
2008-11-17 16:43:00 +00:00
|
|
|
def myDomainEventCallback1 (conn, dom, event, detail, opaque):
|
2010-04-22 15:31:05 +00:00
|
|
|
print "myDomainEventCallback1 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
|
|
|
|
eventToString(event),
|
|
|
|
detailToString(event, detail))
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2008-11-17 16:43:00 +00:00
|
|
|
def myDomainEventCallback2 (conn, dom, event, detail, opaque):
|
2010-04-22 15:31:05 +00:00
|
|
|
print "myDomainEventCallback2 EVENT: Domain %s(%s) %s %s" % (dom.name(), dom.ID(),
|
|
|
|
eventToString(event),
|
|
|
|
detailToString(event, detail))
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2010-03-26 13:22:06 +00:00
|
|
|
def myDomainEventRebootCallback(conn, dom, opaque):
|
|
|
|
print "myDomainEventRebootCallback: Domain %s(%s)" % (dom.name(), dom.ID())
|
|
|
|
|
|
|
|
def myDomainEventRTCChangeCallback(conn, dom, utcoffset, opaque):
|
|
|
|
print "myDomainEventRTCChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), utcoffset)
|
|
|
|
|
|
|
|
def myDomainEventWatchdogCallback(conn, dom, action, opaque):
|
|
|
|
print "myDomainEventWatchdogCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), action)
|
|
|
|
|
|
|
|
def myDomainEventIOErrorCallback(conn, dom, srcpath, devalias, action, opaque):
|
|
|
|
print "myDomainEventIOErrorCallback: Domain %s(%s) %s %s %d" % (dom.name(), dom.ID(), srcpath, devalias, action)
|
|
|
|
|
|
|
|
def myDomainEventGraphicsCallback(conn, dom, phase, localAddr, remoteAddr, authScheme, subject, opaque):
|
|
|
|
print "myDomainEventGraphicsCallback: Domain %s(%s) %d %s" % (dom.name(), dom.ID(), phase, authScheme)
|
|
|
|
|
2011-10-18 14:15:42 +00:00
|
|
|
def myDomainEventDiskChangeCallback(conn, dom, oldSrcPath, newSrcPath, devAlias, reason, opaque):
|
|
|
|
print "myDomainEventDiskChangeCallback: Domain %s(%s) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s" % (
|
|
|
|
dom.name(), dom.ID(), oldSrcPath, newSrcPath, devAlias, reason)
|
2012-03-23 13:44:50 +00:00
|
|
|
def myDomainEventTrayChangeCallback(conn, dom, devAlias, reason, opaque):
|
|
|
|
print "myDomainEventTrayChangeCallback: Domain %s(%s) tray change devAlias: %s reason: %s" % (
|
|
|
|
dom.name(), dom.ID(), devAlias, reason)
|
2012-03-23 14:43:14 +00:00
|
|
|
def myDomainEventPMWakeupCallback(conn, dom, reason, opaque):
|
|
|
|
print "myDomainEventPMWakeupCallback: Domain %s(%s) system pmwakeup" % (
|
|
|
|
dom.name(), dom.ID())
|
2012-03-23 14:50:36 +00:00
|
|
|
def myDomainEventPMSuspendCallback(conn, dom, reason, opaque):
|
|
|
|
print "myDomainEventPMSuspendCallback: Domain %s(%s) system pmsuspend" % (
|
|
|
|
dom.name(), dom.ID())
|
2012-12-11 11:07:34 +00:00
|
|
|
def myDomainEventBalloonChangeCallback(conn, dom, actual, opaque):
|
2012-07-13 09:05:17 +00:00
|
|
|
print "myDomainEventBalloonChangeCallback: Domain %s(%s) %d" % (dom.name(), dom.ID(), actual)
|
2012-10-16 14:24:41 +00:00
|
|
|
def myDomainEventPMSuspendDiskCallback(conn, dom, reason, opaque):
|
|
|
|
print "myDomainEventPMSuspendDiskCallback: Domain %s(%s) system pmsuspend_disk" % (
|
|
|
|
dom.name(), dom.ID())
|
2013-06-19 13:28:00 +00:00
|
|
|
def myDomainEventDeviceRemovedCallback(conn, dom, dev, opaque):
|
|
|
|
print "myDomainEventDeviceRemovedCallback: Domain %s(%s) device removed: %s" % (
|
|
|
|
dom.name(), dom.ID(), dev)
|
2012-07-30 16:30:42 +00:00
|
|
|
|
|
|
|
run = True
|
|
|
|
|
|
|
|
def myConnectionCloseCallback(conn, reason, opaque):
|
|
|
|
reasonStrings = (
|
|
|
|
"Error", "End-of-file", "Keepalive", "Client",
|
|
|
|
)
|
|
|
|
print "myConnectionCloseCallback: %s: %s" % (conn.getURI(), reasonStrings[reason])
|
|
|
|
run = False
|
|
|
|
|
2011-10-12 15:11:58 +00:00
|
|
|
def usage(out=sys.stderr):
|
2011-10-12 15:13:30 +00:00
|
|
|
print >>out, "usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]"
|
2011-10-12 15:11:58 +00:00
|
|
|
print >>out, " uri will default to qemu:///system"
|
2011-10-12 15:13:30 +00:00
|
|
|
print >>out, " --help, -h Print this help message"
|
|
|
|
print >>out, " --debug, -d Print debug output"
|
|
|
|
print >>out, " --loop, -l Toggle event-loop-implementation"
|
2008-10-31 10:13:45 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
2011-10-12 15:13:30 +00:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop"])
|
2008-10-31 10:13:45 +00:00
|
|
|
except getopt.GetoptError, err:
|
|
|
|
# print help information and exit:
|
|
|
|
print str(err) # will print something like "option -a not recognized"
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-h", "--help"):
|
2011-10-12 15:11:58 +00:00
|
|
|
usage(sys.stdout)
|
2008-10-31 10:13:45 +00:00
|
|
|
sys.exit()
|
2011-10-12 15:13:30 +00:00
|
|
|
if o in ("-d", "--debug"):
|
|
|
|
global do_debug
|
|
|
|
do_debug = True
|
|
|
|
if o in ("-l", "--loop"):
|
|
|
|
global use_pure_python_event_loop
|
|
|
|
use_pure_python_event_loop ^= True
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2011-10-12 14:54:40 +00:00
|
|
|
if len(args) >= 1:
|
|
|
|
uri = args[0]
|
2008-10-31 10:13:45 +00:00
|
|
|
else:
|
|
|
|
uri = "qemu:///system"
|
|
|
|
|
|
|
|
print "Using uri:" + uri
|
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
# Run a background thread with the event loop
|
2011-03-23 14:30:30 +00:00
|
|
|
if use_pure_python_event_loop:
|
|
|
|
virEventLoopPureStart()
|
|
|
|
else:
|
|
|
|
virEventLoopNativeStart()
|
2009-10-09 12:05:10 +00:00
|
|
|
|
2011-03-23 14:30:30 +00:00
|
|
|
vc = libvirt.openReadOnly(uri)
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2008-11-24 19:28:12 +00:00
|
|
|
# Close connection on exit (to test cleanup paths)
|
|
|
|
old_exitfunc = getattr(sys, 'exitfunc', None)
|
|
|
|
def exit():
|
|
|
|
print "Closing " + str(vc)
|
|
|
|
vc.close()
|
|
|
|
if (old_exitfunc): old_exitfunc()
|
|
|
|
sys.exitfunc = exit
|
|
|
|
|
2012-07-30 16:30:42 +00:00
|
|
|
vc.registerCloseCallback(myConnectionCloseCallback, None)
|
|
|
|
|
2008-10-31 10:13:45 +00:00
|
|
|
#Add 2 callbacks to prove this works with more than just one
|
|
|
|
vc.domainEventRegister(myDomainEventCallback1,None)
|
2010-03-26 13:22:06 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE, myDomainEventCallback2, None)
|
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_REBOOT, myDomainEventRebootCallback, None)
|
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_RTC_CHANGE, myDomainEventRTCChangeCallback, None)
|
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_IO_ERROR, myDomainEventIOErrorCallback, None)
|
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_WATCHDOG, myDomainEventWatchdogCallback, None)
|
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_GRAPHICS, myDomainEventGraphicsCallback, None)
|
2011-10-18 14:15:42 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_DISK_CHANGE, myDomainEventDiskChangeCallback, None)
|
2012-03-23 13:44:50 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_TRAY_CHANGE, myDomainEventTrayChangeCallback, None)
|
2012-03-23 14:43:14 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_PMWAKEUP, myDomainEventPMWakeupCallback, None)
|
2012-03-23 14:50:36 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_PMSUSPEND, myDomainEventPMSuspendCallback, None)
|
2012-07-13 09:05:17 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_BALLOON_CHANGE, myDomainEventBalloonChangeCallback, None)
|
2012-10-12 19:13:39 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_PMSUSPEND_DISK, myDomainEventPMSuspendDiskCallback, None)
|
2013-06-19 13:28:00 +00:00
|
|
|
vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED, myDomainEventDeviceRemovedCallback, None)
|
2008-10-31 10:13:45 +00:00
|
|
|
|
2011-09-16 09:04:22 +00:00
|
|
|
vc.setKeepAlive(5, 3)
|
|
|
|
|
2009-10-09 12:05:10 +00:00
|
|
|
# The rest of your app would go here normally, but for sake
|
|
|
|
# of demo we'll just go to sleep. The other option is to
|
|
|
|
# run the event loop in your main thread if your app is
|
|
|
|
# totally event based.
|
2012-07-30 16:30:42 +00:00
|
|
|
while run:
|
2009-10-09 12:05:10 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
2008-10-31 10:13:45 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|