Alternative virsh (libvirt)

I use the virsh list to display a list of vms running on the computer. I want the information to be printed in this process as a 2d array.

One way to do this is to get the output, use a tokenizer and save it in an array. But is there any other way when I can directly pass this as an array or something else so that the code is much more scalable. (Something I could think of was using libvirt api in python)

+7
source share
1 answer

There are really libvirt Python binding APIs .

import libvirt conn = libvirt.openReadOnly(None) # $LIBVIRT_DEFAULT_URI, or give a URI here assert conn, 'Failed to open connection' names = conn.listDefinedDomains() domains = map(conn.lookupByName, names) ids = conn.listDomainsID() running = map(conn.lookupByID, ids) columns = 3 states = { libvirt.VIR_DOMAIN_NOSTATE: 'no state', libvirt.VIR_DOMAIN_RUNNING: 'running', libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource', libvirt.VIR_DOMAIN_PAUSED: 'paused by user', libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down', libvirt.VIR_DOMAIN_SHUTOFF: 'shut off', libvirt.VIR_DOMAIN_CRASHED: 'crashed', } def info(dom): [state, maxmem, mem, ncpu, cputime] = dom.info() return '%s is %s,' % (dom.name(), states.get(state, state)) print 'Defined domains:' for row in map(None, *[iter(domains)] * columns): for domain in row: if domain: print info(domain), print print print 'Running domains:' for row in map(None, *[iter(running)] * columns): for domain in row: if domain: print info(domain), print 
+13
source

All Articles