Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Added fault tolerance, if docker engine is not up (#23)
Browse files Browse the repository at this point in the history
* Added error handling when docker engine not ready

* Flake8 corrections

* Added variable to precompute strict version
  • Loading branch information
asuresh4 authored and charless-splunk committed Aug 23, 2017
1 parent 9265f7f commit 9d08a56
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions dockerplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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')]
Expand Down

0 comments on commit 9d08a56

Please sign in to comment.