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

Last change on this file since 2113 was 2113, checked in by iannucci, 16 years ago

Modified invirt-availability and invirt-vmcontrol to stat /etc/invirt/nocreate.

File size: 1.8 KB
RevLine 
[2104]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):
[2107]16    """
17    Calculate the amount of memory available for new VMs
18    The numbers returned by xm info and xm info -c are in MB
19    The numbers in /proc/xen/balloon have nice units
20    All math is done in kilobytes for consistency
21    Output is in MB
[2113]22
23    Bail if /etc/invirt/nocreate exists
[2107]24    """
[2113]25    try:
26        os.stat('/etc/invirt/nocreate')
27        print 0
28        return 0
29    except OSError:
30        pass
31
[2104]32    p = Popen(['/usr/sbin/xm', 'info'], stdout=PIPE)
33    output = p.communicate()[0]
34    if p.returncode != 0:
35        raise RuntimeError("Command '%s' returned non-zero exit status %d"
[2107]36                           % ('/usr/sbin/xm info', p.returncode)) 
[2104]37    xminfo = yaml.load(output, yaml.CSafeLoader)
38
39    free_memory = int(xminfo['free_memory']) * 1024
40
[2107]41    ballooninfo = yaml.load(open('/proc/xen/balloon', 'r').read())
[2104]42    currentallocation = parseUnits(ballooninfo['Current allocation'])
43    minimumtarget = parseUnits(ballooninfo['Minimum target'])
44
45    p = Popen(['/usr/sbin/xm', 'info', '-c'], stdout=PIPE)
46    output = p.communicate()[0]
47    if p.returncode != 0:
48        raise RuntimeError("Command '%s' returned non-zero exit status %d"
[2107]49                           % ('/usr/sbin/xm info -c', p.returncode)) 
[2104]50    xminfoc = yaml.load(output, yaml.CSafeLoader)
51
52    # In kilobytes
53    dom0minmem = int(xminfoc['dom0-min-mem']) * 1024
54
55    dom0_spare_memory = currentallocation - max(minimumtarget, dom0minmem)
56    print int((free_memory + dom0_spare_memory)/1024)
57    return 0
58
59if __name__ == '__main__':
60    sys.exit(main(sys.argv))
Note: See TracBrowser for help on using the repository browser.