From 9d08a5658c574426172fbfe53d7b36cc759ea1a5 Mon Sep 17 00:00:00 2001 From: asuresh4 Date: Wed, 23 Aug 2017 09:21:33 -0400 Subject: [PATCH] Added fault tolerance, if docker engine is not up (#23) * Added error handling when docker engine not ready * Flake8 corrections * Added variable to precompute strict version --- dockerplugin.py | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/dockerplugin.py b/dockerplugin.py index 3f1dc52..b4357f2 100755 --- a/dockerplugin.py +++ b/dockerplugin.py @@ -390,6 +390,7 @@ class DockerPlugin: # The stats endpoint is only supported by API >= 1.17 MIN_DOCKER_API_VERSION = '1.17' + MIN_DOCKER_API_STRICT_VERSION = StrictVersion(MIN_DOCKER_API_VERSION) # TODO: add support for 'networks' from API >= 1.20 to get by-iface stats. METHODS = [read_network_stats, read_blkio_stats, read_cpu_stats, @@ -544,18 +545,21 @@ def init_callback(self): try: version = self.client.version()['ApiVersion'] except IOError, e: - log.exception(('Unable to access Docker daemon at {url} ' - 'This may indicate SELinux problems. : {error}') - .format(url=self.docker_url, - error=e)) - return False + # Log a warning if connection is not established + collectd.warning(( + 'Unable to access Docker daemon at {url} in \ + init_callback. Will try in read_callback.' + 'This may indicate SELinux problems. : {error}') + .format(url=self.docker_url, error=e)) + + collectd.register_read( + self.read_callback, + interval=COLLECTION_INTERVAL) + + return True # Check API version for stats endpoint support. - if StrictVersion(version) < \ - StrictVersion(DockerPlugin.MIN_DOCKER_API_VERSION): - log.error(('Docker daemon at {url} does not ' - 'support container statistics!') - .format(url=self.docker_url)) + if not self.check_version(version): return False collectd.register_read(self.read_callback, @@ -567,7 +571,31 @@ def init_callback(self): timeout=self.timeout)) return True + # Method to compare docker version with min version required + def check_version(self, version): + if StrictVersion(version) < \ + DockerPlugin.MIN_DOCKER_API_STRICT_VERSION: + log.error(('Docker daemon at {url} does not ' + 'support container statistics!') + .format(url=self.docker_url)) + return False + return True + def read_callback(self): + try: + version = self.client.version()['ApiVersion'] + except IOError, e: + # Log a warning if connection is not established + log.exception(('Unable to access Docker daemon at {url}. ' + 'This may indicate SELinux problems. : {error}') + .format(url=self.docker_url, + error=e)) + return + + # Check API version for stats endpoint support. + if not self.check_version(version): + return + try: containers = [c for c in self.client.containers() if c['Status'].startswith('Up')]