118118CONTAINER = "container"
119119PERFORMANCE = "performance"
120120FILTERED = "filtered"
121+ HEALTHCHECK = "healthcheck"
121122IMAGE = "image"
122123
123124ECS_INTROSPECT_DEFAULT_PORT = 51678
124125
126+ def compile_filter_rules (rules ):
127+ patterns = []
128+ tag_names = []
129+
130+ for rule in rules :
131+ patterns .append (re .compile (rule ))
132+ tag_names .append (rule .split (':' )[0 ])
133+
134+ return patterns , tag_names
125135
126136def get_filters (include , exclude ):
127137 # The reasoning is to check exclude first, so we can skip if there is no exclude
@@ -133,12 +143,11 @@ def get_filters(include, exclude):
133143 include_patterns = []
134144
135145 # Compile regex
136- for rule in exclude :
137- exclude_patterns .append (re .compile (rule ))
138- filtered_tag_names .append (rule .split (':' )[0 ])
139- for rule in include :
140- include_patterns .append (re .compile (rule ))
141- filtered_tag_names .append (rule .split (':' )[0 ])
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 )
142151
143152 return set (exclude_patterns ), set (include_patterns ), set (filtered_tag_names )
144153
@@ -208,6 +217,15 @@ def init(self):
208217 self ._exclude_patterns , self ._include_patterns , _filtered_tag_names = get_filters (include , exclude )
209218 self .tag_names [FILTERED ] = _filtered_tag_names
210219
220+
221+ # get the health check whitelist
222+ health_scs_whitelist = instance .get ('health_service_check_whitelist' , [])
223+ if health_scs_whitelist :
224+ patterns , whitelist_tags = compile_filter_rules (health_scs_whitelist )
225+ self .whitelist_patterns = set (patterns )
226+ self .tag_names [HEALTHCHECK ] = set (whitelist_tags )
227+
228+
211229 # Other options
212230 self .collect_image_stats = _is_affirmative (instance .get ('collect_images_stats' , False ))
213231 self .collect_container_size = _is_affirmative (instance .get ('collect_container_size' , False ))
@@ -251,10 +269,8 @@ def check(self, instance):
251269 # containers running with custom cgroups?
252270 custom_cgroups = _is_affirmative (instance .get ('custom_cgroups' , False ))
253271
254- # submit healtcheck service checks?
255- health_service_checks = _is_affirmative (instance .get ('health_service_checks' , False ))
256-
257272 # Get the list of containers and the index of their names
273+ health_service_checks = True if self .whitelist_patterns else False
258274 containers_by_id = self ._get_and_count_containers (custom_cgroups , health_service_checks )
259275 containers_by_id = self ._crawl_container_pids (containers_by_id , custom_cgroups )
260276
@@ -272,10 +288,8 @@ def check(self, instance):
272288 if self .collect_disk_stats :
273289 self ._report_disk_stats ()
274290
275- # Report docker healthcheck SC's where available
276291 if health_service_checks :
277- health_scs_whitelist = set (instance .get ('health_service_check_whitelist' , []))
278- self ._send_container_healthcheck_sc (containers_by_id , health_scs_whitelist )
292+ self ._send_container_healthcheck_sc (containers_by_id )
279293
280294 def _count_and_weigh_images (self ):
281295 try :
@@ -522,23 +536,34 @@ def _report_container_size(self, containers_by_id):
522536 self , 'docker.container.size_rootfs' , container ['SizeRootFs' ],
523537 tags = tags )
524538
525- def _send_container_healthcheck_sc (self , containers_by_id , whitelist ):
526- """Send health service checks for containers. Whitelist should preferably be a set. """
539+ def _send_container_healthcheck_sc (self , containers_by_id ):
540+ """Send health service checks for containers."""
527541 for container in containers_by_id .itervalues ():
528- if container .get ('Image' ) not in whitelist :
529- continue
542+ healthcheck_tags = self ._get_tags (container , HEALTHCHECK )
543+ match = False
544+ for tag in healthcheck_tags :
545+ for rule in self .whitelist_patterns :
546+ if re .match (rule , tag ):
547+ match = True
548+
549+ self ._submit_healthcheck_sc (container )
550+ break
530551
531- health = container .get ('health' , {})
532- tags = self ._get_tags (container , CONTAINER )
533- status = AgentCheck .UNKNOWN
534- if health :
535- _health = health .get ('Status' , '' )
536- if _health == 'unhealthy' :
537- status = AgentCheck .CRITICAL
538- elif _health == 'healthy' :
539- status = AgentCheck .OK
540-
541- self .service_check (HEALTHCHECK_SERVICE_CHECK_NAME , status , tags = tags )
552+ if match :
553+ break
554+
555+ def _submit_healthcheck_sc (self , container ):
556+ health = container .get ('health' , {})
557+ status = AgentCheck .UNKNOWN
558+ if health :
559+ _health = health .get ('Status' , '' )
560+ if _health == 'unhealthy' :
561+ status = AgentCheck .CRITICAL
562+ elif _health == 'healthy' :
563+ status = AgentCheck .OK
564+
565+ tags = self ._get_tags (container , CONTAINER )
566+ self .service_check (HEALTHCHECK_SERVICE_CHECK_NAME , status , tags = tags )
542567
543568 def _report_image_size (self , images ):
544569 for image in images :
0 commit comments