X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-remote.git/blobdiff_plain/8fa7103160f7a8e6663f88be13cb7a40e3032764..b28ef7ddcb14f44c694a55dc547571ff30c3c5cc:/host/usr/sbin/invirt-availability diff --git a/host/usr/sbin/invirt-availability b/host/usr/sbin/invirt-availability new file mode 100644 index 0000000..2afd5ea --- /dev/null +++ b/host/usr/sbin/invirt-availability @@ -0,0 +1,47 @@ +#!/usr/bin/env python +""" +Gathers availability data for the VM host it's running on. +""" + +from subprocess import PIPE, Popen, call +import sys +import yaml + +# return the amount of memory in kilobytes represented by s +def parseUnits(s): + num, unit = s.split(' ') + return int(num) * {'kb':1, 'mb':1024}[unit.lower()] + +def main(argv): + p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE) + output = p.communicate()[0] + if p.returncode != 0: + raise RuntimeError("Command '%s' returned non-zero exit status %d" + % ('invirt-availability', p.returncode)) + xminfo = yaml.load(output, yaml.CSafeLoader) + + # In kilobytes + free_memory = int(xminfo['free_memory']) * 1024 + + f = open('/proc/xen/balloon', 'r') + ballooninfo = yaml.load(f.read()) + f.close() + currentallocation = parseUnits(ballooninfo['Current allocation']) + minimumtarget = parseUnits(ballooninfo['Minimum target']) + + p = Popen(['/usr/sbin/xm', 'info', '-c'], stdout=PIPE) + output = p.communicate()[0] + if p.returncode != 0: + raise RuntimeError("Command '%s' returned non-zero exit status %d" + % ('invirt-availability', p.returncode)) + xminfoc = yaml.load(output, yaml.CSafeLoader) + + # In kilobytes + dom0minmem = int(xminfoc['dom0-min-mem']) * 1024 + + dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem) + print int((free_memory + dom0_spare_memory)/1024) + return 0 + +if __name__ == '__main__': + sys.exit(main(sys.argv))