def init(self): try: # We configure the check with the right cgroup settings for this host # Just needs to be done once instance = self.instances[0] set_docker_settings(self.init_config, instance) self.client = get_client() self._docker_root = self.init_config.get('docker_root', '/') self._mountpoints = get_mountpoints(self._docker_root) self.cgroup_listing_retries = 0 self._latest_size_query = 0 self._filtered_containers = set() self._disable_net_metrics = False # At first run we'll just collect the events from the latest 60 secs self._last_event_collection_ts = int(time.time()) - 60 # Set tagging options self.custom_tags = instance.get("tags", []) self.collect_labels_as_tags = instance.get( "collect_labels_as_tags", []) self.tag_names = { CONTAINER: instance.get("container_tags", DEFAULT_CONTAINER_TAGS), PERFORMANCE: instance.get("performance_tags", DEFAULT_PERFORMANCE_TAGS), IMAGE: instance.get('image_tags', DEFAULT_IMAGE_TAGS) } # Set filtering settings if not instance.get("exclude"): self._filtering_enabled = False if instance.get("include"): self.log.warning( "You must specify an exclude section to enable filtering" ) else: self._filtering_enabled = True include = instance.get("include", []) exclude = instance.get("exclude", []) self._exclude_patterns, self._include_patterns, _filtered_tag_names = get_filters( include, exclude) self.tag_names[FILTERED] = _filtered_tag_names # Other options self.collect_image_stats = _is_affirmative( instance.get('collect_images_stats', False)) self.collect_container_size = _is_affirmative( instance.get('collect_container_size', False)) self.collect_events = _is_affirmative( instance.get('collect_events', True)) self.collect_image_size = _is_affirmative( instance.get('collect_image_size', False)) self.collect_ecs_tags = _is_affirmative( instance.get('ecs_tags', True)) and Platform.is_ecs_instance() except Exception, e: self.log.critical(e) self.warning("Initialization failed. Will retry at next iteration")
def setUp(self): self.docker_client = get_client() for c in CONTAINERS_TO_RUN: images = [ i["RepoTags"][0] for i in self.docker_client.images(c.split(":")[0]) if i["RepoTags"][0].startswith(c) ] if len(images) == 0: for line in self.docker_client.pull(c, stream=True): print line self.containers = [] for c in CONTAINERS_TO_RUN: name = "test-new-{0}".format(c.replace(":", "-")) host_config = None labels = None if c == "nginx": host_config = {"Memory": 137438953472} labels = {"label1": "nginx", "foo": "bar"} cont = self.docker_client.create_container(c, detach=True, name=name, host_config=host_config, labels=labels) self.containers.append(cont) for c in self.containers: log.info("Starting net_equip_details: {0}".format(c)) self.docker_client.start(c)
def init(self): try: # We configure the check with the right cgroup settings for this host # Just needs to be done once instance = self.instances[0] set_docker_settings(self.init_config, instance) self.client = get_client() self._docker_root = self.init_config.get('docker_root', '/') self._mountpoints = get_mountpoints(self._docker_root) self.cgroup_listing_retries = 0 self._latest_size_query = 0 self._filtered_containers = set() self._disable_net_metrics = False # At first run we'll just collect the events from the latest 60 secs self._last_event_collection_ts = int(time.time()) - 60 # Set tagging options self.custom_tags = instance.get("tags", []) self.collect_labels_as_tags = instance.get("collect_labels_as_tags", []) if self.is_k8s(): self.collect_labels_as_tags.append("io.kubernetes.pod.name") self.kube_labels = {} self.use_histogram = _is_affirmative(instance.get('use_histogram', False)) performance_tags = instance.get("performance_tags", DEFAULT_PERFORMANCE_TAGS) self.tag_names = { CONTAINER: instance.get("container_tags", DEFAULT_CONTAINER_TAGS), PERFORMANCE: performance_tags, IMAGE: instance.get('image_tags', DEFAULT_IMAGE_TAGS) } # Set filtering settings if not instance.get("exclude"): self._filtering_enabled = False if instance.get("include"): self.log.warning("You must specify an exclude section to enable filtering") else: self._filtering_enabled = True include = instance.get("include", []) exclude = instance.get("exclude", []) self._exclude_patterns, self._include_patterns, _filtered_tag_names = get_filters(include, exclude) self.tag_names[FILTERED] = _filtered_tag_names # Other options self.collect_image_stats = _is_affirmative(instance.get('collect_images_stats', False)) self.collect_container_size = _is_affirmative(instance.get('collect_container_size', False)) self.collect_events = _is_affirmative(instance.get('collect_events', True)) self.collect_image_size = _is_affirmative(instance.get('collect_image_size', False)) self.collect_ecs_tags = _is_affirmative(instance.get('ecs_tags', True)) and Platform.is_ecs_instance() self.ecs_tags = {} except Exception, e: self.log.critical(e) self.warning("Initialization failed. Will retry at next iteration")
def is_ecs_instance(): """Return True if the agent is running in an ECS instance, False otherwise.""" global _is_ecs if _is_ecs is not None: return _is_ecs try: client = get_client() containers = client.containers() for co in containers: if '/ecs-agent' in co.get('Names', ''): _is_ecs = True return True except Exception, e: pass
def is_ecs_instance(): """Return True if the agent is running in an ECS instance, False otherwise.""" global _is_ecs if _is_ecs is not None: return _is_ecs try: client = get_client() containers = client.containers() for co in containers: if "/ecs-agent" in co.get("Names", ""): _is_ecs = True return True except Exception, e: pass
def test_set_docker_settings(self): self.assertEqual(get_docker_settings()["version"], "auto") cur_loc = __file__ init_config = { "api_version": "foobar", "timeout": "42", "tls_client_cert": cur_loc, "tls_client_key": cur_loc, "tls_cacert": cur_loc, "tls": True } instance = { "url": "https://foo.bar:42", } set_docker_settings(init_config, instance) client = get_client() self.assertEqual(client.verify, cur_loc) self.assertEqual(client.cert, (cur_loc, cur_loc)) reset_docker_settings()
def setUp(self): self.docker_client = get_client() for c in CONTAINERS_TO_RUN: images = [i["RepoTags"][0] for i in self.docker_client.images(c.split(":")[0]) if i["RepoTags"][0].startswith(c)] if len(images) == 0: for line in self.docker_client.pull(c, stream=True): print line self.containers = [] for c in CONTAINERS_TO_RUN: name = "test-new-{0}".format(c.replace(":", "-")) host_config = None labels = None if c == "nginx": host_config = {"Memory": 137438953472} labels = {"label1": "nginx", "foo": "bar"} cont = self.docker_client.create_container( c, detach=True, name=name, host_config=host_config, labels=labels) self.containers.append(cont) for c in self.containers: log.info("Starting container: {0}".format(c)) self.docker_client.start(c)