From 81540f7ddf0bcade2ee253094d3a38ac727730c7 Mon Sep 17 00:00:00 2001 From: Remi Hakim Date: Thu, 12 Jun 2014 12:25:03 -0400 Subject: [PATCH] Let the possibility to disable instance metadata collection Fix #975 --- agent.py | 2 +- checks/collector.py | 6 ++-- config.py | 4 +++ datadog.conf.example | 8 +++++ .../win32/install_files/datadog_win32.conf | 8 +++++ tests/test_ec2.py | 2 +- util.py | 35 +++++++++++++------ 7 files changed, 49 insertions(+), 16 deletions(-) diff --git a/agent.py b/agent.py index 6b461036f5..83b25c2acf 100755 --- a/agent.py +++ b/agent.py @@ -182,7 +182,7 @@ def _set_agent_config_hostname(self, agentConfig): # in the config file. # DEPRECATED if agentConfig.get('hostname') is None and agentConfig.get('use_ec2_instance_id'): - instanceId = EC2.get_instance_id() + instanceId = EC2.get_instance_id(agentConfig) if instanceId is not None: log.info("Running on EC2, instanceId: %s" % instanceId) agentConfig['hostname'] = instanceId diff --git a/checks/collector.py b/checks/collector.py index 85b7c553e5..a7209ef3fb 100644 --- a/checks/collector.py +++ b/checks/collector.py @@ -422,12 +422,12 @@ def _build_payload(self, start_event=True): host_tags.extend([unicode(tag.strip()) for tag in self.agentConfig['tags'].split(",")]) if self.agentConfig['collect_ec2_tags']: - host_tags.extend(EC2.get_tags()) + host_tags.extend(EC2.get_tags(self.agentConfig)) if host_tags: payload['host-tags']['system'] = host_tags - GCE_tags = GCE.get_tags() + GCE_tags = GCE.get_tags(self.agentConfig) if GCE_tags is not None: payload['host-tags'][GCE.SOURCE_TYPE_NAME] = GCE_tags @@ -438,7 +438,7 @@ def _build_payload(self, start_event=True): return payload def _get_metadata(self): - metadata = EC2.get_metadata() + metadata = EC2.get_metadata(self.agentConfig) if metadata.get('hostname'): metadata['ec2-hostname'] = metadata.get('hostname') del metadata['hostname'] diff --git a/config.py b/config.py index 700294550b..0e69b483f6 100644 --- a/config.py +++ b/config.py @@ -400,6 +400,10 @@ def get_config(parse_args=True, cfg_path=None, options=None): if config.has_option("Main", "skip_ssl_validation"): agentConfig["skip_ssl_validation"] = _is_affirmative(config.get("Main", "skip_ssl_validation")) + agentConfig["collect_instance_metadata"] = True + if config.has_option("Main", "collect_instance_metadata"): + agentConfig["collect_instance_metadata"] = _is_affirmative(config.get("Main", "collect_instance_metadata")) + agentConfig["collect_ec2_tags"] = False if config.has_option("Main", "collect_ec2_tags"): agentConfig["collect_ec2_tags"] = _is_affirmative(config.get("Main", "collect_ec2_tags")) diff --git a/datadog.conf.example b/datadog.conf.example index 509e0f8151..d6fc648c47 100644 --- a/datadog.conf.example +++ b/datadog.conf.example @@ -26,6 +26,14 @@ api_key: # Collect AWS EC2 custom tags as agent tags # collect_ec2_tags: no +# Collect instance metadata +# The Agent will try to collect instance metadata for EC2 and GCE instances by +# trying to connect to the local endpoint: http://169.254.169.254 +# See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html +# and https://developers.google.com/compute/docs/metadata +# for more information +# collect_instance_metadata: yes + # Set the threshold for accepting points to allow anything # with recent_point_threshold seconds # Defaults to 30 seconds if no value is provided diff --git a/packaging/datadog-agent/win32/install_files/datadog_win32.conf b/packaging/datadog-agent/win32/install_files/datadog_win32.conf index 029543e39f..866ff69fe7 100644 --- a/packaging/datadog-agent/win32/install_files/datadog_win32.conf +++ b/packaging/datadog-agent/win32/install_files/datadog_win32.conf @@ -32,6 +32,14 @@ api_key: APIKEYHERE # Certificate file. # ca_certs = datadog-cert.pem +# Collect instance metadata +# The Agent will try to collect instance metadata for EC2 and GCE instances by +# trying to connect to the local endpoint: http://169.254.169.254 +# See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html +# and https://developers.google.com/compute/docs/metadata +# for more information +# collect_instance_metadata: yes + # ========================================================================== # # Information page configuration # ========================================================================== # diff --git a/tests/test_ec2.py b/tests/test_ec2.py index a1d69262a2..01ce92f145 100644 --- a/tests/test_ec2.py +++ b/tests/test_ec2.py @@ -12,7 +12,7 @@ def test_metadata(self): if os.environ.get('TRAVIS', False): return # Test gathering metadata from ec2 start = time.time() - d = EC2.get_metadata() + d = EC2.get_metadata({'collect_instance_metadata': True}) end = time.time() assert type(d) == types.DictType # Either we're on ec2 or we're not (at least 7 attributes expected) diff --git a/util.py b/util.py index 845c84aa75..1c23c4a4f5 100644 --- a/util.py +++ b/util.py @@ -198,7 +198,7 @@ def get_hostname(config=None): #Try to get GCE instance name if hostname is None: - gce_hostname = GCE.get_hostname() + gce_hostname = GCE.get_hostname(config) if gce_hostname is not None: if is_valid_hostname(gce_hostname): return gce_hostname @@ -222,7 +222,7 @@ def _get_hostname_unix(): # if we have an ec2 default hostname, see if there's an instance-id available if hostname is not None and True in [hostname.lower().startswith(p) for p in [u'ip-', u'domu']]: - instanceid = EC2.get_instance_id() + instanceid = EC2.get_instance_id(config) if instanceid: hostname = instanceid @@ -249,10 +249,14 @@ class GCE(object): @staticmethod - def _get_metadata(): + def _get_metadata(agentConfig): if GCE.metadata is not None: return GCE.metadata + if not agentConfig['collect_instance_metadata']: + GCE.metadata = {} + return GCE.metadata + socket_to = None try: socket_to = socket.getdefaulttimeout() @@ -279,10 +283,12 @@ def _get_metadata(): @staticmethod - def get_tags(): + def get_tags(agentConfig): + if not agentConfig['collect_instance_metadata']: + return [] try: - host_metadata = GCE._get_metadata() + host_metadata = GCE._get_metadata(agentConfig) tags = [] for key, value in host_metadata['instance'].get('attributes', {}).iteritems(): @@ -303,9 +309,9 @@ def get_tags(): return None @staticmethod - def get_hostname(): + def get_hostname(agentConfig): try: - host_metadata = GCE._get_metadata() + host_metadata = GCE._get_metadata(agentConfig) return host_metadata['instance']['hostname'].split('.')[0] except Exception: return None @@ -320,7 +326,10 @@ class EC2(object): metadata = {} @staticmethod - def get_tags(): + def get_tags(agentConfig): + if not agentConfig['collect_instance_metadata']: + return [] + socket_to = None try: socket_to = socket.getdefaulttimeout() @@ -352,7 +361,7 @@ def get_tags(): @staticmethod - def get_metadata(): + def get_metadata(agentConfig): """Use the ec2 http service to introspect the instance. This adds latency if not running on EC2 """ # >>> import urllib2 @@ -366,6 +375,10 @@ def get_metadata(): # Every call may add TIMEOUT seconds in latency so don't abuse this call # python 2.4 does not support an explicit timeout argument so force it here # Rather than monkey-patching urllib2, just lower the timeout globally for these calls + + if not agentConfig['collect_instance_metadata']: + return {} + socket_to = None try: socket_to = socket.getdefaulttimeout() @@ -391,9 +404,9 @@ def get_metadata(): return EC2.metadata @staticmethod - def get_instance_id(): + def get_instance_id(agentConfig): try: - return EC2.get_metadata().get("instance-id", None) + return EC2.get_metadata(agentConfig).get("instance-id", None) except Exception: return None