Пример #1
0
def containers():
    """
    List all running containers. Dumps out the containerizer.Containers proto
    which lists all of the container IDs.
    """

    stdout, _, exit_code = invoke_docker("ps", stdout=PIPE, stderr=PIPE)
    if exit_code > 0:
        logger.error("Docker returned a bad status code (%d)" % exit_code)
        exit(1)

    running_containers = Containers()

    stdout.readline() # Read off the header
    for line in stdout:
        container_id = line.rstrip().split(" ")[-1]

        if len(container_id) > 0:
            container = running_containers.containers.add()
            container.value = container_id
        else:
            logger.error("Failed to parse container id, empty")
            exit(1)

    send_proto(running_containers)
Пример #2
0
def wait():
    """
    Wait for a running container to exit.
    """

    wait = recv_proto(Wait)

    # Acquire a lock for this container
    with container_lock(wait.container_id.value, "wait"):

        logger.info("Waiting for container %s", wait.container_id.value)

        stdout, _, return_code = invoke_docker("wait", [wait.container_id.value], stdout=PIPE)
        if return_code > 0:
            logger.error("Failed to wait for container, bad exit code (%d)", return_code)
            exit(1)

        container_exit = int(stdout.readline().rstrip())
        logger.info("Container exit code: %d", container_exit)

        termination = Termination()
        termination.killed = False
        termination.status = container_exit
        termination.message = ""

        send_proto(termination)
def usage():
    """
    Retrieve usage information about a running container.
    """

    usage = recv_proto(Usage)
    logger.info("Retrieving usage for container %s", usage.container_id.value)

    # Find the lxc container ID
    info = inspect_container(usage.container_id.value)
    lxc_container_id = info.get("ID", info.get("Id"))

    if lxc_container_id is None:
        raise Exception("Failed to get full container ID")

    logger.info("Using LXC container ID %s", lxc_container_id)

    stats = ResourceStatistics()
    stats.timestamp = int(time.time())

    # Get the number of CPU ticks
    ticks = os.sysconf("SC_CLK_TCK")
    if not ticks > 0:
        logger.error("Unable to retrieve number of CPU clock ticks")
        exit(1)

    collect_container_stats(lxc_container_id, stats, ticks)

    logger.debug("Container usage: %s", stats)

    # Send the stats back to mesos
    send_proto(stats)
def containers():
    """
    List all running containers. Dumps out the a Containers proto
    which lists all of the container IDs.
    """

    stdout, _, exit_code = invoke_docker("ps", stdout=PIPE, stderr=PIPE)
    if exit_code > 0:
        logger.error("Docker returned a bad status code (%d)" % exit_code)
        exit(1)

    send_proto(parse_docker_ps(stdout))
Пример #5
0
def usage():
    """
    Retrieve usage information about a running container.
    """

    usage = recv_proto(Usage)
    logger.info("Retrieving usage for container %s", usage.container_id.value)

    # Find the lxc container ID
    info = inspect_container(usage.container_id.value)
    lxc_container_id = info.get("ID", info.get("Id"))

    if lxc_container_id is None:
        raise Exception("Failed to get full container ID")

    logger.info("Using LXC container ID %s", lxc_container_id)

    stats = ResourceStatistics()
    stats.timestamp = int(time.time())

    # Get the number of CPU ticks
    ticks = os.sysconf("SC_CLK_TCK")
    if not ticks > 0:
        logger.error("Unable to retrieve number of CPU clock ticks")
        exit(1)

    # Retrieve the CPU stats
    try:
        stats.cpus_limit = float(read_metric(lxc_container_id, "cpu.shares")) / 256
        cpu_stats = dict(read_metrics(lxc_container_id, "cpuacct.stat"))
        if "user" in cpu_stats and "system" in cpu_stats:
            stats.cpus_user_time_secs = float(cpu_stats["user"]) / ticks
            stats.cpus_system_time_secs = float(cpu_stats["system"]) / ticks
    except:
        logger.error("Failed to get CPU usage")

    try:
        cpu_stats = dict(read_metrics(lxc_container_id, "cpu.stat"))
        if "nr_periods" in cpu_stats:
            stats.cpus_nr_periods = int(cpu_stats["nr_periods"])
        if "nr_throttled" in cpu_stats:
            stats.cpus_nr_throttled = int(cpu_stats["nr_throttled"])
        if "throttled_time" in cpu_stats:
            throttled_time_nano = int(cpu_stats["throttled_time"])
            throttled_time_secs = throttled_time_nano / 1000000000
            stats.cpus_throttled_time_secs = throttled_time_secs
    except:
        logger.error("Failed to get detailed CPU usage")

    # Retrieve the mem stats
    try:
        stats.mem_limit_bytes = int(read_metric(lxc_container_id, "memory.limit_in_bytes"))
        stats.mem_rss_bytes = int(read_metric(lxc_container_id, "memory.usage_in_bytes"))
    except:
        logger.error("Failed to get memory usage")

    try:
        mem_stats = dict(read_metrics(lxc_container_id, "memory.stat"))
        if "total_cache" in mem_stats:
            stats.mem_file_bytes = int(mem_stats["total_cache"])
        if "total_rss" in mem_stats:
            stats.mem_anon_bytes = int(mem_stats["total_rss"])
        if "total_mapped_file" in mem_stats:
            stats.mem_mapped_file_bytes = int(mem_stats["total_mapped_file"])
    except:
        logger.error("Failed to get detailed memory usage")

    logger.debug("Container usage: %s", stats)

    # Send the stats back to mesos
    send_proto(stats)
