forked from pbanaszkiewicz/collectd-ganeti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover_vm.py
49 lines (38 loc) · 1.14 KB
/
discover_vm.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
# coding: utf-8
import os
import os.path
PATH = "/run/ganeti/kvm-hypervisor" if os.path.exists("/run") else "/var/run/ganeti/kvm-hypervisor"
def discover():
"""
Returns a list of actually running KVM virtual machines and their pids in
this manner:
result[pid] = vm_name
"""
# TODO: add Xen discovery
path = os.path.join(PATH, "pid")
results = {}
try:
for vm in os.listdir(path):
vm_path = os.path.join(path, vm)
results[open(vm_path, "r").readline().strip()] = vm
except OSError:
pass
return results
def discover_nic(hostname):
"""
Returns a list of Network Interface Controllers connected with specified
virtual machine.
NICs are being read from "/run/ganeti/kvm-hypervisor/nic/HOSTNAME/NUMBER" files
"""
path = os.path.join(PATH, "nic")
results = []
try:
host_path = os.path.join(path, hostname)
for F in os.listdir(host_path):
nic = open(os.path.join(host_path, F), "r").read()
results.append(nic)
except OSError:
pass
return results
if __name__ == "__main__":
print discover()