| 1 |
|
| 2 |
""" |
| 3 |
List active domains of an ESX host and print some info. |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
import libvirt |
| 8 |
import libxml2 |
| 9 |
import getpass |
| 10 |
from argparse import ArgumentParser |
| 11 |
from typing import Any |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
def request_credentials(credentials: list[list], user_data: Any) -> int: |
| 29 |
for credential in credentials: |
| 30 |
if credential[0] == libvirt.VIR_CRED_AUTHNAME: |
| 31 |
|
| 32 |
credential[4] = input(credential[1] + ": ") |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
if len(credential[4]) == 0: |
| 38 |
credential[4] = credential[3] |
| 39 |
elif credential[0] == libvirt.VIR_CRED_NOECHOPROMPT: |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
credential[4] = getpass.getpass(credential[1] + ": ") |
| 44 |
else: |
| 45 |
return -1 |
| 46 |
|
| 47 |
return 0 |
| 48 |
|
| 49 |
|
| 50 |
def print_section(title: str) -> None: |
| 51 |
print("\n%s" % title) |
| 52 |
print("=" * 60) |
| 53 |
|
| 54 |
|
| 55 |
def print_entry(key: str, value: Any) -> None: |
| 56 |
print("%-10s %-10s" % (key, value)) |
| 57 |
|
| 58 |
|
| 59 |
def print_xml(key: str, ctx, path: str) -> str: |
| 60 |
res = ctx.xpathEval(path) |
| 61 |
|
| 62 |
if res is None or len(res) == 0: |
| 63 |
value = "Unknown" |
| 64 |
else: |
| 65 |
value = res[0].content |
| 66 |
|
| 67 |
print_entry(key, value) |
| 68 |
|
| 69 |
return value |
| 70 |
|
| 71 |
|
| 72 |
parser = ArgumentParser(description=__doc__) |
| 73 |
parser.add_argument("hostname") |
| 74 |
args = parser.parse_args() |
| 75 |
|
| 76 |
|
| 77 |
uri = "esx://%s/?no_verify=1" % args.hostname |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT], |
| 93 |
request_credentials, None] |
| 94 |
try: |
| 95 |
conn = libvirt.openAuth(uri, auth, 0) |
| 96 |
except libvirt.libvirtError: |
| 97 |
print("Failed to open connection to %s" % args.hostname) |
| 98 |
exit(1) |
| 99 |
|
| 100 |
state_names = { |
| 101 |
libvirt.VIR_DOMAIN_RUNNING: "running", |
| 102 |
libvirt.VIR_DOMAIN_BLOCKED: "idle", |
| 103 |
libvirt.VIR_DOMAIN_PAUSED: "paused", |
| 104 |
libvirt.VIR_DOMAIN_SHUTDOWN: "in shutdown", |
| 105 |
libvirt.VIR_DOMAIN_SHUTOFF: "shut off", |
| 106 |
libvirt.VIR_DOMAIN_CRASHED: "crashed", |
| 107 |
libvirt.VIR_DOMAIN_NOSTATE: "no state", |
| 108 |
} |
| 109 |
|
| 110 |
for id in conn.listDomainsID(): |
| 111 |
domain = conn.lookupByID(id) |
| 112 |
info = domain.info() |
| 113 |
|
| 114 |
print_section("Domain " + domain.name()) |
| 115 |
print_entry("ID:", id) |
| 116 |
print_entry("UUID:", domain.UUIDString()) |
| 117 |
print_entry("State:", state_names[info[0]]) |
| 118 |
print_entry("MaxMem:", info[1]) |
| 119 |
print_entry("UsedMem:", info[2]) |
| 120 |
print_entry("VCPUs:", info[3]) |
| 121 |
|
| 122 |
|
| 123 |
print_section("Devices of " + domain.name()) |
| 124 |
|
| 125 |
xmldesc = domain.XMLDesc(0) |
| 126 |
doc = libxml2.parseDoc(xmldesc) |
| 127 |
ctx = doc.xpathNewContext() |
| 128 |
devs = ctx.xpathEval("/domain/devices/*") |
| 129 |
first = True |
| 130 |
|
| 131 |
for d in devs: |
| 132 |
ctx.setContextNode(d) |
| 133 |
|
| 134 |
if not first: |
| 135 |
print("------------------------------------------------------------") |
| 136 |
else: |
| 137 |
first = False |
| 138 |
|
| 139 |
print_entry("Device", d.name) |
| 140 |
|
| 141 |
type = print_xml("Type:", ctx, "@type") |
| 142 |
|
| 143 |
if type == "file": |
| 144 |
print_xml("Source:", ctx, "source/@file") |
| 145 |
print_xml("Target:", ctx, "target/@dev") |
| 146 |
elif type == "block": |
| 147 |
print_xml("Source:", ctx, "source/@dev") |
| 148 |
print_xml("Target:", ctx, "target/@dev") |
| 149 |
elif type == "bridge": |
| 150 |
print_xml("Source:", ctx, "source/@bridge") |
| 151 |
print_xml("MAC Addr:", ctx, "mac/@address") |