On 04/11/2014 07:14 AM, Ruben Kerkhof wrote:
Hi all,
I have a few python scripts which use the libvirt api to get interface and block device
statistics.
What has been bugging me for a while now that is that there’s no high level api to get a
list of all interfaces or block devices for a vm.
The list can be retrieved from the xml with a bit of Xpath magic, but this seems to me to
break the nice abstraction layer libvirt provides.
Ideally, I don’t have to do anything with xml, and add dependencies on xml parsers to my
code.
I’ve seen examples of code doing this, for example the collectd libvirt plugin, but there
must be many others.
Can I kindly ask for such an API? Unfortunately I don’t have the skills to code this up
myself.
It's an unavoidable fact that XML is part of the libvirt API. Going down the
route of providing APIs that return bits and pieces of the XML is a slippery
slope and increases libvirt maintenance burden.
python has a native XML library. To do what you want is pretty straight
forward once you understand the concepts. For example this prints every
interface mac address for the VM 'f20':
import xml.etree.ElementTree as ET
import libvirt
conn = libvirt.open("qemu:///system")
dom = conn.lookupByName("f20")
xml = dom.XMLDesc(0)
root = ET.fromstring(xml)
ifaces = root.findall("./devices/interface/mac")
for iface in ifaces:
print iface.attrib["address"]