source: trunk/packages/invirt-remote/host/usr/sbin/invirt-availability @ 2104

Last change on this file since 2104 was 2104, checked in by iannucci, 15 years ago

Added remote web availability remctl and script to calculate memory available to new VMs

File size: 1.5 KB
Line 
1#!/usr/bin/env python
2"""
3Gathers availability data for the VM host it's running on.
4"""
5
6from subprocess import PIPE, Popen, call
7import sys
8import yaml
9
10# return the amount of memory in kilobytes represented by s
11def parseUnits(s):
12    num, unit = s.split(' ')
13    return int(num) * {'kb':1, 'mb':1024}[unit.lower()]
14
15def 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
46if __name__ == '__main__':
47    sys.exit(main(sys.argv))
Note: See TracBrowser for help on using the repository browser.