-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwmcs-openstack-compute-allocations.py
69 lines (59 loc) · 1.9 KB
/
wmcs-openstack-compute-allocations.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
#!/usr/bin/env python3
import subprocess
import time
def main():
filename = "compute-allocations" + str(time.time()) + ".csv"
f = open(filename, "w")
f.write("Host, total_cpu, used_cpu, total_ram, used_ram\n")
# get list of hosts
output = subprocess.run(
["sudo", "wmcs-openstack", "host", "list", "-f", "value", "-c", "Host Name"],
stdout=subprocess.PIPE,
).stdout.decode("utf-8")
hosts = output.split("\n")[:-1]
for host in hosts:
if "cloudvirt" in host:
# gather CPU stats
output = subprocess.run(
[
"sudo",
"wmcs-openstack",
"host",
"show",
host,
"-c",
"CPU",
"-f",
"value",
],
stdout=subprocess.PIPE,
).stdout.decode("utf-8")
usage = output.split("\n")
# first is total, second is used_now
total_cpu = usage[0]
used_cpu = usage[1]
# gather MEM stats
output = subprocess.run(
[
"sudo",
"wmcs-openstack",
"host",
"show",
host,
"-c",
"Memory MB",
"-f",
"value",
],
stdout=subprocess.PIPE,
).stdout.decode("utf-8")
usage = output.split("\n")
# first is total, second is used_now
# convert memory to GB
total_ram = int(usage[0]) / 1024
used_ram = int(usage[1]) / 1024
# parse stats and output as csv
f.write(f"{host},{total_cpu},{used_cpu},{total_ram},{used_ram}\n")
f.close()
if __name__ == "__main__":
main()