-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibvirt-autoballoon.py
executable file
·217 lines (168 loc) · 5.89 KB
/
libvirt-autoballoon.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
import sys
import json
import libvirt
from time import sleep
SZ_1KiB = 1
SZ_4KiB = 4
SZ_1MiB = 1024
SZ_4MiB = 4096
SZ_32MiB = 32768
THRESHOLD_RATIO = 0.25
class ExitFailure(Exception):
pass
class LibVirtAutoBalloon:
sleep_time = 1
conn = None
config = None
allowed_vms = []
configfile = "/etc/libvirt/autoballoon.json"
def __init__(self, qemu_addr='qemu:///system'):
print("Connecting to libvirt", flush=True)
self.conn = libvirt.open(qemu_addr)
if self.conn is None:
raise ExitFailure('Failed to open connection to the hypervisor')
self.__load_config()
def __load_config(self):
print("Load config file: {}".format(self.configfile))
content = open(self.configfile).read(-1)
self.config = json.loads(content, parse_int=int)
for i in self.config["vms"]:
if i["balloon"] is True:
self.allowed_vms += [i["name"]]
def dom_status(self, dom):
memstat = dom.memoryStats()
total_ram = dom_ram_total(dom)
Name = dom.name()
actual = memstat.get("actual", 0)
usable = memstat.get("usable", 0)
used = dom_ram_used(dom)
keep_usable = self.dom_keep_usable(dom)
ratio_current = self.dom_usable_ratio(dom)
print(Name,
int(total_ram / SZ_1MiB),
int(actual / SZ_1MiB),
int(used / SZ_1MiB),
int(usable / SZ_1MiB),
int(keep_usable / SZ_1MiB),
"MiB",
format(ratio_current, '2.3'), sep='\t', flush=True)
def status(self):
domainIDs = self.conn.listDomainsID()
if domainIDs is None:
raise ExitFailure('No active domains')
print("Name", "Total", "Actual", "Used", "Usable", "Thrshld", "Units", "Ratio", sep='\t', flush=True)
for domainID in domainIDs:
dom = self.conn.lookupByID(domainID)
self.dom_status(dom)
def process_domainID(self, dom):
if dom.name() not in self.allowed_vms:
return
total_ram = dom_ram_total(dom)
actual = dom_ram_actual(dom)
keep_usable = self.dom_keep_usable(dom)
used = dom_ram_used(dom)
if actual != used:
diff = used * THRESHOLD_RATIO
diff = ALIGN_DOWN(diff, SZ_32MiB)
if diff == 0:
diff = SZ_1MiB
ratio_current = self.dom_usable_ratio(dom)
if ratio_current < 1.0 and actual < total_ram:
dom_balloon(dom, actual + diff)
elif ratio_current > 1.5 and actual > keep_usable:
dom_balloon(dom, actual - diff)
def dom_print_names(self):
domainNames = []
for i in self.conn.listAllDomains():
domainNames += [i.name()]
print("Found domains:", domainNames, flush=True)
for i in domainNames:
if i not in self.allowed_vms:
print("{} not in autoballoon.json, ignored".format(i), flush=True)
def dom_keep_usable(self, dom):
name = dom.name()
total_ram = dom_ram_total(dom)
keep_usable = total_ram * THRESHOLD_RATIO
for vm in self.config["vms"]:
if vm.get("name") == name:
if vm.get("keep_free_kb"):
keep_usable = int(vm.get("keep_free_kb"))
if keep_usable > total_ram:
keep_usable = total_ram
return keep_usable
def dom_usable_ratio(self, dom):
usable = dom.memoryStats().get("usable", 0)
return usable / self.dom_keep_usable(dom)
def daemon(self):
self.sleep_time = 1
print("Start daemon", flush=True)
domain_count = -1
while True:
domainIDs = self.conn.listDomainsID()
if domainIDs is None:
print('No active domains', file=sys.stderr, flush=True)
if self.sleep_time < 10:
self.sleep_time += 1
else:
if domain_count != len(domainIDs):
domain_count = len(domainIDs)
self.dom_print_names()
if self.sleep_time > 1:
self.sleep_time -= 1
for domainID in domainIDs:
dom = self.conn.lookupByID(domainID)
self.process_domainID(dom)
sleep(self.sleep_time)
def ALIGN_DOWN(x, a):
x = int(x)
a = int(a)
return x & ~ (a - 1)
def dom_ram_total(dom):
info = dom.info()
total_ram = info[1]
return total_ram
def dom_ram_used(dom):
memstat = dom.memoryStats()
return memstat.get("actual", 0) - memstat.get("usable", 0)
def dom_ram_actual(dom):
memstat = dom.memoryStats()
return memstat.get("actual", 0)
def dom_balloon(dom, restrict_to):
name = dom.name()
actual = dom_ram_actual(dom)
restrict_to = int(restrict_to)
total_ram = dom_ram_total(dom)
if restrict_to > total_ram:
restrict_to = total_ram
actual_m = int(actual / SZ_1MiB)
restrict_to_m = int(restrict_to / SZ_1MiB)
diff = abs(actual_m - restrict_to_m)
if actual > restrict_to:
print("Shrink dom:",
name,
actual_m, "-", diff, "=", restrict_to_m, "MiB", flush=True)
else:
print("Grow dom:",
name,
actual_m, "+", diff, "=", restrict_to_m, "MiB", flush=True)
dom.setMemory(restrict_to)
def help():
print("Usage: libvirt-autoballoon <arg>", flush=True)
print(" start - start daemon", flush=True)
print(" status - show what daemon see", flush=True)
exit(0)
def libvirt_autoballoon(argv):
if len(argv) < 2:
help()
lv_ctrl = LibVirtAutoBalloon()
if argv[1] == "start":
lv_ctrl.daemon()
elif argv[1] == "status":
lv_ctrl.status()
else:
help()
def main(argv):
libvirt_autoballoon(argv)
if __name__ == '__main__':
main(sys.argv)