# Buggy code
def get_docker_cpu(self, container_id, all_stats):
    """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}
    """
    cpu_new = {}
    ret = {"total": 0.0}

    # Read the stats
    # For each container, you will find a pseudo-file cpuacct.stat,
    # containing the CPU usage accumulated by the processes of the container.
    # Those times are expressed in ticks of 1/USER_HZ of a second.
    # On x86 systems, USER_HZ is 100.
    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:
        # all_stats do not have CPU information
        logger.debug(
            "Can not grab CPU usage for container {0} ({1})".format(container_id, e)
        )
    else:
        # Previous CPU stats stored in the cpu_old variable
        if not hasattr(self, "cpu_old"):
            # First call, we init the cpu_old variable
            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
                )

            # Save stats to compute next stats
            self.cpu_old[container_id] = cpu_new

    # Return the stats
    return ret


# Traceback (TypeError)
$ glances
Traceback (most recent call last):
File "/usr/local/bin/glances", line 9, in <module>
load_entry_point('Glances==2.6.1', 'console_scripts', 'glances')()
File "/usr/local/lib/python3.4/dist-packages/glances/__init__.py", line 133, in main
standalone.serve_forever()
File "/usr/local/lib/python3.4/dist-packages/glances/standalone.py", line 107, in serve_forever
return self.__serve_forever()
File "/usr/local/lib/python3.4/dist-packages/glances/standalone.py", line 88, in __serve_forever
self.stats.update()
File "/usr/local/lib/python3.4/dist-packages/glances/stats.py", line 154, in update
self._plugins[p].update()
File "/usr/local/lib/python3.4/dist-packages/glances/plugins/glances_plugin.py", line 658, in wrapper
ret = fct(*args, **kw)
File "/usr/local/lib/python3.4/dist-packages/glances/plugins/glances_docker.py", line 226, in update
container['memory'] = self.get_docker_memory(container['Id'], self.thread_list[container['Id']].stats)
File "/usr/local/lib/python3.4/dist-packages/glances/plugins/glances_docker.py", line 297, in get_docker_memory
ret['rss'] = all_stats['memory_stats']['stats']['rss']
TypeError: 'NoneType' object is not subscriptable

# Fixed code
def get_docker_cpu(self, container_id, all_stats):
    """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}
    """
    cpu_new = {}
    ret = {"total": 0.0}

    # Read the stats
    # For each container, you will find a pseudo-file cpuacct.stat,
    # containing the CPU usage accumulated by the processes of the container.
    # Those times are expressed in ticks of 1/USER_HZ of a second.
    # On x86 systems, USER_HZ is 100.
    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:
        # all_stats do not have CPU information
        logger.debug(
            "Can not grab CPU usage for container {0} ({1})".format(container_id, e)
        )
        logger.debug(all_stats)
    else:
        # Previous CPU stats stored in the cpu_old variable
        if not hasattr(self, "cpu_old"):
            # First call, we init the cpu_old variable
            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
                )

            # Save stats to compute next stats
            self.cpu_old[container_id] = cpu_new

    # Return the stats
    return ret