'Return the container CPU usage.
Input: id is the full container id
all_stats is the output of the stats method of the Docker API
Output: a dict {\'total\': 1.49}'

def get_docker_cpu(self, container_id, all_stats):

    cpu_new = {}
    ret = {'total': 0.0}
    try:
        cpu_new['total'] = all_stats['cpu_stats']['cpu_usage']['total_usage']
        cpu_new['system'] = all_stats['cpu_stats']['system_cpu_usage']
        cpu_new['nb_core'] = len((all_stats['cpu_stats']['cpu_usage']['percpu_usage'] or []))
    except KeyError as e:
        logger.debug('docker    plugin    -    Cannot    grab    CPU    usage    for    container    {}    ({})'.format(container_id, e))
        logger.debug(all_stats)
    else:
        if (not hasattr(self, 'cpu_old')):
            self.cpu_old = {}
            try:
                self.cpu_old[container_id] = cpu_new
            except (IOError, UnboundLocalError):
                pass
        if (container_id not in self.cpu_old):
            try:
                self.cpu_old[container_id] = cpu_new
            except (IOError, UnboundLocalError):
                pass
        else:
            cpu_delta = float((cpu_new['total'] - self.cpu_old[container_id]['total']))
            system_delta = float((cpu_new['system'] - self.cpu_old[container_id]['system']))
            if ((cpu_delta > 0.0) and (system_delta > 0.0)):
                ret['total'] = (((cpu_delta / system_delta) * float(cpu_new['nb_core'])) * 100)
            self.cpu_old[container_id] = cpu_new
    return ret
