Beispiel #1
0
def real_ipv4_container(name):
    if name in stopped():
        return ''
    cmd = ["lxc-attach --name %s -- ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'" % name]
    try:
        out = subprocess.check_output(cmd, shell=True)
    except:
        return ''
    return out
Beispiel #2
0
def get_filesystem_usage(container, check_running=False):
    '''
    Returns container root filesystem current usage
    Only for running containers
    '''
    result = {
        'total': 0,
        'used': 0,
        'free': 0,
        'percent': '0'
    }
    if check_running and container in stopped():
        return result

    cmd = ["lxc-attach --name %s -- df -h /" % container]
    done = False
    try:
        usage = subprocess.check_output(cmd, shell=True).split('\n')[1].split()
        result = {'total': usage[1],
                'used': usage[2],
                'free': usage[3],
                'percent': usage[4].strip('%')}
        done = True
    except Exception as e:
        pass

    if not done:
        try:
            import lvm
            import fs

            filename = '/var/lib/lxc/%s/config' % container

            config = ConfigParser.RawConfigParser()
            config.readfp(FakeSection(open(filename)))

            try:
                rootfs = config.get('DEFAULT', cgroup['rootfs'])
            except ConfigParser.NoOptionError:
                rootfs = ''

            if rootfs:
                if lvm.is_lvm(rootfs):
                    result.update( fs.get_usage(rootfs) )
                else:
                    # Simulate lxc-attach is rootfs is directory
                    cmd = ["df -h %s" % rootfs]
                    usage = subprocess.check_output(cmd, shell=True).split('\n')[1].split()
                    result = {'total': usage[1],
                            'used': usage[2],
                            'free': usage[3],
                            'percent': usage[4].strip('%')}
        except Exception as e:
            pass

    return result
Beispiel #3
0
def memory_usage(name):
    if not exists(name):
        raise ContainerNotExists("The container (%s) does not exist!" % name)
    if name in stopped():
        return 0
    cmd = ['lxc-cgroup -n %s memory.usage_in_bytes' % name]
    try:
        out = subprocess.check_output(cmd, shell=True).splitlines()
    except:
        return 0
    return int(out[0])/1024/1024
Beispiel #4
0
def container_cpu_percent(name):
    '''
    returns CPU usage in percent
    '''
    if name in stopped():
        return '0'

    cmd = ["lxc-ps --name %s -- u | awk 'BEGIN{s=0.0}{s+=$4}END{print s}'" % name]
    try:
        out = subprocess.check_output(cmd, shell=True).strip()
    except:
        return '0'
    return out
Beispiel #5
0
def memory_usage(name):
    '''
    returns memory usage in MB
    '''
    if not exists(name):
        raise ContainerNotExists("The container (%s) does not exist!" % name)
    if name in stopped():
        return 0
    cmd = ['lxc-cgroup -n %s memory.usage_in_bytes' % name]
    try:
        out = subprocess.check_output(cmd, shell=True).splitlines()
    except:
        return 0
    return int(out[0]) / 1024 / 1024
Beispiel #6
0
def max_memory_usage(name):
    if not exists(name):
        raise ContainerDoesntExists("The container (%s) does not exist!" % name)
    if name in stopped():
        return 0
    cmd = ['lxc-cgroup -n %s memory.limit_in_bytes' % name]
    try:
        out = subprocess.check_output(cmd, shell=True).splitlines()
    except:
        return 0
    host = host_memory_usage()
    limit = int(out[0])/1024/1024
    if limit > host["total"]:
        limit = host["total"]
    return limit