@@ -134,24 +134,6 @@ def compile_filter_rules(rules):
134134
135135 return patterns , tag_names
136136
137- def get_filters (include , exclude ):
138- # The reasoning is to check exclude first, so we can skip if there is no exclude
139- if not exclude :
140- return
141-
142- filtered_tag_names = []
143- exclude_patterns = []
144- include_patterns = []
145-
146- # Compile regex
147- exclude_patterns , tag_names = compile_filter_rules (exclude )
148- filtered_tag_names .extend (tag_names )
149-
150- include_patterns , tag_names = compile_filter_rules (include )
151- filtered_tag_names .extend (tag_names )
152-
153- return set (exclude_patterns ), set (include_patterns ), set (filtered_tag_names )
154-
155137
156138class DockerDaemon (AgentCheck ):
157139 """Collect metrics and events from Docker API and cgroups."""
@@ -219,10 +201,7 @@ def init(self):
219201 self .collect_image_stats = _is_affirmative (instance .get ('collect_images_stats' , False ))
220202 self .collect_container_size = _is_affirmative (instance .get ('collect_container_size' , False ))
221203 self .collect_container_count = _is_affirmative (instance .get ('collect_container_count' , False ))
222- self .collect_dead_container_count = _is_affirmative (instance .get ('collect_dead_container_count' , False ))
223- self .collect_exited_container_count = _is_affirmative (instance .get ('collect_exited_container_count' , False ))
224204 self .collect_volume_count = _is_affirmative (instance .get ('collect_volume_count' , False ))
225- self .collect_dangling_volume_count = _is_affirmative (instance .get ('collect_dangling_volume_count' , False ))
226205 self .collect_events = _is_affirmative (instance .get ('collect_events' , True ))
227206 self .collect_image_size = _is_affirmative (instance .get ('collect_image_size' , False ))
228207 self .collect_disk_stats = _is_affirmative (instance .get ('collect_disk_stats' , False ))
@@ -279,20 +258,11 @@ def check(self, instance):
279258 self ._report_container_size (containers_by_id )
280259
281260 if self .collect_container_count :
282- self ._report_container_count_by_state (containers_by_id )
283-
284- if self .collect_dead_container_count :
285- self ._report_container_count_by_state (containers_by_id , state = "Dead" )
286-
287- if self .collect_exited_container_count :
288- self ._report_container_count_by_state (containers_by_id , state = "Exited" )
261+ self ._report_container_count (containers_by_id )
289262
290263 if self .collect_volume_count :
291264 self ._report_volume_count ()
292265
293- if self .collect_dangling_volume_count :
294- self ._report_volume_count (filters = {'dangling' : True })
295-
296266 # Collect disk stats from Docker info command
297267 if self .collect_disk_stats :
298268 self ._report_disk_stats ()
@@ -369,7 +339,7 @@ def _get_and_count_containers(self, custom_cgroups=False, healthchecks=False):
369339 except Exception as e :
370340 self .log .debug ("Unable to inspect Docker container: %s" , e )
371341
372-
342+ # TODO: deprecate these 2, they should be replaced by _report_container_count
373343 for tags , count in running_containers_count .iteritems ():
374344 self .gauge ("docker.containers.running" , count , tags = list (tags ))
375345
@@ -561,25 +531,32 @@ def _submit_healthcheck_sc(self, container):
561531 tags = self ._get_tags (container , CONTAINER )
562532 self .service_check (HEALTHCHECK_SERVICE_CHECK_NAME , status , tags = tags )
563533
564- def _report_container_count_by_state (self , containers_by_id , state = "Any" ):
565- tags = {}
566- filterlambda = lambda x : not self ._is_container_excluded (x ) and state is "Any" or container ["State" ] is state
567- filtered = list (filter (filterlambda , containers_by_id ))
568- tags = self ._get_tags (filtered [0 ], PERFORMANCE )
569-
534+ def _report_container_count (self , containers_by_id ):
535+ """Report container count per state"""
570536 m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
571- # Report docker.container.count if state is "Any", otherwise
572- # report docker.container.state_STATE.count
573- suffix = ".state_{}" .format (state .lower ()) if state is not "Any" else ""
574- m_func (self , 'docker.container{}.count' .format (suffix ), len (filtered ), tags = tags )
575537
576- def _report_volume_count (self , filters = {}):
577- volumes = self .docker_client .volumes (filters = filters )
578- count = len (volumes ['Volumes' ])
538+ per_state_count = defaultdict (int )
539+
540+ filterlambda = lambda ctr : not self ._is_container_excluded (ctr )
541+ containers = list (filter (filterlambda , containers_by_id .values ()))
542+
543+ for ctr in containers :
544+ per_state_count [ctr .get ('State' , '' )] += 1
579545
546+ for state in per_state_count :
547+ if state :
548+ m_func (self , 'docker.container.count' , per_state_count [state ], tags = ['container_state:%s' % state .lower ()])
549+
550+ def _report_volume_count (self ):
551+ """Report volume count per state (dangling or not)"""
580552 m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
581- suffix = '.' + '-' .join (sorted (filters .keys ())) if len (filters ) is not 0 else ''
582- m_func (self , 'docker.volumes{}.count' .format (suffix ), count )
553+
554+ attached_volumes = self .docker_client .volumes (filters = {'dangling' : False })
555+ dangling_volumes = self .docker_client .volumes (filters = {'dangling' : True })
556+ attached_count = len (attached_volumes ['Volumes' ])
557+ dangling_count = len (dangling_volumes ['Volumes' ])
558+ m_func (self , 'docker.volume.count' , attached_count , tags = ['volume_state:attached' ])
559+ m_func (self , 'docker.volume.count' , dangling_count , tags = ['volume_state:dangling' ])
583560
584561 def _report_image_size (self , images ):
585562 for image in images :
@@ -599,6 +576,7 @@ def _report_performance_metrics(self, containers_by_id):
599576 continue
600577
601578 tags = self ._get_tags (container , PERFORMANCE )
579+
602580 self ._report_cgroup_metrics (container , tags )
603581 if "_proc_root" not in container :
604582 containers_without_proc_root .append (DockerUtil .container_name_extractor (container )[0 ])
0 commit comments