def usage():
    """
    Retrieve usage information about a running container.
    """

    usage = recv_proto(Usage)
    logger.info("Retrieving usage for container %s", usage.container_id.value)

    # Find the lxc container ID
    info = inspect_container(usage.container_id.value)
    lxc_container_id = info["ID"]

    logger.info("Using LXC container ID %s", lxc_container_id)

    stats = ResourceStatistics()
    stats.timestamp = int(time.time())

    # Get the number of CPU ticks
    ticks = os.sysconf("SC_CLK_TCK")
    if not ticks > 0:
        logger.error("Unable to retrieve number of CPU clock ticks")
        exit(1)

    # Retrieve the CPU stats
    try:
        stats.cpus_limit = float(read_metric(lxc_container_id,
                                             "cpu.shares")) / 256
        cpu_stats = dict(read_metrics(lxc_container_id, "cpuacct.stat"))
        if "user" in cpu_stats and "system" in cpu_stats:
            stats.cpus_user_time_secs = float(cpu_stats["user"]) / ticks
            stats.cpus_system_time_secs = float(cpu_stats["system"]) / ticks
    except:
        logger.error("Failed to get CPU usage")

    try:
        cpu_stats = dict(read_metrics(lxc_container_id, "cpu.stat"))
        if "nr_periods" in cpu_stats:
            stats.cpus_nr_periods = int(cpu_stats["nr_periods"])
        if "nr_throttled" in cpu_stats:
            stats.cpus_nr_throttled = int(cpu_stats["nr_throttled"])
        if "throttled_time" in cpu_stats:
            throttled_time_nano = int(cpu_stats["throttled_time"])
            throttled_time_secs = throttled_time_nano / 1000000000
            stats.cpus_throttled_time_secs = throttled_time_secs
    except:
        logger.error("Failed to get detailed CPU usage")

    # Retrieve the mem stats
    try:
        stats.mem_limit_bytes = int(
            read_metric(lxc_container_id, "memory.limit_in_bytes"))
        stats.mem_rss_bytes = int(
            read_metric(lxc_container_id, "memory.usage_in_bytes"))
    except:
        logger.error("Failed to get memory usage")

    try:
        mem_stats = dict(read_metrics(lxc_container_id, "memory.stat"))
        if "total_cache" in mem_stats:
            stats.mem_file_bytes = int(mem_stats["total_cache"])
        if "total_rss" in mem_stats:
            stats.mem_anon_bytes = int(mem_stats["total_rss"])
        if "total_mapped_file" in mem_stats:
            stats.mem_mapped_file_bytes = int(mem_stats["total_mapped_file"])
    except:
        logger.error("Failed to get detailed memory usage")

    logger.debug("Container usage: %s", stats)

    # Send the stats back to mesos
    send_proto(stats)