@@ -218,6 +218,11 @@ def init(self):
218218 # Other options
219219 self .collect_image_stats = _is_affirmative (instance .get ('collect_images_stats' , False ))
220220 self .collect_container_size = _is_affirmative (instance .get ('collect_container_size' , False ))
221+ 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 ))
224+ 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 ))
221226 self .collect_events = _is_affirmative (instance .get ('collect_events' , True ))
222227 self .collect_image_size = _is_affirmative (instance .get ('collect_image_size' , False ))
223228 self .collect_disk_stats = _is_affirmative (instance .get ('collect_disk_stats' , False ))
@@ -273,6 +278,21 @@ def check(self, instance):
273278 if self .collect_container_size :
274279 self ._report_container_size (containers_by_id )
275280
281+ 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" )
289+
290+ if self .collect_volume_count :
291+ self ._report_volume_count ()
292+
293+ if self .collect_dangling_volume_count :
294+ self ._report_volume_count (filters = {'dangling' : True })
295+
276296 # Collect disk stats from Docker info command
277297 if self .collect_disk_stats :
278298 self ._report_disk_stats ()
@@ -505,7 +525,6 @@ def _report_container_size(self, containers_by_id):
505525 tags = self ._get_tags (container , PERFORMANCE )
506526 m_func = FUNC_MAP [GAUGE ][self .use_histogram ]
507527 if "SizeRw" in container :
508-
509528 m_func (self , 'docker.container.size_rw' , container ['SizeRw' ],
510529 tags = tags )
511530 if "SizeRootFs" in container :
@@ -542,6 +561,26 @@ def _submit_healthcheck_sc(self, container):
542561 tags = self ._get_tags (container , CONTAINER )
543562 self .service_check (HEALTHCHECK_SERVICE_CHECK_NAME , status , tags = tags )
544563
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+
570+ 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 )
575+
576+ def _report_volume_count (self , filters = {}):
577+ volumes = self .docker_client .volumes (filters = filters )
578+ count = len (volumes ['Volumes' ])
579+
580+ 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 )
583+
545584 def _report_image_size (self , images ):
546585 for image in images :
547586 tags = self ._get_tags (image , IMAGE )
0 commit comments