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