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