@@ -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."""
@@ -217,10 +199,7 @@ def init(self):
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 ))
219201 self .collect_container_count = _is_affirmative (instance .get ('collect_container_count' , False ))
220- self .collect_dead_container_count = _is_affirmative (instance .get ('collect_dead_container_count' , False ))
221- self .collect_exited_container_count = _is_affirmative (instance .get ('collect_exited_container_count' , False ))
222202 self .collect_volume_count = _is_affirmative (instance .get ('collect_volume_count' , False ))
223- self .collect_dangling_volume_count = _is_affirmative (instance .get ('collect_dangling_volume_count' , False ))
224203 self .collect_events = _is_affirmative (instance .get ('collect_events' , True ))
225204 self .collect_image_size = _is_affirmative (instance .get ('collect_image_size' , False ))
226205 self .collect_disk_stats = _is_affirmative (instance .get ('collect_disk_stats' , False ))
@@ -277,20 +256,11 @@ def check(self, instance):
277256 self ._report_container_size (containers_by_id )
278257
279258 if self .collect_container_count :
280- self ._report_container_count_by_state (containers_by_id )
281-
282- if self .collect_dead_container_count :
283- self ._report_container_count_by_state (containers_by_id , state = "Dead" )
284-
285- if self .collect_exited_container_count :
286- self ._report_container_count_by_state (containers_by_id , state = "Exited" )
259+ self ._report_container_count (containers_by_id )
287260
288261 if self .collect_volume_count :
289262 self ._report_volume_count ()
290263
291- if self .collect_dangling_volume_count :
292- self ._report_volume_count (filters = {'dangling' : True })
293-
294264 # Collect disk stats from Docker info command
295265 if self .collect_disk_stats :
296266 self ._report_disk_stats ()
@@ -367,7 +337,7 @@ def _get_and_count_containers(self, custom_cgroups=False, healthchecks=False):
367337 except Exception as e :
368338 self .log .debug ("Unable to inspect Docker container: %s" , e )
369339
370-
340+ # TODO: deprecate these 2, they should be replaced by _report_container_count
371341 for tags , count in running_containers_count .iteritems ():
372342 self .gauge ("docker.containers.running" , count , tags = list (tags ))
373343
@@ -559,26 +529,32 @@ def _submit_healthcheck_sc(self, container):
559529 tags = self ._get_tags (container , CONTAINER )
560530 self .service_check (HEALTHCHECK_SERVICE_CHECK_NAME , status , tags = tags )
561531
562- def _report_container_count_by_state (self , containers_by_id , state = "Any" ):
563- count = 0
564- tags = {}
565- filterlambda = lambda x : not self ._is_container_excluded (x ) and state is "Any" or container ["State" ] is state
566- filtered = list (filter (filterlambda , containers_by_id ))
567- tags = self ._get_tags (filtered [0 ], PERFORMANCE )
568-
532+ def _report_container_count (self , containers_by_id ):
533+ """Report container count per state"""
569534 m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
570- # Report docker.container.count if state is "Any", otherwise
571- # report docker.container.state_STATE.count
572- suffix = ".state_{}" .format (state .lower ()) if state is not "Any" else ""
573- m_func (self , 'docker.container{}.count' .format (suffix ), len (filtered ), tags = tags )
574535
575- def _report_volume_count (self , filters = {}):
576- volumes = self .docker_client .volumes (filters = filters )
577- count = len (volumes ['Volumes' ])
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
578543
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)"""
579550 m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
580- suffix = '.' + '-' .join (sorted (filters .keys ())) if len (filters ) is not 0 else ''
581- m_func (self , 'docker.volumes{}.count' .format (suffix ), count )
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' ])
582558
583559 def _report_image_size (self , images ):
584560 for image in images :
@@ -598,6 +574,7 @@ def _report_performance_metrics(self, containers_by_id):
598574 continue
599575
600576 tags = self ._get_tags (container , PERFORMANCE )
577+
601578 self ._report_cgroup_metrics (container , tags )
602579 if "_proc_root" not in container :
603580 containers_without_proc_root .append (DockerUtil .container_name_extractor (container )[0 ])
0 commit comments