Debian Codegraph @

examples/dominfo.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 Print information about the domain DOMAIN
4 """
5
6 import libvirt
7 import libxml2
8 from argparse import ArgumentParser
9 from typing import Any
10
11
12 parser = ArgumentParser(description=__doc__)
13 parser.add_argument("domain")
14 args = parser.parse_args()
15
16
17 def print_section(title: str) -> None:
18 print("\n%s" % title)
19 print("=" * 60)
20
21
22 def print_entry(key: str, value: Any) -> None:
23 print("%-10s %-10s" % (key, value))
24
25
26 def print_xml(key: str, ctx, path: str) -> str:
27 res = ctx.xpathEval(path)
28 if res is None or len(res) == 0:
29 value = "Unknown"
30 else:
31 value = res[0].content
32 print_entry(key, value)
33 return value
34
35
36 # Connect to libvirt
37 try:
38 conn = libvirt.openReadOnly(None)
39 except libvirt.libvirtError:
40 print('Failed to open connection to the hypervisor')
41 exit(1)
42
43 try:
44 dom = conn.lookupByName(args.domain)
45 # Annoyiingly, libvirt prints its own error message here
46 except libvirt.libvirtError:
47 print("Domain %s is not running" % args.domain)
48 exit(0)
49
50 info = dom.info()
51 print_section("Domain info")
52 print_entry("State:", info[0])
53 print_entry("MaxMem:", info[1])
54 print_entry("UsedMem:", info[2])
55 print_entry("VCPUs:", info[3])
56
57 # Read some info from the XML desc
58 xmldesc = dom.XMLDesc(0)
59 doc = libxml2.parseDoc(xmldesc)
60 ctx = doc.xpathNewContext()
61 print_section("Kernel")
62 print_xml("Type:", ctx, "/domain/os/type")
63 print_xml("Kernel:", ctx, "/domain/os/kernel")
64 print_xml("initrd:", ctx, "/domain/os/initrd")
65 print_xml("cmdline:", ctx, "/domain/os/cmdline")
66
67 print_section("Devices")
68 devs = ctx.xpathEval("/domain/devices/*")
69 for d in devs:
70 ctx.setContextNode(d)
71 # import pdb; pdb.set_trace()
72 type = print_xml("Type:", ctx, "@type")
73 if type == "file":
74 print_xml("Source:", ctx, "source/@file")
75 print_xml("Target:", ctx, "target/@dev")
76 elif type == "block":
77 print_xml("Source:", ctx, "source/@dev")
78 print_xml("Target:", ctx, "target/@dev")
79 elif type == "bridge":
80 print_xml("Source:", ctx, "source/@bridge")
81 print_xml("MAC Addr:", ctx, "mac/@address")