Add timeout to requests requets...

This commit is contained in:
nicolargo 2017-03-05 20:13:34 +01:00
parent 89e4d1f11f
commit cc1dd36e94
2 changed files with 24 additions and 17 deletions

View File

@ -152,9 +152,13 @@ class ThreadAwsEc2Grabber(threading.Thread):
for k, v in iteritems(self.AWS_EC2_API_METADATA): for k, v in iteritems(self.AWS_EC2_API_METADATA):
r_url = '{}/{}'.format(self.AWS_EC2_API_URL, v) r_url = '{}/{}'.format(self.AWS_EC2_API_URL, v)
try: try:
r = requests.get(r_url) # Local request, a timeout of 3 seconds is OK
r = requests.get(r_url, timeout=3)
except requests.exceptions.ConnectTimeout:
logger.debug('cloud plugin - Connection to {} timed out'.format(r_url))
break
except Exception as e: except Exception as e:
logger.debug('Can not connect to the AWS EC2 API {}'.format(r_url, e)) logger.debug('cloud plugin - Can not connect to the AWS EC2 API {}'.format(r_url, e))
break break
else: else:
if r.ok: if r.ok:

View File

@ -92,7 +92,7 @@ class Plugin(GlancesPlugin):
try: try:
ret = self.stats['containers'] ret = self.stats['containers']
except KeyError as e: except KeyError as e:
logger.debug("Docker export error {}".format(e)) logger.debug("docker plugin - Docker export error {}".format(e))
return ret return ret
def __connect_old(self, version): def __connect_old(self, version):
@ -107,7 +107,7 @@ class Plugin(GlancesPlugin):
init_docker = docker.Client init_docker = docker.Client
else: else:
# Can not found init method (new API version ?) # Can not found init method (new API version ?)
logger.error("Can not found any way to init the Docker API") logger.error("docker plugin - Can not found any way to init the Docker API")
return None return None
# Init connection to the Docker API # Init connection to the Docker API
try: try:
@ -138,36 +138,39 @@ class Plugin(GlancesPlugin):
# Check the server connection with the version() method # Check the server connection with the version() method
try: try:
ret.version() ret.version()
except requests.exceptions.ConnectTimeout:
logger.debug('docker plugin - Connection to {} timed out'.format(r_url))
break
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
# Connexion error (Docker not detected) # Connexion error (Docker not detected)
# Let this message in debug mode # Let this message in debug mode
logger.debug("Can't connect to the Docker server (%s)" % e) logger.debug("docker plugin - Can't connect to the Docker server (%s)" % e)
return None return None
except docker.errors.APIError as e: except docker.errors.APIError as e:
if version is None: if version is None:
# API error (Version mismatch ?) # API error (Version mismatch ?)
logger.debug("Docker API error (%s)" % e) logger.debug("docker plugin - Docker API error (%s)" % e)
# Try the connection with the server version # Try the connection with the server version
version = re.search('(?:server API version|server)\:\ (.*)\)\".*\)', str(e)) version = re.search('(?:server API version|server)\:\ (.*)\)\".*\)', str(e))
if version: if version:
logger.debug("Try connection with Docker API version %s" % version.group(1)) logger.debug("docker plugin - Try connection with Docker API version %s" % version.group(1))
ret = self.connect(version=version.group(1)) ret = self.connect(version=version.group(1))
else: else:
logger.debug("Can not retreive Docker server version") logger.debug("docker plugin - Can not retreive Docker server version")
ret = None ret = None
else: else:
# API error # API error
logger.error("Docker API error (%s)" % e) logger.error("docker plugin - Docker API error (%s)" % e)
ret = None ret = None
except Exception as e: except Exception as e:
# Others exceptions... # Others exceptions...
# Connexion error (Docker not detected) # Connexion error (Docker not detected)
logger.error("Can't connect to the Docker server (%s)" % e) logger.error("docker plugin - Can't connect to the Docker server (%s)" % e)
ret = None ret = None
# Log an info if Docker plugin is disabled # Log an info if Docker plugin is disabled
if ret is None: if ret is None:
logger.debug("Docker plugin is disable because an error has been detected") logger.debug("docker plugin - Docker plugin is disable because an error has been detected")
return ret return ret
@ -296,7 +299,7 @@ class Plugin(GlancesPlugin):
cpu_new['nb_core'] = len(all_stats['cpu_stats']['cpu_usage']['percpu_usage'] or []) cpu_new['nb_core'] = len(all_stats['cpu_stats']['cpu_usage']['percpu_usage'] or [])
except KeyError as e: except KeyError as e:
# all_stats do not have CPU information # all_stats do not have CPU information
logger.debug("Cannot grab CPU usage for container {} ({})".format(container_id, e)) logger.debug("docker plugin - Cannot grab CPU usage for container {} ({})".format(container_id, e))
logger.debug(all_stats) logger.debug(all_stats)
else: else:
# Previous CPU stats stored in the cpu_old variable # Previous CPU stats stored in the cpu_old variable
@ -344,7 +347,7 @@ class Plugin(GlancesPlugin):
ret['max_usage'] = all_stats['memory_stats']['max_usage'] ret['max_usage'] = all_stats['memory_stats']['max_usage']
except (KeyError, TypeError) as e: except (KeyError, TypeError) as e:
# all_stats do not have MEM information # all_stats do not have MEM information
logger.debug("Cannot grab MEM usage for container {} ({})".format(container_id, e)) logger.debug("docker plugin - Cannot grab MEM usage for container {} ({})".format(container_id, e))
logger.debug(all_stats) logger.debug(all_stats)
# Return the stats # Return the stats
return ret return ret
@ -367,7 +370,7 @@ class Plugin(GlancesPlugin):
netcounters = all_stats["networks"] netcounters = all_stats["networks"]
except KeyError as e: except KeyError as e:
# all_stats do not have NETWORK information # all_stats do not have NETWORK information
logger.debug("Cannot grab NET usage for container {} ({})".format(container_id, e)) logger.debug("docker plugin - Cannot grab NET usage for container {} ({})".format(container_id, e))
logger.debug(all_stats) logger.debug(all_stats)
# No fallback available... # No fallback available...
return network_new return network_new
@ -398,7 +401,7 @@ class Plugin(GlancesPlugin):
network_new['cumulative_tx'] = netcounters["eth0"]["tx_bytes"] network_new['cumulative_tx'] = netcounters["eth0"]["tx_bytes"]
except KeyError as e: except KeyError as e:
# all_stats do not have INTERFACE information # all_stats do not have INTERFACE information
logger.debug("Cannot grab network interface usage for container {} ({})".format(container_id, e)) logger.debug("docker plugin - Cannot grab network interface usage for container {} ({})".format(container_id, e))
logger.debug(all_stats) logger.debug(all_stats)
# Save stats to compute next bitrate # Save stats to compute next bitrate
@ -425,7 +428,7 @@ class Plugin(GlancesPlugin):
iocounters = all_stats["blkio_stats"] iocounters = all_stats["blkio_stats"]
except KeyError as e: except KeyError as e:
# all_stats do not have io information # all_stats do not have io information
logger.debug("Cannot grab block IO usage for container {} ({})".format(container_id, e)) logger.debug("docker plugin - Cannot grab block IO usage for container {} ({})".format(container_id, e))
logger.debug(all_stats) logger.debug(all_stats)
# No fallback available... # No fallback available...
return io_new return io_new
@ -456,7 +459,7 @@ class Plugin(GlancesPlugin):
iow_old = [i for i in self.iocounters_old[container_id]['io_service_bytes_recursive'] if i['op'] == 'Write'][0]['value'] iow_old = [i for i in self.iocounters_old[container_id]['io_service_bytes_recursive'] if i['op'] == 'Write'][0]['value']
except (IndexError, KeyError) as e: except (IndexError, KeyError) as e:
# all_stats do not have io information # all_stats do not have io information
logger.debug("Cannot grab block IO usage for container {} ({})".format(container_id, e)) logger.debug("docker plugin - Cannot grab block IO usage for container {} ({})".format(container_id, e))
else: else:
io_new['time_since_update'] = getTimeSinceLastUpdate('docker_io_{}'.format(container_id)) io_new['time_since_update'] = getTimeSinceLastUpdate('docker_io_{}'.format(container_id))
io_new['ior'] = ior - ior_old io_new['ior'] = ior - ior_old