-
Notifications
You must be signed in to change notification settings - Fork 1
/
libvirt_wrapper.py
39 lines (32 loc) · 1.09 KB
/
libvirt_wrapper.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
import libvirt
import psutil
import guest
class Connection:
def __init__(self):
self.conn = libvirt.open("qemu:///system")
def get_guests(self):
kvm_processes = [process
for process
in psutil.get_process_list()
if process.name == "kvm"]
pid_for_name = {}
for process in kvm_processes:
flag = False
for arg in process.cmdline:
if flag:
pid_for_name[arg] = process.pid
break
if arg == "-name":
flag = True
domains = [self.conn.lookupByID(id)
for id
in self.conn.listDomainsID()]
result = []
for domain in domains:
result.append(guest.Guest(domain, pid_for_name[domain.name()]))
return result
if __name__ == "__main__":
conn = Connection()
guests = conn.get_guests()
for guest in guests:
print guest.get_name(), ":", guest.get_pid(), ":", guest.get_mac(), ":", guest.get_ip()