From bf398b5b88c546ca78f3733c63d0e7bae7e3c8d1 Mon Sep 17 00:00:00 2001 From: Jean Praloran Date: Fri, 27 Nov 2015 16:18:08 +1300 Subject: [PATCH] docker-py 1.6 to use non stream stats --- dockerplugin.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/dockerplugin.py b/dockerplugin.py index 47f2fdc..98697c2 100755 --- a/dockerplugin.py +++ b/dockerplugin.py @@ -33,6 +33,8 @@ import re +STREAM_DOCKER_PY_VERSION=(1, 6, 0) + def _c(c): """A helper method for representing a container in messages. If the given argument is a string, it is assumed to be the container's ID and only the @@ -180,7 +182,7 @@ class ContainerStats(threading.Thread): second), and make the most recently read data available in a variable. """ - def __init__(self, container, client): + def __init__(self, container, client, stream): threading.Thread.__init__(self) self.daemon = True self.stop = False @@ -189,6 +191,7 @@ def __init__(self, container, client): self._client = client self._feed = None self._stats = None + self._stream = stream # Automatically start stats reading thread self.start() @@ -200,10 +203,15 @@ def run(self): failures = 0 while not self.stop: try: - if not self._feed: - self._feed = self._client.stats(self._container, - decode=True) - self._stats = self._feed.next() + + if not self._stream: + if not self._feed: + self._feed = self._client.stats(self._container, + decode=True) + self._stats = self._feed.next() + else: + self._stats = self._client.stats(self._container, + decode=True,stream=False) # Reset failure count on successfull read from the stats API. failures = 0 except Exception, e: @@ -255,6 +263,12 @@ def __init__(self, docker_url=None): self.timeout = DockerPlugin.DEFAULT_DOCKER_TIMEOUT self.capture = False self.stats = {} + self.stream = False + s_version = re.match('([\d.]+)', docker.__version__) + version = tuple([int(x) for x in s_version.group(1).split('.')]) + if version >= STREAM_DOCKER_PY_VERSION: + self.stream = True + collectd.info('Docker stats use stream') def configure_callback(self, conf): for node in conf.children: @@ -310,7 +324,8 @@ def read_callback(self): # Start a stats gathering thread if the container is new. if container['Id'] not in self.stats: self.stats[container['Id']] = ContainerStats(container, - self.client) + self.client, + self.stream) # Get and process stats from the container. stats = self.stats[container['Id']].stats