From a1df75452cae2df416dca01a386fc83579188705 Mon Sep 17 00:00:00 2001 From: Remi Hakim Date: Thu, 13 Aug 2015 14:32:34 -0400 Subject: [PATCH] [docker] Add support for Docker 1.8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docker 1.8 adds cgroup bind mount by default ( See https://github.com/docker/docker/blob/v1.8.0/CHANGELOG.md#runtime ) Which confuses the docker check when running in a container. This is a tiny fix to make the docker check prefers mountpoint starting with “/host”. --- checks.d/docker.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/checks.d/docker.py b/checks.d/docker.py index 7bbcb41efa..1b2bf72faf 100644 --- a/checks.d/docker.py +++ b/checks.d/docker.py @@ -442,13 +442,9 @@ def _get_cgroup_file(self, cgroup, container_id, filename): def _find_cgroup(self, hierarchy, docker_root): """Finds the mount point for a specified cgroup hierarchy. Works with old style and new style mounts.""" - fp = None - try: - fp = open(os.path.join(docker_root, "/proc/mounts")) + with open(os.path.join(docker_root, "/proc/mounts"), 'r') as fp: mounts = map(lambda x: x.split(), fp.read().splitlines()) - finally: - if fp is not None: - fp.close() + cgroup_mounts = filter(lambda x: x[2] == "cgroup", mounts) if len(cgroup_mounts) == 0: raise Exception("Can't find mounted cgroups. If you run the Agent inside a container," @@ -456,20 +452,24 @@ def _find_cgroup(self, hierarchy, docker_root): # Old cgroup style if len(cgroup_mounts) == 1: return os.path.join(docker_root, cgroup_mounts[0][1]) + + candidate = None for _, mountpoint, _, opts, _, _ in cgroup_mounts: if hierarchy in opts: - return os.path.join(docker_root, mountpoint) + if mountpoint.startswith("/host/"): + return os.path.join(docker_root, mountpoint) + candidate = mountpoint + if candidate is not None: + return os.path.join(docker_root, candidate) + raise Exception("Can't find mounted %s cgroups." % hierarchy) + def _parse_cgroup_file(self, stat_file): """Parses a cgroup pseudo file for key/values.""" - fp = None self.log.debug("Opening cgroup file: %s" % stat_file) try: - fp = open(stat_file) - return dict(map(lambda x: x.split(), fp.read().splitlines())) + with open(stat_file, 'r') as fp: + return dict(map(lambda x: x.split(), fp.read().splitlines())) except IOError: # It is possible that the container got stopped between the API call and now self.log.info("Can't open %s. Metrics for this container are skipped." % stat_file) - finally: - if fp is not None: - fp.close()