Получите информацию об запуске доменов с помощью Python + Libvirt

StackOverflow https://stackoverflow.com/questions/4714983

  •  12-10-2019
  •  | 
  •  

Вопрос

Я пытаюсь сделать простой сценарий, который получит различную информацию о запуске доменов на хосте 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)

Это было полезно?

Решение

Из документации: 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

Другие советы

Чтобы получить документации об API Libvirt в Python, используйте встроенную помощь.

Начните интерпретатор Python (просто введите python в оболочке).

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

Это должно дать вам подробную документацию по либевирт.

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')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top