--- /dev/null
+#!/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))