obtenir des domaines en cours d'exécution en utilisant python + d'info libvirt

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

  •  12-10-2019
  •  | 
  •  

Question

Je suis en train de faire un script simple qui se diverses informations sur les domaines en cours d'exécution sur un hôte Xen.

Jusqu'à présent, j'ai:

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

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

ce qui me donne l'erreur suivante:

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

Ce qui, selon l'API (http://www.libvirt.org/html/libvirt-libvirt.html#virDomainGetInfo) devrait au moins me de retour quelque chose.

Un indice? (Je suis un débutant python)

Était-ce utile?

La solution

De la documentation: 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

Autres conseils

Pour obtenir sur les API documentations libvirt en python, utilisez l'aide en ligne.

Démarrez votre interpréteur Python (il suffit de taper python dans la coquille).

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

Cela devrait vous donner une documentation détaillée sur 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')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top