-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (78 loc) · 3.05 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
This script utilizes the python library of libvirt to display relevant information from the VMs.
The data displayed are:
- VM name
- the VM state
- allocated CPU number
- allocated and max memory
- disks and their source
- the list of network interfaces with their IP addresses
"""
import sys
from xml.dom import minidom
import libvirt
from constants import VIR_DOMAIN_EVENT_MAPPING, VIR_DOMAIN_STATE_MAPPING, KB_to_MB_FACTOR
# registers default event implmenentation
libvirt.virEventRegisterDefaultImpl()
def diplay_info(_conn, dom, event, _detail, _opaque):
"""
displays the required information using the libvirt library
:param dom: domain object currently in use
:param event: the event triggered
:return: prints the information
"""
if event in (libvirt.VIR_DOMAIN_EVENT_STARTED, libvirt.VIR_DOMAIN_EVENT_STOPPED):
print("")
print("=-" * 25)
state, maxmem, mem, cpus, _cput = dom.info()
print("name: " + dom.name())
print("event: " + VIR_DOMAIN_EVENT_MAPPING.get(event, "?"))
print("state: " + VIR_DOMAIN_STATE_MAPPING.get(state, "?"))
print("cpus: " + str(cpus))
print("memory: " + str(mem * KB_to_MB_FACTOR))
print("max memory: " + str(maxmem * KB_to_MB_FACTOR))
raw_xml = dom.XMLDesc(0)
xml = minidom.parseString(raw_xml)
disk_types = xml.getElementsByTagName('disk')
for disk_type in disk_types:
print('disk: type='+disk_type.getAttribute('type') +
' device='+disk_type.getAttribute('device'))
disk_nodes = disk_type.childNodes
for disk_node in disk_nodes:
if disk_node.nodeName[0:1] != '#' and disk_node.nodeName == "source":
for attr in disk_node.attributes.keys():
print(
'disk: ' + disk_node.attributes[attr].name
+ disk_node.attributes[attr].value)
ifaces = dom.interfaceAddresses(
libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, 0)
print("IP Address: ")
for (_, val) in ifaces.items():
if val['addrs']:
for ipaddr in val['addrs']:
if ipaddr['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV4:
print(ipaddr['addr'] + " VIR_IP_ADDR_TYPE_IPV4")
elif ipaddr['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV6:
print(ipaddr['addr'] + " VIR_IP_ADDR_TYPE_IPV6")
print("=-" * 25)
def register_events(conn):
"""
add callback to receive notifications of arbitary domain events occuring on a domain
"""
conn.domainEventRegisterAny(
None,
libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE,
diplay_info,
conn)
# setup connection
CONN = libvirt.open("qemu:///system")
if CONN is None:
print('Failed to open connection to the hypervisor')
sys.exit(1)
# register events
register_events(CONN)
# event loop
while True:
# process events
libvirt.virEventRunDefaultImpl()
sys.exit(0)