3 Gathers availability data for the VM host it's running on.
6 from subprocess import PIPE, Popen, call
10 # return the amount of memory in kilobytes represented by s
12 num, unit = s.split(' ')
13 return int(num) * {'kb':1, 'mb':1024}[unit.lower()]
17 Calculate the amount of memory available for new VMs
18 The numbers returned by xm info and xm info -c are in MB
19 The numbers in /proc/xen/balloon have nice units
20 All math is done in kilobytes for consistency
23 p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE)
24 output = p.communicate()[0]
26 raise RuntimeError("Command '%s' returned non-zero exit status %d"
27 % ('/usr/sbin/xm info', p.returncode))
28 xminfo = yaml.load(output, yaml.CSafeLoader)
30 free_memory = int(xminfo['free_memory']) * 1024
32 ballooninfo = yaml.load(open('/proc/xen/balloon', 'r').read())
33 currentallocation = parseUnits(ballooninfo['Current allocation'])
34 minimumtarget = parseUnits(ballooninfo['Minimum target'])
36 p = Popen(['/usr/sbin/xm', 'info', '-c'], stdout=PIPE)
37 output = p.communicate()[0]
39 raise RuntimeError("Command '%s' returned non-zero exit status %d"
40 % ('/usr/sbin/xm info -c', p.returncode))
41 xminfoc = yaml.load(output, yaml.CSafeLoader)
44 dom0minmem = int(xminfoc['dom0-min-mem']) * 1024
46 dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem)
47 print int((free_memory + dom0_spare_memory)/1024)
50 if __name__ == '__main__':
51 sys.exit(main(sys.argv))