@@ -133,24 +133,6 @@ def compile_filter_rules(rules):
133133
134134 return patterns , tag_names
135135
136- def get_filters (include , exclude ):
137- # The reasoning is to check exclude first, so we can skip if there is no exclude
138- if not exclude :
139- return
140-
141- filtered_tag_names = []
142- exclude_patterns = []
143- include_patterns = []
144-
145- # Compile regex
146- exclude_patterns , tag_names = compile_filter_rules (exclude )
147- filtered_tag_names .extend (tag_names )
148-
149- include_patterns , tag_names = compile_filter_rules (include )
150- filtered_tag_names .extend (tag_names )
151-
152- return set (exclude_patterns ), set (include_patterns ), set (filtered_tag_names )
153-
154136
155137class DockerDaemon (AgentCheck ):
156138 """Collect metrics and events from Docker API and cgroups."""
@@ -216,6 +198,8 @@ def init(self):
216198 # Other options
217199 self .collect_image_stats = _is_affirmative (instance .get ('collect_images_stats' , False ))
218200 self .collect_container_size = _is_affirmative (instance .get ('collect_container_size' , False ))
201+ self .collect_container_count = _is_affirmative (instance .get ('collect_container_count' , False ))
202+ self .collect_volume_count = _is_affirmative (instance .get ('collect_volume_count' , False ))
219203 self .collect_events = _is_affirmative (instance .get ('collect_events' , True ))
220204 self .collect_image_size = _is_affirmative (instance .get ('collect_image_size' , False ))
221205 self .collect_disk_stats = _is_affirmative (instance .get ('collect_disk_stats' , False ))
@@ -271,6 +255,12 @@ def check(self, instance):
271255 if self .collect_container_size :
272256 self ._report_container_size (containers_by_id )
273257
258+ if self .collect_container_count :
259+ self ._report_container_count (containers_by_id )
260+
261+ if self .collect_volume_count :
262+ self ._report_volume_count ()
263+
274264 # Collect disk stats from Docker info command
275265 if self .collect_disk_stats :
276266 self ._report_disk_stats ()
@@ -347,7 +337,7 @@ def _get_and_count_containers(self, custom_cgroups=False, healthchecks=False):
347337 except Exception as e :
348338 self .log .debug ("Unable to inspect Docker container: %s" , e )
349339
350-
340+ # TODO: deprecate these 2, they should be replaced by _report_container_count
351341 for tags , count in running_containers_count .iteritems ():
352342 self .gauge ("docker.containers.running" , count , tags = list (tags ))
353343
@@ -503,7 +493,6 @@ def _report_container_size(self, containers_by_id):
503493 tags = self ._get_tags (container , PERFORMANCE )
504494 m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
505495 if "SizeRw" in container :
506-
507496 m_func (self , 'docker.container.size_rw' , container ['SizeRw' ],
508497 tags = tags )
509498 if "SizeRootFs" in container :
@@ -540,6 +529,33 @@ def _submit_healthcheck_sc(self, container):
540529 tags = self ._get_tags (container , CONTAINER )
541530 self .service_check (HEALTHCHECK_SERVICE_CHECK_NAME , status , tags = tags )
542531
532+ def _report_container_count (self , containers_by_id ):
533+ """Report container count per state"""
534+ m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
535+
536+ per_state_count = defaultdict (int )
537+
538+ filterlambda = lambda ctr : not self ._is_container_excluded (ctr )
539+ containers = list (filter (filterlambda , containers_by_id .values ()))
540+
541+ for ctr in containers :
542+ per_state_count [ctr .get ('State' , '' )] += 1
543+
544+ for state in per_state_count :
545+ if state :
546+ m_func (self , 'docker.container.count' , per_state_count [state ], tags = ['container_state:%s' % state .lower ()])
547+
548+ def _report_volume_count (self ):
549+ """Report volume count per state (dangling or not)"""
550+ m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
551+
552+ attached_volumes = self .docker_client .volumes (filters = {'dangling' : False })
553+ dangling_volumes = self .docker_client .volumes (filters = {'dangling' : True })
554+ attached_count = len (attached_volumes ['Volumes' ])
555+ dangling_count = len (dangling_volumes ['Volumes' ])
556+ m_func (self , 'docker.volume.count' , attached_count , tags = ['volume_state:attached' ])
557+ m_func (self , 'docker.volume.count' , dangling_count , tags = ['volume_state:dangling' ])
558+
543559 def _report_image_size (self , images ):
544560 for image in images :
545561 tags = self ._get_tags (image , IMAGE )
@@ -558,6 +574,7 @@ def _report_performance_metrics(self, containers_by_id):
558574 continue
559575
560576 tags = self ._get_tags (container , PERFORMANCE )
577+
561578 self ._report_cgroup_metrics (container , tags )
562579 if "_proc_root" not in container :
563580 containers_without_proc_root .append (DockerUtil .container_name_extractor (container )[0 ])
0 commit comments