Skip to content

Commit

Permalink
Implement include/exclude rules. Set MAX_CONTAINERS_WITHOUT_RULES to …
Browse files Browse the repository at this point in the history
…limit

the number of collected containers if no rules are supplied.
  • Loading branch information
steeve committed Mar 7, 2014
1 parent d6e38a4 commit 3bfe7ec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
33 changes: 31 additions & 2 deletions checks.d/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import httplib
import socket
import os
import re
from urlparse import urlsplit, urljoin
from util import json, headers
from checks import AgentCheck

MAX_CONTAINERS_WITHOUT_RULES = 10

LXC_METRICS = [
{
Expand Down Expand Up @@ -119,13 +121,23 @@ def check(self, instance):
containers = self._get_containers(instance)
if not containers:
self.warning("No containers are running.")

# If we don't have include/exclude rules, we cap to
# MAX_CONTAINERS_WITHOUT_RULES containers to collect.
if not instance.get("exclude") or not instance.get("include"):
containers = containers[:MAX_CONTAINERS_WITHOUT_RULES]
for container in containers:
container_details = self._get_container(instance, container["Id"])
container_tags = list(tags)
for key in DOCKER_TAGS:
container_tags.append("%s:%s" % (key.lower(), container_details[key]))
container_tags.append(self._make_tag(key, container_details[key]))
for key in DOCKER_CONFIG_TAGS:
container_tags.append("%s:%s" % (key.lower(), container_details["Config"][key]))
container_tags.append(self._make_tag(key, container_details["Config"][key]))

# Check if the container is included/excluded via its tags
if not self._is_container_included(instance, container_tags):
continue

for key, (dd_key, metric_type) in DOCKER_METRICS.items():
if key in container:
getattr(self, metric_type)(dd_key, int(container[key]), tags=container_tags)
Expand All @@ -137,6 +149,23 @@ def check(self, instance):
if key in stats:
getattr(self, metric_type)(dd_key, int(stats[key]), tags=container_tags)

def _make_tag(self, key, value):
return "%s:%s" % (key.lower(), value)

def _is_container_included(self, instance, tags):
def _is_tag_included(tag):
for exclude_rule in instance.get("exclude") or []:
if re.match(exclude_rule, tag):
for include_rule in instance.get("include") or []:
if re.match(include_rule, tag):
return True
return False
return True
for tag in tags:
if _is_tag_included(tag):
return True
return False

def _get_containers(self, instance):
"""Gets the list of running containers in Docker."""
return self._get_json("%(url)s/containers/json" % instance, params={"size": 1})
Expand Down
24 changes: 24 additions & 0 deletions conf.d/docker.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,27 @@ init_config:

instances:
- url: "unix://var/run/docker.sock"

# Include/Exclude rules
#
# To include or exclude containers based on their tags, use the include and
# exclude keys in your instance.
# The reasoning is: if a tag matches an exclude rule, it won't be included
# unless it also matches an include rule.
#
# Examples:
# exclude all, except ubuntu and debian:
# instances:
# - url: "unix://var/run/docker.sock"
# include:
# - "image:ubuntu"
# - "image:debian"
# exclude:
# - ".*"
#
# include all, except ubuntu and debian:
# instances:
# - url: "unix://var/run/docker.sock"
# exclude:
# - "image:ubuntu"
# - "image:debian"

0 comments on commit 3bfe7ec

Please sign in to comment.