使用Python + Libvirt获取正在运行的域信息

6

我想写一个简单的脚本来获取在Xen主机上运行的域的各种信息。

到目前为止,我已经有了:

import libvirt
import pprint
conn = libvirt.open('xen:///')

for id in conn.listDomainsID():
    dom = conn.lookupByID(id)
    infos = libvirt.virDomainGetInfo(dom)

这让我遇到了以下错误:

AttributeError: 'module' object has no attribute 'virDomainGetInfo'

根据API(http://www.libvirt.org/html/libvirt-libvirt.html#virDomainGetInfo)的说明,该函数至少应该返回一些内容。有什么线索吗?(我是Python新手)
3个回答

6

根据文档:http://www.libvirt.org/python.html

There is a couple of function who don't map directly to their C counterparts due to specificities in their argument conversions:

    * virConnectListDomains is replaced by virDomain::listDomainsID(self) which returns a list of the integer ID for the currently running domains
    * virDomainGetInfo is replaced by virDomain::info() which returns a list of
         1. state: one of the state values (virDomainState)
         2. maxMemory: the maximum memory used by the domain
         3. memory: the current amount of memory used by the domain
         4. nbVirtCPU: the number of virtual CPU
         5. cpuTime: the time used by the domain in nanoseconds

2
这意味着 infos = libvirt.virDomainGetInfo(dom) 必须改为 infos = dom.info() - Jochen Ritzel

4

要获取关于Python中libvirt API的文档,请使用内联帮助。

启动你的Python解释器(只需在shell中输入python即可)。

>>> import libvirt
>>> help(libvirt)

这应该为您提供有关libvirt的详细文档。

0
import libvirt
import xml.etree.ElementTree as ET
conn = libvirt.open(name)
domain = conn.lookupByName(domain_name)
domain_config = ET.fromstring(domain.XMLDesc())
domain_disks = domain_config.findall('//disk')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接