[2104] | 1 | #!/usr/bin/env python |
---|
| 2 | """ |
---|
| 3 | Gathers availability data for the VM host it's running on. |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | from subprocess import PIPE, Popen, call |
---|
| 7 | import sys |
---|
| 8 | import yaml |
---|
| 9 | |
---|
| 10 | # return the amount of memory in kilobytes represented by s |
---|
| 11 | def parseUnits(s): |
---|
| 12 | num, unit = s.split(' ') |
---|
| 13 | return int(num) * {'kb':1, 'mb':1024}[unit.lower()] |
---|
| 14 | |
---|
| 15 | def main(argv): |
---|
| 16 | p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE) |
---|
| 17 | output = p.communicate()[0] |
---|
| 18 | if p.returncode != 0: |
---|
| 19 | raise RuntimeError("Command '%s' returned non-zero exit status %d" |
---|
| 20 | % ('invirt-availability', p.returncode)) |
---|
| 21 | xminfo = yaml.load(output, yaml.CSafeLoader) |
---|
| 22 | |
---|
| 23 | # In kilobytes |
---|
| 24 | free_memory = int(xminfo['free_memory']) * 1024 |
---|
| 25 | |
---|
| 26 | f = open('/proc/xen/balloon', 'r') |
---|
| 27 | ballooninfo = yaml.load(f.read()) |
---|
| 28 | f.close() |
---|
| 29 | currentallocation = parseUnits(ballooninfo['Current allocation']) |
---|
| 30 | minimumtarget = parseUnits(ballooninfo['Minimum target']) |
---|
| 31 | |
---|
| 32 | p = Popen(['/usr/sbin/xm', 'info', '-c'], stdout=PIPE) |
---|
| 33 | output = p.communicate()[0] |
---|
| 34 | if p.returncode != 0: |
---|
| 35 | raise RuntimeError("Command '%s' returned non-zero exit status %d" |
---|
| 36 | % ('invirt-availability', p.returncode)) |
---|
| 37 | xminfoc = yaml.load(output, yaml.CSafeLoader) |
---|
| 38 | |
---|
| 39 | # In kilobytes |
---|
| 40 | dom0minmem = int(xminfoc['dom0-min-mem']) * 1024 |
---|
| 41 | |
---|
| 42 | dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem) |
---|
| 43 | print int((free_memory + dom0_spare_memory)/1024) |
---|
| 44 | return 0 |
---|
| 45 | |
---|
| 46 | if __name__ == '__main__': |
---|
| 47 | sys.exit(main(sys.argv)) |
---|